{"id":19111399,"url":"https://github.com/mageplaza/magento-2-sample-payment-method","last_synced_at":"2025-10-14T18:32:19.409Z","repository":{"id":69522453,"uuid":"74015597","full_name":"mageplaza/magento-2-sample-payment-method","owner":"mageplaza","description":"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.","archived":false,"fork":false,"pushed_at":"2018-07-14T15:10:13.000Z","size":30,"stargazers_count":63,"open_issues_count":4,"forks_count":34,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-19T08:35:22.481Z","etag":null,"topics":["gateway","payment-methods"],"latest_commit_sha":null,"homepage":"https://www.mageplaza.com/magento-2-create-payment-method/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mageplaza.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-11-17T10:19:26.000Z","updated_at":"2024-10-20T20:39:53.000Z","dependencies_parsed_at":"2023-02-26T21:31:29.787Z","dependency_job_id":null,"html_url":"https://github.com/mageplaza/magento-2-sample-payment-method","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mageplaza%2Fmagento-2-sample-payment-method","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mageplaza%2Fmagento-2-sample-payment-method/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mageplaza%2Fmagento-2-sample-payment-method/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mageplaza%2Fmagento-2-sample-payment-method/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mageplaza","download_url":"https://codeload.github.com/mageplaza/magento-2-sample-payment-method/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251782774,"owners_count":21642987,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gateway","payment-methods"],"created_at":"2024-11-09T04:28:23.264Z","updated_at":"2025-10-14T18:32:14.369Z","avatar_url":"https://github.com/mageplaza.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Magento 2 sample payment method gateway\n\n\n[**Magento 2 Create Payment Method**](https://www.mageplaza.com/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.\n\nIn 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 \u003e Stores \u003e Settings \u003e Configuration \u003e Sales \u003e Payment Methods`. There, admin possibly assigns a payment method to specific shipping method, this means they will work in pairs when enabling.\n\n\n## Step 1: Create payment method module\n\n1. Create file [registration.php](https://github.com/mageplaza/magento-2-sample-payment-method/blob/master/registration.php)\n\n``` php\n\u003c?php\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    'Mageplaza_Payment',\n    __DIR__\n);\n```\n\n2. Declare `module.xml` file\n\n``` xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cconfig xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module/etc/module.xsd\"\u003e\n    \u003cmodule name=\"Mageplaza_Payment\" setup_version=\"0.1.0\"\u003e\n        \u003csequence\u003e\n            \u003cmodule name=\"Magento_Sales\"/\u003e\n            \u003cmodule name=\"Magento_Payment\"/\u003e\n            \u003cmodule name=\"Magento_Checkout\"/\u003e\n            \u003cmodule name=\"Magento_Directory\" /\u003e\n            \u003cmodule name=\"Magento_Config\" /\u003e\n        \u003c/sequence\u003e\n    \u003c/module\u003e\n\u003c/config\u003e\n```\n\nThis 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.\n\n## Step 2: Declare payment method module\n\n1. Now we create file `payment.xml` file in `etc` folder [etc/payment.xml](https://github.com/mageplaza/magento-2-sample-payment-method/blob/master/etc/payment.xml)\n\n``` xml\n\u003c?xml version=\"1.0\" ?\u003e\n\u003cpayment xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n         xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Payment:etc/payment.xsd\"\u003e\n    \u003cgroups\u003e\n        \u003cgroup id=\"offline\"\u003e\n            \u003clabel\u003eOffline Payment Methods\u003c/label\u003e\n        \u003c/group\u003e\n    \u003c/groups\u003e\n    \u003cmethods\u003e\n        \u003cmethod name=\"simple\"\u003e\n            \u003callow_multiple_address\u003e1\u003c/allow_multiple_address\u003e\n        \u003c/method\u003e\n    \u003c/methods\u003e\n\u003c/payment\u003e\n```\n\n2. Create `config.xml` file in `etc` folder.\n\n``` xml\n\u003c?xml version=\"1.0\" ?\u003e\n\u003cconfig xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Store:etc/config.xsd\"\u003e\n    \u003cdefault\u003e\n        \u003cpayment\u003e\n            \u003csimple\u003e\n                \u003cactive\u003e1\u003c/active\u003e\n                \u003cmodel\u003eMageplaza\\Payment\\Model\\Payment\\Simple\u003c/model\u003e\n                \u003corder_status\u003epending\u003c/order_status\u003e\n                \u003ctitle\u003eSimple\u003c/title\u003e\n                \u003callowspecific\u003e0\u003c/allowspecific\u003e\n                \u003cgroup\u003eOffline\u003c/group\u003e\n            \u003c/simple\u003e\n        \u003c/payment\u003e\n    \u003c/default\u003e\n\u003c/config\u003e\n```\n\nIn this file, we declare Model of this payment method, we calle `Simple` model.\n\n3. Create Simple model file\n\nCreate file [Model/Payment/Simple.php](https://github.com/mageplaza/magento-2-sample-payment-method/blob/master/Model/Payment/Simple.php)\n\n``` php\n\u003c?php\nnamespace Mageplaza\\Payment\\Model\\Payment;\nclass Simple extends \\Magento\\Payment\\Model\\Method\\Cc\n{\n    protected $_isGateway = true;\n    protected $_canCapture = true;\n    protected $_canCapturePartial = true;\n    protected $_canRefund = true;\n    protected $_canRefundInvoicePartial = true;\n    protected $_stripeApi = false;\n    protected $_countryFactory;\n    protected $_minAmount = null;\n    protected $_maxAmount = null;\n    protected $_supportedCurrencyCodes = array('USD');\n    protected $_debugReplacePrivateDataKeys\n        = ['number', 'exp_month', 'exp_year', 'cvc'];\n    public function __construct(\\Magento\\Framework\\Model\\Context $context,\n        \\Magento\\Framework\\Registry $registry,\n        \\Magento\\Framework\\Api\\ExtensionAttributesFactory $extensionFactory,\n        \\Magento\\Framework\\Api\\AttributeValueFactory $customAttributeFactory,\n        \\Magento\\Payment\\Helper\\Data $paymentData,\n        \\Magento\\Framework\\App\\Config\\ScopeConfigInterface $scopeConfig,\n        \\Magento\\Payment\\Model\\Method\\Logger $logger,\n        \\Magento\\Framework\\Module\\ModuleListInterface $moduleList,\n        \\Magento\\Framework\\Stdlib\\DateTime\\TimezoneInterface $localeDate,\n        \\Magento\\Directory\\Model\\CountryFactory $countryFactory,\n        array $data = array()\n    ) {\n        parent::__construct(\n            $context, $registry, $extensionFactory, $customAttributeFactory,\n            $paymentData, $scopeConfig, $logger, $moduleList, $localeDate, null,\n            null, $data\n        );\n        $this-\u003e_countryFactory = $countryFactory;\n        $this-\u003e_minAmount = $this-\u003egetConfigData('min_order_total');\n        $this-\u003e_maxAmount = $this-\u003egetConfigData('max_order_total');\n    }\n    /**\n     * Authorize payment abstract method\n     *\n     * @param \\Magento\\Framework\\DataObject|InfoInterface $payment\n     * @param float $amount\n     * @return $this\n     * @throws \\Magento\\Framework\\Exception\\LocalizedException\n     * @api\n     * @SuppressWarnings(PHPMD.UnusedFormalParameter)\n     */\n    public function authorize(\\Magento\\Payment\\Model\\InfoInterface $payment, $amount)\n    {\n        if (!$this-\u003ecanAuthorize()) {\n            throw new \\Magento\\Framework\\Exception\\LocalizedException(__('The authorize action is not available.'));\n        }\n        return $this;\n    }\n    /**\n     * Capture payment abstract method\n     *\n     * @param \\Magento\\Framework\\DataObject|InfoInterface $payment\n     * @param float $amount\n     * @return $this\n     * @throws \\Magento\\Framework\\Exception\\LocalizedException\n     * @api\n     * @SuppressWarnings(PHPMD.UnusedFormalParameter)\n     */\n    public function capture(\\Magento\\Payment\\Model\\InfoInterface $payment, $amount)\n    {\n        if (!$this-\u003ecanCapture()) {\n            throw new \\Magento\\Framework\\Exception\\LocalizedException(__('The capture action is not available.'));\n        }\n        return $this;\n    }\n    /**\n     * Refund specified amount for payment\n     *\n     * @param \\Magento\\Framework\\DataObject|InfoInterface $payment\n     * @param float $amount\n     * @return $this\n     * @throws \\Magento\\Framework\\Exception\\LocalizedException\n     * @api\n     * @SuppressWarnings(PHPMD.UnusedFormalParameter)\n     */\n    public function refund(\\Magento\\Payment\\Model\\InfoInterface $payment, $amount)\n    {\n        if (!$this-\u003ecanRefund()) {\n            throw new \\Magento\\Framework\\Exception\\LocalizedException(__('The refund action is not available.'));\n        }\n        return $this;\n    }\n}\n```\n\nThis model includes basic functions such as \n- `authorize()`: Authorize the payment e.g: card\n- `capture()`: Capture money from a customer\n- `refund()`: Chargeback money to the customer.\n\n\n## Step 3: Display payment method in checkout page\n\nIn previous steps, we talked about declare module, config and model files. Now we need to display this Simple payment method in checkout page.\n\n1. Create layout file: [view/frontend/layout/checkout_index_index.xml](https://github.com/mageplaza/magento-2-sample-payment-method/blob/master/view/frontend/layout/checkout_index_index.xml)\n\n``` xml\n\u003c?xml version=\"1.0\" ?\u003e\n\u003cpagepage layout=\"1column\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n          xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View/Layout/etc/page_configuration.xsd\"\u003e\n    \u003cbody\u003e\n        \u003creferenceBlock name=\"checkout.root\"\u003e\n            \u003carguments\u003e\n                \u003cargument name=\"jsLayout\" xsi:type=\"array\"\u003e\n                    \u003citem name=\"components\" xsi:type=\"array\"\u003e\n                        \u003citem name=\"checkout\" xsi:type=\"array\"\u003e\n                            \u003citem name=\"children\" xsi:type=\"array\"\u003e\n                                \u003citem name=\"steps\" xsi:type=\"array\"\u003e\n                                    \u003citem name=\"children\" xsi:type=\"array\"\u003e\n                                        \u003citem name=\"billing-step\" xsi:type=\"array\"\u003e\n                                            \u003citem name=\"children\" xsi:type=\"array\"\u003e\n                                                \u003citem name=\"payment\" xsi:type=\"array\"\u003e\n                                                    \u003citem name=\"children\" xsi:type=\"array\"\u003e\n                                                        \u003citem name=\"renders\" xsi:type=\"array\"\u003e\n                                                            \u003citem name=\"children\" xsi:type=\"array\"\u003e\n                                                                \u003citem name=\"offline-payments\" xsi:type=\"array\"\u003e\n                                                                    \u003citem name=\"component\" xsi:type=\"string\"\u003eMagento_OfflinePayments/js/view/payment/offline-payments\u003c/item\u003e\n                                                                    \u003citem name=\"methods\" xsi:type=\"array\"\u003e\n                                                                        \u003citem name=\"simple\" xsi:type=\"array\"\u003e\n                                                                            \u003citem name=\"isBillingAddressRequired\" xsi:type=\"boolean\"\u003etrue\u003c/item\u003e\n                                                                        \u003c/item\u003e\n                                                                    \u003c/item\u003e\n                                                                \u003c/item\u003e\n                                                            \u003c/item\u003e\n                                                        \u003c/item\u003e\n                                                    \u003c/item\u003e\n                                                \u003c/item\u003e\n                                            \u003c/item\u003e\n                                        \u003c/item\u003e\n                                    \u003c/item\u003e\n                                \u003c/item\u003e\n                            \u003c/item\u003e\n                        \u003c/item\u003e\n                    \u003c/item\u003e\n                \u003c/argument\u003e\n            \u003c/arguments\u003e\n        \u003c/referenceBlock\u003e\n    \u003c/body\u003e\n\u003c/pagepage\u003e\n```\n\n\n2. Create js files to load KO template in checkout page\n\n- File: [/view/frontend/web/js/view/payment/simple.js](https://github.com/mageplaza/magento-2-sample-payment-method/blob/master/view/frontend/web/js/view/payment/simple.js)\n\n``` js\ndefine(\n    [\n        'uiComponent',\n        'Magento_Checkout/js/model/payment/renderer-list'\n    ],\n    function (Component,\n              rendererList) {\n        'use strict';\n        rendererList.push(\n            {\n                type: 'simple',\n                component: 'Mageplaza_Payment/js/view/payment/method-renderer/simple-method'\n            }\n        );\n        return Component.extend({});\n    }\n);\n```\n\nIn this file, it call another component: `js/view/payment/method-renderer/simple-method`\n\n\nNow we need to create `/view/frontend/web/js/view/payment/method-renderer/simple-method.js`\n\n``` js\ndefine(\n    [\n        'Magento_Checkout/js/view/payment/default'\n    ],\n    function (Component) {\n        'use strict';\n        return Component.extend({\n            defaults: {\n                template: 'Mageplaza_Payment/payment/simple'\n            },\n            getMailingAddress: function () {\n                return window.checkoutConfig.payment.checkmo.mailingAddress;\n            },\n        });\n    }\n);\n```\n\n\n3. Finally, create KO template file [/view/frontend/web/template/payment/simple.html](https://github.com/mageplaza/magento-2-sample-payment-method/blob/master/view/frontend/web/template/payment/simple.html)\n\n``` php\n\u003cdiv class=\"payment-method\" data-bind=\"css: {'_active': (getCode() == isChecked())}\"\u003e\n    \u003cdiv class=\"payment-method-title field choice\"\u003e\n        \u003cinput type=\"radio\"\n               name=\"payment[method]\"\n               class=\"radio\"\n               data-bind=\"attr: {'id': getCode()}, value: getCode(), checked: isChecked, click: selectPaymentMethod, visible: isRadioButtonVisible()\"/\u003e\n        \u003clabel data-bind=\"attr: {'for': getCode()}\" class=\"label\"\u003e\u003cspan data-bind=\"text: getTitle()\"\u003e\u003c/span\u003e\u003c/label\u003e\n    \u003c/div\u003e\n    \u003cdiv class=\"payment-method-content\"\u003e\n        \u003c!-- ko foreach: getRegion('messages') --\u003e\n        \u003c!-- ko template: getTemplate() --\u003e\u003c!-- /ko --\u003e\n        \u003c!--/ko--\u003e\n        \u003cdiv class=\"payment-method-billing-address\"\u003e\n            \u003c!-- ko foreach: $parent.getRegion(getBillingAddressFormName()) --\u003e\n            \u003c!-- ko template: getTemplate() --\u003e\u003c!-- /ko --\u003e\n            \u003c!--/ko--\u003e\n        \u003c/div\u003e\n        \u003cdiv class=\"checkout-agreements-block\"\u003e\n            \u003c!-- ko foreach: $parent.getRegion('before-place-order') --\u003e\n            \u003c!-- ko template: getTemplate() --\u003e\u003c!-- /ko --\u003e\n            \u003c!--/ko--\u003e\n        \u003c/div\u003e\n        \u003cdiv class=\"actions-toolbar\"\u003e\n            \u003cdiv class=\"primary\"\u003e\n                \u003cbutton 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\u003e\n                    \u003cspan data-bind=\"i18n: 'Place Order'\"\u003e\u003c/span\u003e\n                \u003c/button\u003e\n            \u003c/div\u003e\n        \u003c/div\u003e\n    \u003c/div\u003e\n\u003c/div\u003e\n```\n\n\n\nYou maybe also interested in [Magento 2 Create Shipping Methods](https://www.mageplaza.com/magento-2-create-shipping-method/) to custom the shipping methods as expected.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmageplaza%2Fmagento-2-sample-payment-method","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmageplaza%2Fmagento-2-sample-payment-method","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmageplaza%2Fmagento-2-sample-payment-method/lists"}