magento2 checkout – How to Enable and Disable Payment Method in Magento 2

checkoutmagento2onepage-checkout

I have a custom payment method in magento 2.
I have added an enabled option in the payment settings page in the admin area, this is done in the same way as the payment methods from the offline payments module.

Currently this value affects nothing, you can always see the option for this payment method in the checkout even if enabled is set to no. Which piece of code makes this work for the offline payment options and how can I get my payment method to show or hide correctly?

Best Answer

Assuming that your module have the following structure and naming convention for the active field name you module should automatically get enabled/disabled. Since the module is enabled then the naming convention seem correct, so try flush cache or check to see if it is enabled on the store level.

config.xml

<system>
    <section id="payment">
        <group id="sample_gateway" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Sample Gateway</label>
            <field id="active" ....

See vendor/magento/module-payment/Model/Method/Adapter.php

/**
 * @inheritdoc
 */
public function isActive($storeId = null)
{
    return $this->getConfiguredValue('active', $storeId);
}