Как создать собственный способ оплаты в Magento2 :
Здесь я покажу, как создавать пользовательский способ оплаты и отобразить его на странице checkout в magento2.
Потребуется создать следующие файлы:
1 – Создать Test/Testpayment/registration.php для регистрации вашего модуля в системе.
|
1
2
3
4
5
6
|
<?php\Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Test_Testpayment', __DIR__); |
2- Создать Test/Testpayment/etc/module.xml для определения имени модуля.
|
1
2
3
4
5
|
<?xml version="1.0"?><config xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noTestSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Test_Testpayment" setup_version="2.0.0" active="true"> </module></config> |
3- Создать Test/Testpayment/etc/config.xml для определения способа оплаты.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?xml version="1.0"?><config xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Store/etc/config.xsd"> <default> <payment> <testpayment> <payment_action>authorize</payment_action><!-- You can use another methor like capture --> <model>Test\Testpayment\Model\PaymentMethod</model> <active>1</active> <title>Test Payment</title> <order_status>pending_payment</order_status><!-- set default order status--> </testpayment> </payment> </default></config> |
4- Создать Test/Testpayment/etc/adminhtml/system.xml для отображения способ оплаты в backend. В этом файле упоминается только одно поле для включения / выключения данного метода оплаты. Вы можете добавить еще своих полей которые вам потребуются для организации оплаты (логин и пароль в системе и т.д.).
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?xml version="1.0"?><config xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> <section id="payment"> <group id="testpayment" translate="label" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Test Payment</label> <field id="active" translate="label comment" sortOrder="1" type="select" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Enable</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> </field> </group> </section> </system></config> |
5- Создать файл модели для определения способа оплаты Test/Testpayment/Model/PaymentMethod.php
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?phpnamespace Test\Testpayment\Model;/** * Pay In Store payment method model */class PaymentMethod extends \Magento\Payment\Model\Method\AbstractMethod{ /** * Payment code * * @var string */ protected $_code = 'testpayment';} |
6- В этом файле Test/Testpayment/view/frontend/web/js/view/payment/method-renderer.js зарегистрируем наш шаблон или файл рендеринга.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
define( [ 'uiComponent', 'Magento_Checkout/js/model/payment/renderer-list' ], function ( Component, rendererList ) { 'use strict'; rendererList.push( { type: 'testpayment', component: 'Test_Testpayment/js/view/payment/method-renderer/testpayment' } ); return Component.extend({}); }); |
7- Создаем Test/Testpayment/view/frontend/web/js/view/payment/method-renderer/testpayment.js
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
define( [ 'Magento_Checkout/js/view/payment/default' ], function (Component) { 'use strict'; return Component.extend({ defaults: { template: 'Test_Testpayment/payment/testpayment' } }); }); |
8- Создаем файл шаблона Test/Testpayment/view/frontend/web/template/payment/testpayment.html
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<div class="payment-method" data-bind="css: {'_active': (getCode() == isChecked())}"> <div class="payment-method-title field choice"> <input type="radio" name="payment[method]" class="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> <div class="payment-method-content"> <div class="actions-toolbar"> <div class="primary"> <button class="action primary checkout" type="submit" data-bind=" click: placeOrder, attr: {title: $t('Place Order')}, css: {disabled: !isPlaceOrderActionAllowed()}, enable: (getCode() == isChecked()) " disabled> <span data-bind="i18n: 'Place Order'"></span> </button> </div> </div> </div></div> |
9- Создаем Test/Testpayment/view/frontend/layout/checkout_index_index.xml для определения способа оплаты на странице проверки.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<?xml version="1.0"?><page xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="checkout.root"> <arguments> <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="component" xsi:type="string">uiComponent</item> <item name="children" xsi:type="array"> <item name="payment" xsi:type="array"> <item name="children" xsi:type="array"> <item name="renders" xsi:type="array"> <!-- merge payment method renders here --> <item name="children" xsi:type="array"> <item name="testpayment" xsi:type="array"> <item name="component" xsi:type="string">Test_Testpayment/js/view/payment/method-renderer</item> <item name="methods" xsi:type="array"> <item name="testpayment" xsi:type="array"> <item name="isBillingAddressRequired" xsi:type="boolean">true</item> </item> </item> </item> </item> </item> </item> </item> </item> </item> </item> </item> </item> </item> </item> </argument> </arguments> </referenceBlock> </body></page> |
Теперь включите этот модуль с помощью консольной команды php bin / magento module:enable ‘name_module’ и пересоберите все модули с помощью php bin/magento setup:upgrade.
Теперь вы можете проверить свой способ оплаты на странице оплаты.
