How to Get Active Payment Methods in Magento 2

In this tutorial we are going to show you how to get active payment methods list in Magento 2, This article will guide you how to get active payment methods in Magento 2 step by step.

Now let’s code!

Follow the below code to get active payment methods:

<?php
 
namespace MageBeginners\Test\Model;
 
use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Payment\Model\Config;
 
class Paymentmethod extends \Magento\Framework\DataObject 
    implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * @var ScopeConfigInterface
     */
    protected $_appConfigScopeConfigInterface;
    /**
     * @var Config
     */
    protected $_paymentModelConfig;
     
    /**
     * @param ScopeConfigInterface $appConfigScopeConfigInterface
     * @param Config               $paymentModelConfig
     */
    public function __construct(
        ScopeConfigInterface $appConfigScopeConfigInterface,
        Config $paymentModelConfig
    ) {
 
        $this->_appConfigScopeConfigInterface = $appConfigScopeConfigInterface;
        $this->_paymentModelConfig = $paymentModelConfig;
    }
  
    public function toOptionArray()
    {
        $payments = $this->_paymentModelConfig->getActiveMethods();
        $methods = array();
        foreach ($payments as $paymentCode => $paymentModel) {
            $paymentTitle = $this->_appConfigScopeConfigInterface
                ->getValue('payment/'.$paymentCode.'/title');
            $methods[$paymentCode] = array(
                'label' => $paymentTitle,
                'value' => $paymentCode
            );
        }
        return $methods;
    }
}

$_appConfigScopeConfigInterface is an object of Magento\Framework\App\Config\ScopeConfigInterface class.

$_paymentModelConfig is an object of Magento\Payment\Model\Config class, and in toOptionArray() function we use a method: getActiveMethods() which returns model of all the active methods.

How to Get Active Payment Methods in Magento 2 Click To Tweet

We hope this article helped you learn how to get active payment methods in Magento 2. You may also want to see our list of How to Change Favicon in Magento 2.

If you liked this article, then please subscribe to our Newsletter for Magento tutorials. You can also find us on Twitter and Facebook. If you have any problem or would like to add to the discussion, leave a comment below!

You May Also Like

Editorial Staff

About the Author: Editorial Staff

Editorial Staff at the MageBeginners is a team of Magento experts writing about Magento Tutorials, Themes, Extensions, Guides, Tricks and much more...

Leave a Reply

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