Magento 2 – How to Get Active Payment Methods List Store Wise

magento2methodspaymentstores

I want to get all active payment method list store wise.

I have two store

— store indstore

— store usstore

And i have set payment methods for only indstore
and if i try to get all active methods ,it returns from default store settings
and from default settings payment methods are disable.

if i enable from default settings than i got all payment methods.

Does anyone know how i can get methods store wise?

app\code\Vendor\Extension\Model\System\Methods.php

<?php

namespace Vendor\Extension\Model\System;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Payment\Model\Config;
class Methods implements \Magento\Framework\Option\ArrayInterface
{
    protected $scopeConfig;
    protected $paymentmodelconfig;

    public function __construct(Config $paymentmodelconfig, ScopeConfigInterface $scopeConfig)
    {
        $this->paymentmodelconfig = $paymentmodelconfig;
        $this->scopeConfig = $scopeConfig;
    }

    public function toOptionArray()
    {
        $payments = $this->paymentmodelconfig->getActiveMethods();

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);

        $methodList = $this->scopeConfig->getValue('payment',\Magento\Store\Model\ScopeInterface::SCOPE_STORE,1);

            foreach ($methodList as $code => $_method) {
                $active_status = "";
                $title = "";
                if (isset($_method['active']))
                {
                    if ($_method['active'] == 1)
                    {
                        if (isset($_method['title']))
                        {
                            $title = $_method['title'];
                            $logger->info($title);
                        }
                    }
                }
                 $methods = array();
                 return $methods;
        }
    }
}

Output IN Log File

2019-12-24T05:55:20+00:00 INFO (6): Check / Money order

Paypal is also active but i am getting only check/money order

Best Answer

class ABC{
    protected $paymentMethodList;
    public function __construct(
        \Magento\Payment\Model\PaymentMethodList $paymentMethodList
    ) {
        $this->paymentMethodList = $paymentMethodList;
    }

    public function getmethods()
    {
        $storeId = 'your store id';
        $this->paymentMethodList->getActiveList($storeId);
    }
}
Related Topic