Get All Payment Methods in Magento 2

Written by Jigar Patel

Sep 30, 2021

Get All Payment Methods in Magento 2

In Magento 2, a large number of payment methods internally and externally. I will show you how to get all payment methods in Magento 2.

Magento 2 supports different types of payment methods. To check out, log into your Magento 2 Admin Panel. Navigate to STORES > Configuration > Sales > Payment Methods.

I will be showing two ways (Dependency Injection & Object Manager ) to get payment methods.

Use Dependency Injection:

Here is the code that you need to write 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;
}

Use Object Manager:

If you want to get a list and information of all available payment methods on Magento programmatically you can use below mention code and you will get all payment method information.

$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']);            
}
var_dump($paymentMethods);

Output:

getAllPaymentMethods():

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
        )

getAllPaymentMethodsList():

Array
(
    [banktransfer] => Bank Transfer Payment
    [cashondelivery] => Cash On Delivery
    [checkmo] => Check / Money order
    [free] => No Payment Information Required
    [purchaseorder] => Purchase Order
)

getUsedPaymentMethods():

Array
(
    [0] => Array
        (
            [value] => 
            [label] => Any
        )

    [1] => Array
        (
            [value] => cashondelivery
            [label] => Cash On Delivery
        )

    [2] => Array
        (
            [value] => checkmo
            [label] => Check / Money order
        )

)

I hope this instruction will be helpful for you.

If you have any difficulties regarding this blog, do consider them posting in the Comments section below!

I’m here to help. Thank you!

Jigar Patel

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out This Form and one of Our Technical Experts will Contact you Within 12 Hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    TO TOP