How to Create Custom Payment Method using Adapter Class Magento 2?

Create a custom offline payment gateway in Magento 2 without the use of AbstractModel.

Using class Magento\Payment\Model\Method\Adapter with virtualType to make it compatible with the latest Magento standard practice to create a payment method.

The offline Payment method with the configuration setting from the admin panel, you can enable/disable payment method with the Payment method title. We will create a simple Purchase Order Payment method.

You can see the new payment method from the admin panel,
Stores > Settings > Configuration > Sales > Payment Methods.

The module name is, Jesadiya_PurchaseOrderPayment
We start with the registration.php file of the module,

Create a module.xml file,

Path: app/code/Jesadiya/PurchaseOrderPayment/etc/module.xml

Create system.xml file to Display settings on Payment methods tab in the Configuration page,
Path: app/code/Jesadiya/PurchaseOrderPayment/etc/adminhtml/system.xml

All the node is understandable like Title used to display title name on checkout page.

sort_order used for the payment method on billing step position.

order_status will be pending for the Offline payment method.

allowspecific used to display payment for all the country or select a specific country.

Create a config.xml file to the default value set inside the Payment Configuration step in Admin Panel.

Offline Payment Method using Magento Standard Way Adapter Class

Path: app/code/Jesadiya/PurchaseOrderPayment/etc/config.xml

A model tag used to display the Payment facade that will be used to create virtualType in di.xml file.

can_use_internal – Value 1 used to display only in the admin panel.

can_use_checkout – Value 1 used to display the payment method in the front end checkout page.

is_offline node will notify its offline payment method. you don’t require to call third party payment API to capture/settle transactions.

Main File is the di.xml to declare all the configuration and payment method facade without the use of Deprecated abstractMethod.

Set a relation using the di.xml file

Path: app/code/Jesadiya/PurchaseOrderPayment/etc/di.xml

We have to create PurchaseOrderPaymentFacade for the adapter class which we have defined as a model node in config.xml.

A discussion of the PurchaseOrderPaymentFacade,

code argument is the path of class which contains the payment method code.

formBlockType argument you can use your custom class to made admin panel level changes for the payment method in the admin sales order view page.

infoBlockType argument you can use a custom class to make changes on the frontend in order detail page.

valueHandlerPool argument used to pool of value handlers used for queries to configuration.

Create a Model class to define our payment method code,
Path: app/code/Jesadiya/PurchaseOrderPayment/Model/Ui/ConfigProvider.php

Define your payment method code, I have used “custompurchaseorder” as the payment code.

Now Declare the payment method in the layout using checkout_index_index.xml,

Path: app/code/Jesadiya/PurchaseOrderPayment/view/frontend/layout/checkout_index_index.xml

Create a .js component which we have declared in the layout file that registers the renderer in the billing step.

Path: app/code/Jesadiya/PurchaseOrderPayment/view/frontend/web/js/view/payment/custom-payments.js

Create a js component to define a template file that contains our payment method html form,

Path: app/code/Jesadiya/PurchaseOrderPayment/view/frontend/web/js/view/payment/method-renderer/purchaseorder-method.js

Create a form template for the payment method component,

Path: app/code/Jesadiya/PurchaseOrderPayment/view/frontend/web/template/payment/purchaseorder-form.html

The form template will be rendered on the billing step of checkout. You can modify the above template as per your requirement.

Now Run the Command using CLI from Magento root folder,

Install the code and Clean the cache using CLI,

The output will look like in checkout page,

Custom Payment Method
Custom Payment Method Using Adapter