Magento, Magento 2

Magento 2 : List of payment methods using command line

List of payment methods in Magento 2

In this technical blog, We are going to learn how to get the payment methods list in Magento 2 using the command line. Magento is by default provides many commands like setup:upgrade, cache:clean, etc. But, There are no commands available for the check which payment methods available in Magento 2.

Sometimes, it may happen that we want to check which payment methods available in the Magento store. Now, using this module you can check all payment methods list by command line and you can check enabled and disabled command list by using the command line.

We can get the payment list programmatically in two ways :

  • Dependency Injection (DI)
  • Object Manager.

But, Magento recommends using the dependency injection method. So, we will use the Dependency Injection method.

Now, follow the below steps to create the module for get payment list in Magento 2 using the command line :

You may also like this blog :

Dependency Injection (DI) :

1) First of all, create registration.php file at app/code/RH/CustomCommand/ and paste the below code :


<?php
/**
 * Created By : Rohan Hapani
 */
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'RH_CustomCommand',
    __DIR__
);

2) Then, create module.xml file at app/code/RH/CustomCommand/etc and paste the below code :


<?xml version="1.0"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="RH_CustomCommand" setup_version="1.0.0" schema_version="1.0.0" />
</config>

3) Then, create di.xml file for define command option at app/code/RH/CustomCommand/etc/ and paste the below code :


<?xml version="1.0"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Console\CommandList">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="payment_list" xsi:type="object">RH\CustomCommand\Console\GetPaymentList</item>
            </argument>
        </arguments>
    </type>
</config>

4) At last, create command class GetPaymentList.php file at app/code/RH/CustomCommand/Console/ and paste the below code :


<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\CustomCommand\Console;

use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use \Symfony\Component\Console\Command\Command;
use \Symfony\Component\Console\Input\InputInterface;
use \Symfony\Component\Console\Output\OutputInterface;

class GetPaymentList extends Command
{
    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    private $scopeValue;

    /**
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeValue
     */
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeValue
    ) {
        $this->scopeValue = $scopeValue;
        parent::__construct();
    }

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this->setName('info:payment:list')
        ->setDescription('Display Payment Method List')
        ->addArgument('status', InputArgument::OPTIONAL, 'Optional module name');
        parent::configure();
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $moduleName = (string) $input->getArgument('status');

        if ($moduleName == "Enabled" || $moduleName == "enabled")
        {
            return $this->showEnabledPaymentMethodList($output);
        }
        if ($moduleName == "Disabled" || $moduleName == "disabled")
        {
            return $this->showDisabledPaymentMethodList($output);
        }
        if ($moduleName == '')
        {
            return $this->showAllPaymentMethodList($output);
        }
        if ($moduleName != "" && ($moduleName != "Enabled" || $moduleName != "enabled" || $moduleName != "Disabled" || $moduleName != "disabled"))
        {
            $output->writeln('<error>Please enter correct command</error>');
            return \Magento\Framework\Console\Cli::RETURN_FAILURE;
        }
    }

    /**
     * Return enabled payment method list
     */
    private function showEnabledPaymentMethodList(OutputInterface $output)
    {
        try {
            $table = new Table($output);
            $table->setHeaders(['Code', 'Name', 'Status']);
            $methodList = $this->scopeValue->getValue('payment');
            foreach ($methodList as $code => $_method)
            {
                $active_status = "";
                $title = "";

                if (isset($_method['active']))
                {
                    if ($_method['active'] == 1)
                    {
                        if (isset($_method['title']))
                        {
                            $title = $_method['title'];
                        }
                        $table->addRow([$code, $title, 'Enable']);
                    }
                }
            }
            $table->render();
            return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
        } catch (\Exception $e) {
            $output->writeln('<error>' . $e->getMessage() . '</error>');
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE)
            {
                $output->writeln($e->getTraceAsString());
            }
            return \Magento\Framework\Console\Cli::RETURN_FAILURE;
        }
    }

    /**
     * Return disabled payment method list
     */
    private function showDisabledPaymentMethodList(OutputInterface $output)
    {
        try {
            $table = new Table($output);
            $table->setHeaders(['Code', 'Name', 'Status']);
            $methodList = $this->scopeValue->getValue('payment');
            foreach ($methodList as $code => $_method)
            {
                $active_status = "";
                $title = "";

                if (isset($_method['active']))
                {
                    if ($_method['active'] == 0)
                    {
                        if (isset($_method['title']))
                        {
                            $title = $_method['title'];
                        }
                        $table->addRow([$code, $title, 'Disabled']);
                    }
                }
            }
            $table->render();
            return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
        } catch (\Exception $e) {
            $output->writeln('<error>' . $e->getMessage() . '</error>');
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE)
            {
                $output->writeln($e->getTraceAsString());
            }
            return \Magento\Framework\Console\Cli::RETURN_FAILURE;
        }
    }

    /**
     * Return all payment method list
     */
    private function showAllPaymentMethodList(OutputInterface $output)
    {
        try {
            $table = new Table($output);
            $table->setHeaders(['Code', 'Name', 'Status']);
            $methodList = $this->scopeValue->getValue('payment');
            foreach ($methodList as $code => $_method)
            {
                $active_status = "";
                $title = "";
                if (isset($_method['active']))
                {
                    if ($_method['active'] == 1)
                    {
                        $active_status = "Enable";
                    }
                    else
                    {
                        $active_status = "Disable";
                    }
                }
                if (isset($_method['title']))
                {
                    $title = $_method['title'];
                }
                $table->addRow([$code, $title, $active_status]);
            }
            $table->render();
            return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
        } catch (\Exception $e) {
            $output->writeln('<error>' . $e->getMessage() . '</error>');
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE)
            {
                $output->writeln($e->getTraceAsString());
            }
            return \Magento\Framework\Console\Cli::RETURN_FAILURE;
        }
    }
}

Now, Open Command line in folder root of Magento and run commands.


php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:c

In conclusion, you can see where your custom command listed in php bin/magento list

Get-Payment-List-Terminal

Finally, Module is ready for get payment list 🙂

You can use below commands for different requirements :

  • Enabled payment list only : php bin/magento info:payment:list enabled

List of active payment methods

  • Disabled payment list only : php bin/magento info:payment:list disabled

List of inactive payment methods

  • All payment list : php bin/magento info:payment:list

List of payment methods

I hope this blog is easy to understand about get payment methods list using command line in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution 🙂

Tagged , ,