Successful e-commerce store demands to provide better user experience to grow their sales by offering and managing various payment methods at checkout page according to the needs of the specific customer as every customer has different demands. For example, the merchant which offers the cash on delivery for the local region may not be offered to international customers; In some cases, payment gateway requires certain taxes and fees for the different part which cannot be borne by the merchant.TO solve those issues, it has become vital for Magento store owners to optimize their payment methods according to different customer groups
To enable the payment process between customers and Magento store, we can restrict the payment methods by different customers groups by following the steps below.
1. Create an events.xml in : /app/code/Magemonkeys/PaymentMethod/etc
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="payment_method_is_active"> <observer name="Magemonkeys_PaymentMethod_DisabledPgByCustomergroup" instance="MagemonkeysPaymentMethodObserverDisabledPgByCustomergroup" /> </event> </config>
2. Create an DisabledPgByCustomergroup.php in : /app/code/Magemonkeys/PaymentMethod/Observer
<?php
namespace MagemonkeysPaymentMethodObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestDataPersistorInterface;
use MagentoFrameworkAppObjectManager;
class DisabledPgByCustomergroup implements ObserverInterface
{
public function execute(MagentoFrameworkEventObserver $observer)
{
$helper = MagentoFrameworkAppObjectManager::getInstance()->get('MagemonkeysPaymentMethodHelperData');
if($helper->isEnable()){
$customer_grp = $helper->isActiveGroup();
$result = $observer->getEvent()->getResult();
$method_instance = $observer->getEvent()->getMethodInstance();
$quote = $observer->getEvent()->getQuote();
if (null !== $quote && $quote->getCustomerGroupId() != 'CUSTOMER_GROUP_ID') {
if ($method_instance->getCode() == 'PAYMENT_METHOD_CODE') {
$result->setData('is_available', false);
}
}
}
}
}
?>
By following the above steps, you can restrict the payment methods in Magento 2 store.
If you have any doubt regarding the above method, feel free to ask me the comment section.
I would be happy to help you.

