Magento 2 how to create custom payment method

By | March 9, 2023
Spread the love

To create a custom payment method in Magento 2, you will need to follow the following steps:

  1. 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.
  2. 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.

  3.  Create a module.xml file in the etc 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.

  4.  Create a etc/payment.xml file in the etc 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, and Vendor\Module\Model\PaymentMethod with the namespace and class name of your payment method model.

  5.  Create a Model/PaymentMethod.php file in the Model 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, and method_code with a unique code for your payment method.

  6.  Clear the Magento cache by running the following command in the terminal:
  7.   php bin/magento cache:clean

Your custom payment method should now be visible in the Magento admin panel under Stores > Configuration > Sales > Payment Methods.