Best Improved Navigation Extension

Monday, February 6, 2017

On 7:11 AM by Trọng Huỳnh Nguyễn   No comments
Magento 2 Create Payment Method proves that store admin has rights to generate as many payment methods as they need when your store is based on Magento 2 platform, an great era of ecommerce architecture. Depending on the customer’s requirement, you probably plug it in your list of the existing payment method. The additional payment methods surely bring the diversity of customer choice when they proceed to checkout on your site. On the other’s hands, multiple payment method is the great strategy to reach out the global marketplace.

In the tutorial, you will learn how to create own Payment Gateway integration in Magento 2 stores. After launching the new payment methods, you will find and configure it according the path Admin panel > Stores > Settings > Configuration > Sales > Payment Methods. There, admin possibly assigns a payment method to specific shipping method, this means they will work in pairs when enabling.

Step 1: Create payment method module

  1. Create file registration.php


  1. Declare module.xml file

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mageplaza_Payment" setup_version="0.1.0">
<module name="Magento_Sales"/>
<module name="Magento_Payment"/>
<module name="Magento_Checkout"/>
<module name="Magento_Directory" />
<module name="Magento_Config" />

This module have to run after the Magento_Sales, Magento_Payment, Magento_Checkout, Magento_Directory, and Magento_Config. So we add depends (sequence) them like above block of code.

Step 2: Declare payment method module

  1. Now we create file payment.xml file in etc folder etc/payment.xml

<?xml version="1.0" ?>
<payment xmlns:xsi=""
<group id="offline">
<label>Offline Payment Methods</label>
<method name="simple">

  1. Create config.xml file in etc folder.

<?xml version="1.0" ?>
<config xmlns:xsi=""

In this file, we declare Model of this payment method, we calle Simple model.
  1. Create Simple model file

Create file Model/Payment/Simple.php

namespace Mageplaza\Payment\Model\Payment;
class Simple extends \Magento\Payment\Model\Method\Cc
InfoInterface $payment
* @param float $amount
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
* @api
* @SuppressWarnings(PHPMD.UnusedFormalParameter)

public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount)

if (!$this->canRefund())
throw new \Magento\Framework\Exception\LocalizedException(__("The refund action is not available."));

return $this;

This model includes basic functions such as - authorize(): Authorize the payment e.g: card - capture(): Capture money from a customer - refund(): Chargeback money to the customer.

Step 3: Display payment method in checkout page

In previous steps, we talked about declare module, config and model files. Now we need to display this Simple payment method in checkout page.
  1. Create layout file: view/frontend/layout/checkout_index_index.xml

<?xml version="1.0" ?>
<pagepage layout="1column" xmlns:xsi=""
<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="offline-payments" xsi:type="array">
<item name="component" xsi:type="string">Magento_OfflinePayments/js/view/payment/offline-payments</item>
<item name="methods" xsi:type="array">
<item name="simple" xsi:type="array">
<item name="isBillingAddressRequired" xsi:type="boolean">true</item>

  1. Create js files to load KO template in checkout page

function (Component,
"use strict";

type: "simple",
component: "Mageplaza_Payment/js/view/payment/method-renderer/simple-method"

return Component.extend();


In this file, it call another component: js/view/payment/method-renderer/simple-method

Now we need to create /view/frontend/web/js/view/payment/method-renderer/simple-method.js

function (Component)
"use strict";
return Component.extend(
template: "Mageplaza_Payment/payment/simple"
getMailingAddress: function ()
return window.checkoutConfig.payment.checkmo.mailingAddress;


  1. Finally, create KO template file /view/frontend/web/template/payment/simple.html

<div class="payment-method" data-bind="css: "_active": (getCode() == isChecked())">
<div class="payment-method-title field choice">
<input type="radio"
data-bind="attr: "id": getCode(), value: getCode(), checked: isChecked, click: selectPaymentMethod, visible: isRadioButtonVisible()"/>
<label data-bind="attr: "for": getCode()" class="label"><span data-bind="text: getTitle()"></span></label>
<div class="payment-method-content">
<!-- ko foreach: getRegion("messages") -->
<!-- ko template: getTemplate() --><!-- /ko -->
<div class="payment-method-billing-address">
<!-- ko foreach: $parent.getRegion(getBillingAddressFormName()) -->
<!-- ko template: getTemplate() --><!-- /ko -->
<div class="checkout-agreements-block">
<!-- ko foreach: $parent.getRegion("before-place-order") -->
<!-- ko template: getTemplate() --><!-- /ko -->
<div class="actions-toolbar">
<div class="primary">
<button class="action primary checkout"
click: placeOrder,
attr: title: $t("Place Order"),
css: disabled: !isPlaceOrderActionAllowed(),
enable: (getCode() == isChecked())

<span data-bind="i18n: "Place Order""></span>

You maybe also interested in Magento 2 Create Shipping Methods to custom the shipping methods as expected.


Post a Comment