Create Custom Payment Methods in Magento 2.4.7

๐Ÿ›’ B-Online Store

Premium Magento 2 Extensions & Services

Enhance your e-commerce store with high-quality extensions and expert Magento development services. Trusted by thousands of merchants worldwide!

๐Ÿ’ฌ WhatsApp Button โšก Flash Sale ๐Ÿท๏ธ Product Labels ๐ŸŒ Translation Suite
๐Ÿ”ฅ UP TO 71% OFF Limited Time!
โญ Thousands of Merchants ๐Ÿ›ก๏ธ Quality Guaranteed โšก Instant Download

First we create our module registration.php in app/code/Itcforu/CustomPayment/registration.php

<?php

declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Itcforu_CustomPayment',
    __DIR__,
);

Now we create module.xml in app/code/Itcforu/CustomPayment/etc/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="Itcforu_CustomPayment">
    <sequence>
        <module name="Magento_Sales"/>
        <module name="Magento_Checkout"/>
        <module name="Magento_Theme"/>
    </sequence>
    </module>
</config>

Create system.xml file app/code/Itcforu/CustomPayment/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="payment">
            <group id="custom_payment" translate="label" type="text" sortOrder="150" showInDefault="1" showInWebsite="1"
                   showInStore="1">
                <label>Custom Payment</label>
                <field id="active" translate="label" type="select" sortOrder="5" showInDefault="1" showInWebsite="1"
                       showInStore="0" canRestore="1">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="title" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Method title</label>
                </field>
            </group>
        </section>
    </system>
</config>

Now we create config.xml in app/code/Itcforu/CustomPayment/etc/config.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <payment>
            <custom_payment>
                <active>1</active>
                <model>Itcforu\CustomPayment\Model\CustomPayment</model>
                <title>Custom Payment</title>
                <can_use_internal>1</can_use_internal>
                <group>offline</group>
            </custom_payment>
        </payment>
    </default>
</config>

Now we create our model app/code/Itcforu/CustomPayment/Model/CustomPayment.php

<?php

namespace Itcforu\CustomPayment\Model;

/**
 * Pay In Store payment method model
 */
class CustomPayment extends \Magento\Payment\Model\Method\AbstractMethod
{
    /**
     * Payment code
     *
     * @var string
     */
    protected $_code = 'custom_payment';
}

We create this custom payment method for use in create order in admin panel only not in checkout page if you want me to make tutorial add this in checkout page ping me in linked in.