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/code
directory of your Magento installation. - Create a
registration.php
file 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_Module
with the name of your module. - Create a
module.xml
file in theetc
directory of your module directory with the following code:<?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_Module" setup_version="1.0.0"> <sequence> <module name="Magento_Payment"/> </sequence> </module> </config>
Replace
Vendor_Module
with the name of your module. - Create a
etc/payment.xml
file in theetc
directory of your module directory with the following code:<?xml version="1.0"?> <config xmlns:xsi="http://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_name
with the name of your payment group,method_name
with the name of your payment method, andVendor\Module\Model\PaymentMethod
with the namespace and class name of your payment method model. - Create a
Model/PaymentMethod.php
file in theModel
directory 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\Module
with the namespace of your module,PaymentMethod
with the name of your payment method class, andmethod_code
with 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.