Magento 2: Disable and enable payment method programmatically

To use Object Manager is not proper way but as i know this may be write answer. it helps you.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$conf = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('payment/cashondelivery/active');
$objectManager->get('Magento\Framework\App\Config\Storage\WriterInterface')->save('payment/cashondelivery/active', 1);
/*
your code
*/
$objectManager->get('Magento\Framework\App\Config\Storage\WriterInterface')->save('payment/cashondelivery/active', 0);

Refresh Cache.

See below link

Refresh the cache programmatically in Magento 2 at window system


You have to use Magento\Config\Model\ResourceModel\Config object.

Like Following Code

$this->resourceConfig->saveConfig('payment/cashondelivery/active', '1', 'default', ScopeInterface::SCOPE_STORES);

after complition order you need to set to 0

It will help you