Magento 2: Get List Of All Payment Methods

In my previous blog Magento 2: Get List Of All Shipping Methods, I have explained to get all the shipping methods in Magento 2 programmatically. Here, I will show you how to get all the payment methods in Magento 2 programmatically.

We can get the payment methods programmatically in two ways, one is Dependency Injection (DI) and another one is Object Manager. But, Magento recommends using the dependency injection method. Whether you are developing an extension or a theme, avoid using the object manager method as you may receive technical rejection while submitting your extension to the Magento marketplace.

Using Dependency Injection (DI)

Here is the example code to get all the payment methods in Magento 2 using dependency injection.

In the below example, I will inject the object of Magento\Payment\Helper\Data class and Magento\Payment\Model\Config in the constructor of our module’s block class and access it from the view ( .phtml ) file.

  • Magento\Payment\Helper\Data : to get all the payment methods
  • Magento\Payment\Model\Config : to get active payment methods

Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php

<?php
namespace YourCompanyName\YourModuleName\Block;

class YourCustomBlock extends \Magento\Framework\View\Element\Template
{
    protected $paymentHelper;

    protected $paymentConfig;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Payment\Helper\Data $paymentHelper,
        \Magento\Payment\Model\Config $paymentConfig,
        array $data = []
    ) {
        $this->paymentHelper = $paymentHelper;
        $this->paymentConfig = $paymentConfig;
        parent::__construct($context, $data);
    }

    public function getAllPaymentMethods() {
        return $this->paymentHelper->getPaymentMethods();
    }

    public function getActivePaymentMethods() {
        return $this->paymentConfig->getActiveMethods();
    }
}

Now, we can use the functions in our view (.phtml) file as follows.

/*
 * Code to get all the payment methods
 */
$allPaymentMethods = $block->getAllPaymentMethods(); 
echo '<pre>'; print_r($allPaymentMethods);

/*
 * Code to get active payment methods
 */
$activePaymentMethods = $block->getActivePaymentMethods(); 
echo '<pre>'; print_r(array_keys($activePaymentMethods));

Using Object Manager

Here is the example code to get all the payment methods in Magento 2 using object manager.

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
       
/*
 * Code to get all the payment methods
 */
$paymentHelper = $objectManager->get('Magento\Payment\Helper\Data');
$allPaymentMethods = $paymentHelper->getPaymentMethods(); 
echo '<pre>'; print_r($allPaymentMethods);

/*
 * Code to get active payment methods
 */ 
$paymentConfig = $objectManager->get('Magento\Payment\Model\Config');
$activePaymentMethods = $paymentConfig->getActiveMethods(); 
echo '<pre>'; print_r(array_keys($activePaymentMethods));

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *