为了保证兼容性,可升级和易维护,不要修改 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> ``` > 备注:**你的支付方式可能有更多的依赖项,请根据实际情况添加**