💳 Magento 2 Custom Payment Gateway – Create Your Own Payment Module
Magento 2 allows you to build your own custom payment gateways to integrate with local providers, in-house systems, or special checkout flows. Let’s walk through creating a basic custom payment module.
🧱 Folder Structure for Custom Payment Module
Create the module at app/code/Vendor/CustomPayment:
Vendor/
└── CustomPayment/
├── etc/
│ ├── module.xml
│ ├── config.xml
│ └── di.xml
├── Model/
│ └── PaymentMethod.php
├── view/frontend/web/template/payment/
│ └── custompayment.html
├── view/frontend/layout/
│ └── checkout_index_index.xml
├── registration.php
└── composer.json
📦 Module Declaration – module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_CustomPayment" setup_version="1.0.0"/>
</config>
⚙️ Payment Model – PaymentMethod.php
This is the brain of your gateway, handling method settings and availability.
namespace Vendor\CustomPayment\Model;
use Magento\Payment\Model\Method\AbstractMethod;
class PaymentMethod extends AbstractMethod
{
protected $_code = 'custompayment';
protected $_isOffline = true; // no API call
public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
{
return true;
}
}
🔧 Configuration – config.xml
This will make your method appear in the admin settings.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/config.xsd">
<default>
<payment>
<custompayment>
<active>1</active>
<model>Vendor\CustomPayment\Model\PaymentMethod</model>
<title>Custom Payment</title>
<order_status>pending</order_status>
<allowspecific>0</allowspecific>
<sort_order>100</sort_order>
</custompayment>
</payment>
</default>
</config>
🛠️ Dependency Injection – di.xml
Map your payment method in Magento’s dependency injection system:
<type name="Magento\Payment\Model\Method\Factory">
<arguments>
<argument name="methods" xsi:type="array">
<item name="custompayment" xsi:type="string">Vendor\CustomPayment\Model\PaymentMethod</item>
</argument>
</arguments>
</type>
✅ Register the Module
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_CustomPayment',
__DIR__
);
🚀 Final Steps
- Run
php bin/magento setup:upgrade - Run
php bin/magento cache:flush - Enable the payment method in Stores → Configuration → Sales → Payment Methods
📌 Summary
- You now have a working custom payment method
- This can be extended to include API calls, custom validation, and more
- Magento’s modular structure makes payment integration scalable
With this custom gateway, you’re in full control of your checkout process! 💰