為了保證兼容性,可升級和易維護,不要修改 M2 默認代碼,要在獨立的模塊中添加自定義部分的代碼。
你可以使用 Magento_SamplePaymentGateway 模塊樣板代碼](https://github.com/magento/magento2-samples/tree/master/sample-module-payment-gateway) 這部分的代碼,在此之上自定義模塊結構和對應的文件。
## 指明模塊依賴
自定義支付方式對應的模塊必須包含如下依賴:
- Magento_Sales 模塊:用來獲取訂單詳情
- Magento_Payment 模塊,用來使用 M2 提供的支付網關基礎設施
- Magento_Checkout 模塊,用來將新的支付方式添加到 checkout, 如果你不打算在網站前臺的 checkout 使用對應的支付方式,這個依賴不是必須的
- 在 `composer.json` 和 `module.xml` 文件中指明對應的依賴項
### composer.json
在你的 `%Vendor_Module%/composer.json` 文件中,按照如下示例申明依賴:
```php
{
...
"require": {
...
"magento/module-payment": "100.1.*",
"magento/module-checkout": "100.1.*",
"magento/module-sales": "100.1.*",
...
},
...
}
```
關于 composer.json 的細節,參考[這里](https://devdocs.magento.com/guides/v2.4/extension-dev-guide/build/composer-integration.html)
### module.xml
在 `%Vendor_Module%/etc/module.xml` 中按照如下示例添加同樣的依賴項:
```php
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="2.0.0">
<sequence>
...
<module name="Magento_Sales"/>
<module name="Magento_Payment"/>
<module name="Magento_Checkout"/>
...
</sequence>
</module>
</config>
```
> 備注:**你的支付方式可能有更多的依賴項,請根據實際情況添加**
- 支付流程梳理
- Magento 支付網關
- payment-gateway-structure
- 網關命令 (Gateway Command)
- 網關命令池(Gateway command pool)
- 請求構造器
- 網關客戶端(Gateway client)
- Response Validator
- Response Handler
- Error Code Mapping
- 集成新的支付方式
- 支付方式模塊配置
- 支付方式配置
- payment method facade
- Admin checkout 支付信息渲染
- 新增 payment action
- 將支付信息從前端傳遞到后端
- 根據 area 來配置 payment method
- 支付中的專有名字解釋