Magento2 Offline Payment Method

In this blog post, we will see how to create a custom offline payment method in Magento2.
Steps to create our custom offline payment method in Magento2:
1) We need to create a Namespace_ModuleName or VendorName_ModuleName like Magetec_Payment.
Location: app/code/Magetec/Payment
2) Next we need to create a registration xml file of our module
Location: app/code/Magetec/Payment/etc/module.xml

<?xml version = “1.0”?>
<module name=”Magetec_Payment” setup_version=”0.0.1″/>


3) Now we need to register our module in magento framework directory
Location: app/code/Magetec/Payment/Registration.php


4) Next we need to enable our module in magento environment by two alternative ways
a) Go to “app/etc/config.php” and edit file to add ‘Magetec_Payment’ => 1
a) Go to command shell and run command “php bin/magento module:enable Magetec_Payment” (this will enable our module)
b) Go to command shell and run command “php bin/magento setup:upgrade” (this will register / upgrade our module version in database)
5) Now we will define configuration of our payment method
Location: app/code/Magetec/Payment/etc/config.xml

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../Store/etc/config.xsd">
                <title>Magetec Payment</title>
                <instructions>Add comments</instructions>

6) Next we will define the system configuration where admin can configure our custom payment method
Location: app/code/Magetec/Payment/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd">
        <section id="payment">
            <group id="magetecpay" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
                <field id="title" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="order_status" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>New Order Status</label>
                <field id="allowspecific" translate="label" type="allowspecific" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Payment from Applicable Countries</label>
                <field id="specificcountry" translate="label" type="multiselect" sortOrder="41" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Payment from Specific Countries</label>
                <field id="instructions" translate="label" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="sort_order" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Sort Order</label>

7) Now create our model file which we already defined in config.xml
Location: app/code/Magetec/Payment/Model/PaymentMethod.php

namespace Magetec\Payment\Model;
class PaymentMethod extends \Magento\Payment\Model\Method\AbstractMethod
     * Payment code
     * @var string
    protected $_code = 'magetecpay';
     * Availability option
     * @var bool
    protected $_isOffline = true;

8) Next we will create .js file which will be used to register our custom template
Location: app/code/Magetec/Payment/view/frontend/web/js/view/payment/magetecpay.js

    function (
    ) {
        'use strict';
                type: 'magetecpay',
                component: 'Magetec_Payment/js/view/payment/method-renderer/magetecpay-method'
        /** Add view logic here if needed */
        return Component.extend({});

9) Next we will add our .js renderer file for our custom template registered
Location: app/code/Magetec/Payment/view/frontend/web/js/view/payment/method-renderer/magetecpay-method.js

    function(Component) {
        'use strict';
        return Component.extend({
            defaults: {
                template: 'Magetec_Payment/payment/magetecpay'
            getMailingAddress: function() {
                return window.checkoutConfig.payment.checkmo.mailingAddress;

10) Next we will create our checkout payment template file
Location: app/code/Magetec/Payment/view/frontend/web/template/payment/magetecpay.html

11) Next we will create layout file to add our custom payment method to checkout page

Location: app/code/Magetec/Payment/view/frontend/layout/checkout_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
        <referenceBlock name="checkout.root">
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="checkout" xsi:type="array">
                            <item name="children" xsi:type="array">
                                <item name="steps" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="billing-step" xsi:type="array">
                                            <item name="children" xsi:type="array">
                                                <item name="payment" xsi:type="array">
                                                    <item name="children" xsi:type="array">
                                                        <item name="renders" xsi:type="array">
                                                            <item name="children" xsi:type="array">
                                                                <item name="payment-magetecpay" xsi:type="array">
                                                                    <item name="component" xsi:type="string">Magetec_Payment/js/view/payment/magetecpay</item>
                                                                    <item name="methods" xsi:type="array">                            <item name="magetecpay" xsi:type="array">
                                                                            <item name="isBillingAddressRequired" xsi:type="boolean">true</item>

12) Next we will define our order status source model of our custom payment method
Location: app/code/Magetec/Payment/Model/Config/Source/Order/Status/Pendingpayment.php

namespace Magetec\Payment\Model\Config\Source\Order\Status;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Config\Source\Order\Status;
 * Order status source model
class Pendingpayment extends Status

Now your module is ready!
Just run the following two commands from root and it will be a working module.
1) php bin/magento setup:upgrade
2) php bin/magento setup:static-content:deploy
That’s it! you have created your custom offline payment method in Magento2.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s