CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

23 February, 2022

Magento 2 Get All Payment Methods

 Programing Coderfunda     February 23, 2022     Magento 2, MAGENTO TUTORIALS     No comments   

 

Magento 2 Get All Payment Methods

In Magento 2, there will be 3 types of payment methods that you will be fetching which are All Payment Methods, Active/Enabled Payment Methods and Payment Methods that have been used while placing orders.

In today’s post, I will show you how to get all payment methods in Magento 2.

How to retrieve all payment methods in Magento 2 via two methods

  • Method 1: Use Dependency Injection
  • Method 2: Use Object Manager

Method 1: Use Dependency Injection

When using this method, you need to write the following code in your Block class.

/**
 * Order Payment
 *
 * @var \Magento\Sales\Model\ResourceModel\Order\Payment\Collection
 */
protected $_orderPayment;
 
/**
 * Payment Helper Data
 *
 * @var \Magento\Payment\Helper\Data
 */
protected $_paymentHelper;
 
/**
 * Payment Model Config
 *
 * @var \Magento\Payment\Model\Config
 */
protected $_paymentConfig;
 
/**
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Sales\Model\ResourceModel\Order\Payment\Collection $orderPayment
 * @param \Magento\Payment\Helper\Data $paymentHelper
 * @param \Magento\Payment\Model\Config $paymentConfig
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Sales\Model\ResourceModel\Order\Payment\Collection $orderPayment,
    \Magento\Payment\Helper\Data $paymentHelper,
    \Magento\Payment\Model\Config $paymentConfig,
    array $data = []
) {
    $this->_orderPayment = $orderPayment;
    $this->_paymentHelper = $paymentHelper;
    $this->_paymentConfig = $paymentConfig;
    parent::__construct($context, $data);
}
 
/**
 * Get all payment methods
 * 
 * @return array
 */ 
public function getAllPaymentMethods() 
{
    return $this->_paymentHelper->getPaymentMethods();
}
 
/**
 * Get key-value pair of all payment methods
 * key = method code & value = method name
 * 
 * @return array
 */ 
public function getAllPaymentMethodsList() 
{
    return $this->_paymentHelper->getPaymentMethodList();
}
 
/**
 * Get active/enabled payment methods
 * 
 * @return array
 */ 
public function getActivePaymentMethods() 
{
    return $this->_paymentConfig->getActiveMethods();
}
 
/**
 * Get payment methods that have been used for orders
 * 
 * @return array
 */ 
public function getUsedPaymentMethods() 
{
    $collection = $this->_orderPayment;
    $collection->getSelect()->group('method');
    $paymentMethods[] = array('value' => '', 'label' => 'Any');
    foreach ($collection as $col) { 
        $paymentMethods[] = array('value' => $col->getMethod(), 'label' => $col->getAdditionalInformation()['method_title']);            
    }        
    return $paymentMethods;
}

Method 2: Use Object Manager

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
 
$paymentHelper = $objectManager->get('Magento\Payment\Helper\Data');
$allPaymentMethods = $paymentHelper->getPaymentMethods();
$allPaymentMethodsArray = $paymentHelper->getPaymentMethodList();
 
var_dump($allPaymentMethodsArray);
var_dump($allPaymentMethods);
 
$paymentConfig = $objectManager->get('Magento\Payment\Model\Config');
$activePaymentMethods = $paymentConfig->getActiveMethods();
 
var_dump(array_keys($activePaymentMethods));
 
$orderPaymentCollection = $objectManager->get('\Magento\Sales\Model\ResourceModel\Order\Payment\Collection');
$orderPaymentCollection->getSelect()->group('method');
$paymentMethods[] = array('value' => '', 'label' => 'Any');
foreach ($orderPaymentCollection as $col) { 
    $paymentMethods[] = array('value' => $col->getMethod(), 'label' => $col->getAdditionalInformation()['method_title']);            
}        

Here is the getAllPaymentMethods()’s stample output

Array
(
    [free] => Array
        (
            [active] => 1
            [model] => Magento\Payment\Model\Method\Free
            [order_status] => pending
            [title] => No Payment Information Required
            [allowspecific] => 0
            [sort_order] => 1
            [group] => offline
        )
 
    [substitution] => Array
        (
            [active] => 0
            [model] => Magento\Payment\Model\Method\Substitution
            [allowspecific] => 0
        )
 
    [vault] => Array
        (
            [debug] => 1
            [model] => Magento\Vault\Model\VaultPaymentInterface
        )
 
    [checkmo] => Array
        (
            [active] => 1
            [model] => Magento\OfflinePayments\Model\Checkmo
            [order_status] => pending
            [title] => Check / Money order
            [allowspecific] => 0
            [group] => offline
        )
 
    [purchaseorder] => Array
        (
            [active] => 0
            [model] => Magento\OfflinePayments\Model\Purchaseorder
            [order_status] => pending
            [title] => Purchase Order
            [allowspecific] => 0
            [group] => offline
        )
 
    [banktransfer] => Array
        (
            [active] => 0
            [model] => Magento\OfflinePayments\Model\Banktransfer
            [order_status] => pending
            [title] => Bank Transfer Payment
            [allowspecific] => 0
            [group] => offline
        )
 
    [cashondelivery] => Array
        (
            [active] => 0
            [model] => Magento\OfflinePayments\Model\Cashondelivery
            [order_status] => pending
            [title] => Cash On Delivery
            [allowspecific] => 0
            [group] => offline
        )
 
    [paypal_express] => Array
        (
            [model] => Magento\Paypal\Model\Express
            [title] => PayPal Express Checkout
            [payment_action] => Authorization
            [solution_type] => Mark
            [line_items_enabled] => 1
            [visible_on_cart] => 1
            [visible_on_product] => 1
            [allow_ba_signup] => never
            [group] => paypal
            [authorization_honor_period] => 3
            [order_valid_period] => 29
            [child_authorization_number] => 1
            [verify_peer] => 1
            [skip_order_review_step] => 1
        )
 
    [paypal_express_bml] => Array
        (
            [model] => Magento\Paypal\Model\Bml
            [title] => PayPal Credit
            [group] => paypal
        )
 
    [payflow_express] => Array
        (
            [title] => PayPal Express Checkout Payflow Edition
            [payment_action] => Authorization
            [line_items_enabled] => 1
            [visible_on_cart] => 1
            [visible_on_product] => 1
            [group] => paypal
            [verify_peer] => 1
            [model] => Magento\Paypal\Model\PayflowExpress
        )
 
    [payflow_express_bml] => Array
        (
            [model] => Magento\Paypal\Model\Payflow\Bml
            [title] => PayPal Credit
            [group] => paypal
        )
 
    [payflowpro] => Array
        (
            [model] => Magento\Paypal\Model\Payflow\Transparent
            [title] => Credit Card
            [payment_action] => Authorization
            [cctypes] => AE,VI
            [useccv] => 1
            [tender] => C
            [verbosity] => MEDIUM
            [user] => 
            [pwd] => 
            [group] => paypal
            [verify_peer] => 1
            [date_delim] => 
            [ccfields] => csc,expdate,acct
            [place_order_url] => paypal/transparent/requestSecureToken
            [cgi_url_test_mode] => https://pilot-payflowlink.paypal.com
            [cgi_url] => https://payflowlink.paypal.com
            [transaction_url_test_mode] => https://pilot-payflowpro.paypal.com
            [transaction_url] => https://payflowpro.paypal.com
            [avs_street] => 0
            [avs_zip] => 0
            [avs_international] => 0
            [avs_security_code] => 1
            [cc_year_length] => 2
            [can_authorize_vault] => 1
            [can_capture_vault] => 1
        )
 
    [payflowpro_cc_vault] => Array
        (
            [model] => PayflowProCreditCardVaultFacade
            [title] => Stored Cards (Payflow Pro)
        )
 
    [paypal_billing_agreement] => Array
        (
            [active] => 1
            [allow_billing_agreement_wizard] => 1
            [model] => Magento\Paypal\Model\Method\Agreement
            [title] => PayPal Billing Agreement
            [group] => paypal
            [verify_peer] => 1
        )
 
    [payflow_link] => Array
        (
            [model] => Magento\Paypal\Model\Payflowlink
            [payment_action] => Authorization
            [verbosity] => HIGH
            [user] => 
            [pwd] => 
            [group] => paypal
            [title] => Credit Card
            [partner] => PayPal
            [csc_required] => 1
            [csc_editable] => 1
            [url_method] => GET
            [email_confirmation] => 0
            [verify_peer] => 1
            [transaction_url_test_mode] => https://pilot-payflowpro.paypal.com
            [transaction_url] => https://payflowpro.paypal.com
            [cgi_url_test_mode] => https://pilot-payflowlink.paypal.com
            [cgi_url] => https://payflowlink.paypal.com
        )
 
    [payflow_advanced] => Array
        (
            [model] => Magento\Paypal\Model\Payflowadvanced
            [payment_action] => Authorization
            [verbosity] => HIGH
            [user] => PayPal
            [pwd] => 
            [group] => paypal
            [title] => Credit Card
            [partner] => PayPal
            [vendor] => PayPal
            [csc_required] => 1
            [csc_editable] => 1
            [url_method] => GET
            [email_confirmation] => 0
            [verify_peer] => 1
            [transaction_url_test_mode] => https://pilot-payflowpro.paypal.com
            [transaction_url] => https://payflowpro.paypal.com
            [cgi_url_test_mode] => https://pilot-payflowlink.paypal.com
            [cgi_url] => https://payflowlink.paypal.com
        )
 
    [hosted_pro] => Array
        (
            [model] => Magento\Paypal\Model\Hostedpro
            [title] => Payment by cards or by PayPal account
            [payment_action] => Authorization
            [group] => paypal
            [display_ec] => 0
            [verify_peer] => 1
        )
 
    [authorizenet_directpost] => Array
        (
            [active] => 0
            [cctypes] => AE,VI,MC,DI
            [debug] => 0
            [email_customer] => 0
            [login] => 
            [merchant_email] => 
            [model] => Magento\Authorizenet\Model\Directpost
            [order_status] => processing
            [payment_action] => authorize
            [test] => 1
            [title] => Credit Card Direct Post (Authorize.net)
            [trans_key] => 
            [trans_md5] => 
            [allowspecific] => 0
            [currency] => USD
            [create_order_before] => 1
            [date_delim] => /
            [ccfields] => x_card_code,x_exp_date,x_card_num
            [place_order_url] => authorizenet/directpost_payment/place
            [cgi_url_test_mode] => https://test.authorize.net/gateway/transact.dll
            [cgi_url] => https://secure.authorize.net/gateway/transact.dll
            [cgi_url_td_test_mode] => https://apitest.authorize.net/xml/v1/request.api
            [cgi_url_td] => https://api2.authorize.net/xml/v1/request.api
        )
 
    [braintree] => Array
        (
            [model] => BraintreeFacade
            [title] => Credit Card (Braintree)
            [payment_action] => authorize
            [active] => 0
            [is_gateway] => 1
            [can_use_checkout] => 1
            [can_authorize] => 1
            [can_capture] => 1
            [can_capture_partial] => 1
            [can_authorize_vault] => 1
            [can_capture_vault] => 1
            [can_use_internal] => 1
            [can_refund_partial_per_invoice] => 1
            [can_refund] => 1
            [can_void] => 1
            [can_cancel] => 1
            [cctypes] => AE,VI,MC,DI,JCB,CUP,DN,MI
            [useccv] => 1
            [cctypes_braintree_mapper] => {"american-express":"AE","discover":"DI","jcb":"JCB","mastercard":"MC","master-card":"MC","visa":"VI","maestro":"MI","diners-club":"DN","unionpay":"CUP"}
            [order_status] => processing
            [environment] => sandbox
            [allowspecific] => 0
            [sdk_url] => https://js.braintreegateway.com/js/braintree-2.17.6.min.js
            [public_key] => 
            [private_key] => 
            [masked_fields] => cvv,number
            [privateInfoKeys] => avsPostalCodeResponseCode,avsStreetAddressResponseCode,cvvResponseCode,processorAuthorizationCode,processorResponseCode,processorResponseText,liabilityShifted,liabilityShiftPossible,riskDataId,riskDataDecision
            [paymentInfoKeys] => cc_type,cc_number,avsPostalCodeResponseCode,avsStreetAddressResponseCode,cvvResponseCode,processorAuthorizationCode,processorResponseCode,processorResponseText,liabilityShifted,liabilityShiftPossible,riskDataId,riskDataDecision
        )
 
    [braintree_paypal] => Array
        (
            [model] => BraintreePayPalFacade
            [title] => PayPal (Braintree)
            [active] => 0
            [payment_action] => authorize
            [allowspecific] => 0
            [require_billing_address] => 0
            [allow_shipping_address_override] => 1
            [display_on_shopping_cart] => 1
            [order_status] => processing
            [is_gateway] => 1
            [can_use_checkout] => 1
            [can_authorize] => 1
            [can_capture] => 1
            [can_capture_partial] => 1
            [can_refund] => 1
            [can_refund_partial_per_invoice] => 1
            [can_void] => 1
            [can_cancel] => 1
            [privateInfoKeys] => processorResponseCode,processorResponseText,paymentId
            [paymentInfoKeys] => processorResponseCode,processorResponseText,paymentId,payerEmail
        )
 
    [braintree_cc_vault] => Array
        (
            [model] => BraintreeCreditCardVaultFacade
            [title] => Stored Cards (Braintree)
        )

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Sitaare Zameen Par Full Movie Review
     Here’s a  complete Vue.js tutorial for beginners to master level , structured in a progressive and simple way. It covers all essential topi...
  • Writing and debugging Eloquent queries with Tinkerwell
    In this article, let's look into the options that you can use with Tinkerwell to write and debug Eloquent queries easier. The post Wr...
  • The token request was rejected by the remote server
    error:invalid_granterror_description:The token request was rejected by the remote server.error_uri: https://documentation.openiddict.com/err...
  • Laravel Search String
      Laravel Search String is a package by   Loris Leiva   that generates database queries based on one unique string using a simple and custom...
  • AI foot tracking model
    I am a student doing a graduation project. I urgently need to deal with this model (I am attaching a link). I've never worked with pytho...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (69)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • July (4)
  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

Loading...

Laravel News

Loading...

Copyright © CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda