{"id":735,"date":"2019-12-26T11:42:10","date_gmt":"2019-12-26T11:42:10","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/12\/26\/create-payment-method-in-magento-2\/"},"modified":"2025-05-22T15:10:35","modified_gmt":"2025-05-22T09:40:35","slug":"create-payment-method-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-payment-method-in-magento-2\/","title":{"rendered":"DIY: Create Payment Method in Magento 2 Using Our Guide"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A payment gateway is a key to secure E-commerce business. Selecting a payment method that is suitable for your business type deserves proper attention. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A wrong choice is going to be a disaster for your payment system. A customer&#8217;s trust and loyalty depend on the payment processing system you offer. For example you want to restrict certain payment methods and make them only visible to admin you can create an payment method such as <a href=\"https:\/\/meetanshi.com\/blog\/make-a-payment-method-visible-only-to-admin-in-magento-2\/\">make a payment visible only to admin in Magento 2<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How a payment gateway should be?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>100% secure<\/li>\n\n\n\n<li>Compatible with your store platform<\/li>\n\n\n\n<li>Offers speedy payment process<\/li>\n\n\n\n<li>Fraud detection<\/li>\n\n\n\n<li>Invoicing capabilities<\/li>\n\n\n\n<li>Impressive UI<\/li>\n\n\n\n<li>Feasible costs<\/li>\n\n\n\n<li>Support for international payments<\/li>\n\n\n\n<li>Support policy<\/li>\n\n\n\n<li>Types of cards supported<\/li>\n\n\n\n<li>Support for recurring payments<\/li>\n\n\n\n<li>Hosted\/Non hosted payments<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If you are a Magento store owner, you need to filter out the points that must be fulfilled by the payment gateway you use for your business requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, an ideal payment gateway that fulfills all your requirements exists only in a parallel world. But wait, you can <em><strong>create payment method in Magento 2<\/strong><\/em> on your own! And this post shows you how to do it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Create Payment Method in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create <strong>registration.php<\/strong> file at <strong>app\/code\/Meetanshi\/CustomPayment\/registration.php<\/strong> and add the below code to it:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    'Meetanshi_CustomPayment',\n    __DIR__\n);<\/pre>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create <strong>module.xml<\/strong> file at <strong>app\/code\/Meetanshi\/CustomPayment\/etc\/module.xml<\/strong> and add below code to this file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Meetanshi_CustomPayment\" setup_version=\"1.0.0\">\n    &lt;\/module>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create <strong>config.xml<\/strong> file at <strong>app\/code\/Meetanshi\/CustomPayment\/etc\/config.xml<\/strong> and add below code to this file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"..\/..\/Store\/etc\/config.xsd\">\n    &lt;default>\n        &lt;payment>\n            &lt;custompayment>\n                &lt;payment_action>authorize&lt;\/payment_action> &lt;!-- You can use another method -->\n                &lt;model>Meetanshi\\CustomPayment\\Model\\PaymentMethod&lt;\/model>\n                &lt;active>1&lt;\/active>\n                &lt;title>Custom Payment&lt;\/title>\n                &lt;order_status>pending_payment&lt;\/order_status>&lt;!-- set default order status-->\n            &lt;\/custompayment>\n        &lt;\/payment>\n    &lt;\/default>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create <strong>system.xml<\/strong> file at <strong>app\/code\/Meetanshi\/CustomPayment\/etc\/adminhtml\/system.xml<\/strong> and add below code to this file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Config:etc\/system_file.xsd\">\n    &lt;system>\n        &lt;section id=\"payment\">\n                &lt;group id=\"custompayment\" translate=\"label\" sortOrder=\"100\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\">\n                    &lt;label>Custom Payment Method&lt;\/label>\n                    &lt;field id=\"active\" translate=\"label comment\" sortOrder=\"10\" type=\"select\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"0\">\n                        &lt;label>Enable&lt;\/label>\n                        &lt;source_model>Magento\\Config\\Model\\Config\\Source\\Yesno&lt;\/source_model>\n                    &lt;\/field>\n                    &lt;field id=\"title\" translate=\"label\" type=\"text\" sortOrder=\"20\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\">\n                        &lt;label>Custom Payment&lt;\/label>\n                    &lt;\/field>\n                &lt;\/group>\n        &lt;\/section>\n    &lt;\/system>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>5. Create PaymentMethod.php file at app\/code\/Meetanshi\/CustomPayment\/Model\/PaymentMethod.php&nbsp;add below code to this file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n \nnamespace Meetanshi\\CustomPayment\\Model;\n \n\/**\n * Pay In Store payment method model\n *\/\nclass PaymentMethod extends \\Magento\\Payment\\Model\\Method\\AbstractMethod\n{\n    \/**\n     * Payment code\n     *\n     * @var string\n     *\/\n    protected $_code = 'custompayment';\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>6. Create <strong>checkout_index_index.xml<\/strong> file at <strong>app\/code\/Meetanshi\/CustomPayment\/view\/frontend\/layout\/checkout_index_index.xml<\/strong> and add below code to this file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n    &lt;body>\n        &lt;referenceBlock name=\"checkout.root\">\n            &lt;arguments>\n                &lt;argument name=\"jsLayout\" xsi:type=\"array\">\n                    &lt;item name=\"components\" xsi:type=\"array\">\n                        &lt;item name=\"checkout\" xsi:type=\"array\">\n                            &lt;item name=\"children\" xsi:type=\"array\">\n                                &lt;item name=\"steps\" xsi:type=\"array\">\n                                    &lt;item name=\"children\" xsi:type=\"array\">\n                                        &lt;item name=\"billing-step\" xsi:type=\"array\">\n                                            &lt;item name=\"component\" xsi:type=\"string\">uiComponent&lt;\/item>\n                                            &lt;item name=\"children\" xsi:type=\"array\">\n                                                &lt;item name=\"payment\" xsi:type=\"array\">\n                                                    &lt;item name=\"children\" xsi:type=\"array\">\n                                                        &lt;item name=\"renders\" xsi:type=\"array\">\n                                                            &lt;!-- merge payment method renders here -->\n                                                            &lt;item name=\"children\" xsi:type=\"array\">\n                                                                &lt;item name=\"custompayment\" xsi:type=\"array\">\n                                                                    &lt;item name=\"component\" xsi:type=\"string\">Meetanshi_CustomPayment\/js\/view\/payment\/method-renderer&lt;\/item>\n                                                                    &lt;item name=\"methods\" xsi:type=\"array\">\n                                                                        &lt;item name=\"custompayment\" xsi:type=\"array\">\n                                                                            &lt;item name=\"isBillingAddressRequired\" xsi:type=\"boolean\">true&lt;\/item>\n                                                                        &lt;\/item>\n                                                                    &lt;\/item>\n                                                                &lt;\/item>\n                                                            &lt;\/item>\n                                                        &lt;\/item>\n                                                    &lt;\/item>\n                                                &lt;\/item>\n                                            &lt;\/item>\n                                        &lt;\/item>\n                                    &lt;\/item>\n                                &lt;\/item>\n                            &lt;\/item>\n                        &lt;\/item>\n                    &lt;\/item>\n                &lt;\/argument>\n            &lt;\/arguments>\n        &lt;\/referenceBlock>\n    &lt;\/body>\n&lt;\/page><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">7. Create a <strong>method-renderer.js<\/strong> file at <strong>app\/code\/Meetanshi\/CustomPayment\/view\/frontend\/web\/js\/view\/payment\/method-renderer.js<\/strong> and add below code to this file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">define(\n    [\n        'uiComponent',\n        'Magento_Checkout\/js\/model\/payment\/renderer-list'\n    ],\n    function (\n        Component,\n        rendererList\n    ) {\n        'use strict';\n        rendererList.push(\n            {\n                type: 'custompayment',\n                component: 'Meetanshi_CustomPayment\/js\/view\/payment\/method-renderer\/custompayment'\n            }\n        );\n        return Component.extend({});\n    }\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">8. Create <strong>custompayment.js<\/strong> file at <strong>app\/code\/Meetanshi\/CustomPayment\/view\/frontend\/web\/js\/view\/payment\/method-renderer\/custompayment.js<\/strong>&nbsp;add below code to this file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">define(\n    [\n        'Magento_Checkout\/js\/view\/payment\/default'\n    ],\n    function (Component) {\n        'use strict';\n \n        return Component.extend({\n            defaults: {\n                template: 'Meetanshi_CustomPayment\/payment\/customtemplate'\n            }\n        });\n    }\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">9. Create <strong>customtemplate.html<\/strong> file at <strong>app\/code\/Meetanshi\/CustomPayment\/view\/frontend\/web\/template\/payment\/customtemplate.html <\/strong>and&nbsp;add below code to this file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;div class=\"payment-method\" data-bind=\"css: {'_active': (getCode() == isChecked())}\">\n    &lt;div class=\"payment-method-title field choice\">\n        &lt;input type=\"radio\"\n               name=\"payment[method]\"\n               class=\"radio\"\n               data-bind=\"attr: {'id': getCode()}, value: getCode(), checked: isChecked, click: selectPaymentMethod, visible: isRadioButtonVisible()\"\/>\n        &lt;label data-bind=\"attr: {'for': getCode()}\" class=\"label\">&lt;span data-bind=\"text: getTitle()\">&lt;\/span>&lt;\/label>\n    &lt;\/div>\n    &lt;div class=\"payment-method-content\">\n        &lt;!-- ko foreach: getRegion('messages') -->\n        &lt;!-- ko template: getTemplate() -->&lt;!-- \/ko -->\n        &lt;!--\/ko-->\n        &lt;div class=\"payment-method-billing-address\">\n            &lt;!-- ko foreach: $parent.getRegion(getBillingAddressFormName()) -->\n            &lt;!-- ko template: getTemplate() -->&lt;!-- \/ko -->\n            &lt;!--\/ko-->\n        &lt;\/div>\n        &lt;div class=\"checkout-agreements-block\">\n            &lt;!-- ko foreach: $parent.getRegion('before-place-order') -->\n                &lt;!-- ko template: getTemplate() -->&lt;!-- \/ko -->\n            &lt;!--\/ko-->\n        &lt;\/div>\n        &lt;div class=\"actions-toolbar\">\n            &lt;div class=\"primary\">\n                &lt;button class=\"action primary checkout\"\n                        type=\"submit\"\n                        data-bind=\"\n                        click: placeOrder,\n                        attr: {title: $t('Place Order')},\n                        css: {disabled: !isPlaceOrderActionAllowed()},\n                        enable: (getCode() == isChecked())\n                        \"\n                        disabled>\n                    &lt;span data-bind=\"i18n: 'Place Order'\">&lt;\/span>\n                &lt;\/button>\n            &lt;\/div>\n        &lt;\/div>\n    &lt;\/div>\n&lt;\/div><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s it for creating custom payment methods in Magento 2 store. The extension will create backend settings&nbsp;to enable the payment method and set a title for it.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2019\/12\/custom-payment-method-configuration.png\" alt=\"custom payment method creation\" class=\"wp-image-7175\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Once, the payment method is enabled, it can be displayed in the frontend while checking out the products:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2019\/12\/custom-payment-method-in-frontend.png\" alt=\"custom payment method on checkout page in frontend\" class=\"wp-image-7177\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If you can find readymade solutions from <a href=\"https:\/\/meetanshi.com\/magento-2-payments-extensions.html\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> for your Magento 2 store, you would never need to visit this post again! Likewise you can <a href=\"https:\/\/meetanshi.com\/blog\/get-payment-related-additional-information-from-order-in-magento-2\/\">get payment related additional information from order in Magento 2<\/a> to help you extract and access the payment related details for specific products.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the post with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A payment gateway is a key to secure E-commerce business. Selecting a payment method that is suitable for your business type deserves proper attention. A&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-735","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=735"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/735\/revisions"}],"predecessor-version":[{"id":15137,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/735\/revisions\/15137"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}