Hello Magento Lovers, Let's have check, how can we disable payment method programmatically at runtime. We have disabled check memo payment method in this post.

    <?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="disable_checkmo" instance="Magefever\PaymentDemo\Observer\DisableCheckMo" />
    </event>
</config>
	

    <?php
namespace Magefever\PaymentDemo\Observer;

use Magento\Framework\Event\ObserverInterface;

class DisableCheckMo implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        if($observer->getEvent()->getMethodInstance()->getCode()=="checkmo"){
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', false); 
        }
    }
}
	

 

ou may use your payment method's code at below line in order to disable payment method

$observer->getEvent()->getMethodInstance()->getCode()=="checkmo"

 

 

Let us know your query in the comment section, we are happy to help. Thank you.