https://github.com/mageplaza/magento-2-sample-payment-method
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.
https://github.com/mageplaza/magento-2-sample-payment-method
gateway payment-methods
Last synced: 8 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/mageplaza/magento-2-sample-payment-method
- Owner: mageplaza
- Created: 2016-11-17T10:19:26.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-07-14T15:10:13.000Z (almost 8 years ago)
- Last Synced: 2025-04-19T08:35:22.481Z (about 1 year ago)
- Topics: gateway, payment-methods
- Language: PHP
- Homepage: https://www.mageplaza.com/magento-2-create-payment-method/
- Size: 29.3 KB
- Stars: 63
- Watchers: 9
- Forks: 34
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Magento 2 sample payment method gateway
[**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.
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](https://github.com/mageplaza/magento-2-sample-payment-method/blob/master/registration.php)
``` php
```
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](https://github.com/mageplaza/magento-2-sample-payment-method/blob/master/etc/payment.xml)
``` xml
Offline Payment Methods
1
```
2. Create `config.xml` file in `etc` folder.
``` xml
1
Mageplaza\Payment\Model\Payment\Simple
pending
Simple
0
Offline
```
In this file, we declare Model of this payment method, we calle `Simple` model.
3. Create Simple model file
Create file [Model/Payment/Simple.php](https://github.com/mageplaza/magento-2-sample-payment-method/blob/master/Model/Payment/Simple.php)
``` php
_countryFactory = $countryFactory;
$this->_minAmount = $this->getConfigData('min_order_total');
$this->_maxAmount = $this->getConfigData('max_order_total');
}
/**
* Authorize payment abstract method
*
* @param \Magento\Framework\DataObject|InfoInterface $payment
* @param float $amount
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
* @api
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
{
if (!$this->canAuthorize()) {
throw new \Magento\Framework\Exception\LocalizedException(__('The authorize action is not available.'));
}
return $this;
}
/**
* Capture payment abstract method
*
* @param \Magento\Framework\DataObject|InfoInterface $payment
* @param float $amount
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
* @api
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
{
if (!$this->canCapture()) {
throw new \Magento\Framework\Exception\LocalizedException(__('The capture action is not available.'));
}
return $this;
}
/**
* Refund specified amount for payment
*
* @param \Magento\Framework\DataObject|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](https://github.com/mageplaza/magento-2-sample-payment-method/blob/master/view/frontend/layout/checkout_index_index.xml)
``` xml
Magento_OfflinePayments/js/view/payment/offline-payments
true
```
2. Create js files to load KO template in checkout page
- 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)
``` js
define(
[
'uiComponent',
'Magento_Checkout/js/model/payment/renderer-list'
],
function (Component,
rendererList) {
'use strict';
rendererList.push(
{
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`
``` js
define(
[
'Magento_Checkout/js/view/payment/default'
],
function (Component) {
'use strict';
return Component.extend({
defaults: {
template: 'Mageplaza_Payment/payment/simple'
},
getMailingAddress: function () {
return window.checkoutConfig.payment.checkmo.mailingAddress;
},
});
}
);
```
3. 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)
``` php
```
You 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.