To create a custom payment method in Magento 2, you will need to follow the following steps:
- Create a custom module for your payment method by creating a directory with the name of your module under
app/codedirectory of your Magento installation. - Create a
registration.phpfile in the root of your module directory with the following code:<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__ );Replace
Vendor_Modulewith the name of your module. - Create a
module.xmlfile in theetcdirectory of your module directory with the following code:<?xml version="1.0"?> <config xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Vendor_Module" setup_version="1.0.0"> <sequence> <module name="Magento_Payment"/> </sequence> </module> </config>Replace
Vendor_Modulewith the name of your module. - Create a
etc/payment.xmlfile in theetcdirectory of your module directory with the following code:<?xml version="1.0"?> <config xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Payment:etc/payment.xsd"> <payment> <groups> <group_name translate="true"> <methods> <method_name translate="true"> <title translate="true">Title of your payment method</title> <model>Vendor\Module\Model\PaymentMethod</model> <active>true</active> <order_status>pending</order_status> <can_use_checkout>true</can_use_checkout> </method_name> </methods> </group_name> </groups> </payment> </config>Replace
group_namewith the name of your payment group,method_namewith the name of your payment method, andVendor\Module\Model\PaymentMethodwith the namespace and class name of your payment method model. - Create a
Model/PaymentMethod.phpfile in theModeldirectory of your module directory with the following code:<?php namespace Vendor\Module\Model; class PaymentMethod extends \Magento\Payment\Model\Method\AbstractMethod { protected $_code = 'method_code'; }Replace
Vendor\Modulewith the namespace of your module,PaymentMethodwith the name of your payment method class, andmethod_codewith a unique code for your payment method. - Clear the Magento cache by running the following command in the terminal:
- php bin/magento cache:clean
Your custom payment method should now be visible in the Magento admin panel under Stores > Configuration > Sales > Payment Methods.
