Creating an Online Payment Method in Magento 2: A Step-by-Step Guide

Magento 2 is a versatile and powerful eCommerce platform that provides extensive customization options for developers. One essential feature for any online store is the ability to offer various payment methods. In this blog post, we’ll show you how to create a custom online payment method in Magento 2. Follow this step-by-step guide, complete with code snippets, to integrate your own payment solution into Magento 2.

Why Create a Custom Payment Method?

  1. Business Requirements: Tailor the payment process to meet specific business needs.
  2. Customer Convenience: Provide customers with their preferred payment options.
  3. Integration: Seamlessly integrate with third-party payment gateways not natively supported by Magento.

Step-by-Step Guide to Creating an Online Payment Method:

Step 1: Set Up Your Module

First, create the necessary directories for your custom module:

app/code/Vendor/Module

Next, create the registration.php file:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

Then, create the module.xml file:

<?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"/>
</config>

Step 2: Define Payment Method Configuration

Create the etc/config.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/config.xsd">
    <default>
        <payment>
            <vendor_module>
                <active>1</active>
                <title>Custom Online Payment</title>
                <model>Vendor\Module\Model\Payment</model>
                <order_status>pending</order_status>
                <is_gateway>1</is_gateway>
                <allowspecific>0</allowspecific>
                <group>online</group>
                <can_authorize>1</can_authorize>
                <can_capture>1</can_capture>
                <can_refund>1</can_refund>
            </vendor_module>
        </payment>
    </default>
</config>

Step 3: Create Payment Model

Create the Model/Payment.php file:

<?php
namespace Vendor\Module\Model;

use Magento\Payment\Model\Method\AbstractMethod;

class Payment extends AbstractMethod
{
    protected $_code = 'vendor_module';

    public function isAvailable(
        \Magento\Quote\Api\Data\CartInterface $quote = null
    ) {
        return parent::isAvailable($quote);
    }

    public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
    {
        // Custom authorization logic
        return $this;
    }

    public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
    {
        // Custom capture logic
        return $this;
    }

    public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount)
    {
        // Custom refund logic
        return $this;
    }
}

Step 4: Add Admin Configuration

Create the etc/adminhtml/system.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/system_file.xsd">
    <system>
        <section id="payment">
            <group id="vendor_module" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Custom Online Payment</label>
                <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Title</label>
                </field>
                <field id="order_status" translate="label" type="select" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>New Order Status</label>
                    <source_model>Magento\Sales\Model\Config\Source\Order\Status\NewStatus</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

Step 5: Implement Payment Method Logic

Depending on your specific requirements, you may need to implement additional logic for processing payments, handling callbacks, or integrating with third-party APIs. This typically involves extending the payment model and adding custom controllers for handling requests.

Step 6: Configure and Test

Enable the custom payment method in the Magento admin panel:

Stores > Configuration > Sales > Payment Methods > Custom Online Payment

Test the payment method thoroughly to ensure it works as expected in various scenarios, including order placement, payment processing, and refunds.

Conclusion: Creating a custom online payment method in Magento 2 can significantly enhance your store’s flexibility and provide a better customer experience. By following this step-by-step guide, you can develop a tailored payment solution that meets your specific business needs. Remember to adhere to Magento’s best practices and thoroughly test your implementation to ensure a smooth and secure payment process for your customers.

Updated: