Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How To Get All Payment Methods In Magento 2

Vinh Jacker | 12-18-2024

Get all payment methods

In the past, cash was the preferred method of payment, but nowadays, online transactions have become the prime payment method for many people. If you want to build a Magento store or already have one, it’s essential to ensure your Magento store supports all payment methods. Offering flexibility in payment options can significantly enhance the shopping experience and increase conversion rates.

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 using two methods: dependency injection and object manager.

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

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)
        )

Final words

Above is the guide on how to get all payment methods by two different methods. I hope this tutorial is helpful for you. If you have any trouble while following this tutorial, feel free to let me know. I will reach to you as soon as possible.

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services