当前位置: 技术文章>> 一篇文章详细介绍如何为 Magento 2 添加自定义支付网关?

文章标题:一篇文章详细介绍如何为 Magento 2 添加自定义支付网关?
  • 文章分类: 后端
  • 7438 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


在Magento 2中添加自定义支付网关涉及多个步骤,包括模块创建、配置、接口实现以及后端集成。下面将详细介绍整个流程。

步骤 1: 创建模块结构

首先,你需要在Magento 2的app/code目录下创建一个新的模块目录。假设我们的模块名为CustomPaymentGateway,那么目录结构如下:

app/code/CustomPaymentGateway/
└── Payment
    ├── registration.php
    ├── composer.json
    ├── etc
    │   ├── module.xml
    │   ├── config.xml
    │   └── adminhtml
    │       └── system.xml
    ├── Model
    │   └── PaymentMethod.php
    ├── Controller
    │   └── ...
    └── view
        └── frontend
            └── layout
                └── checkout_index_index.xml

1.1 创建 registration.php

app/code/CustomPaymentGateway/Payment目录下创建registration.php文件,用于注册模块:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'CustomPaymentGateway_Payment',
    __DIR__
);

1.2 创建 composer.json

在模块根目录下创建composer.json文件,描述模块的基本信息:

{
    "name": "custompaymentgateway/module-payment",
    "description": "Custom Payment Gateway for Magento 2",
    "type": "magento2-module",
    "version": "1.0.0",
    "license": ["OSL-3.0", "AFL-3.0"],
    "require": {
        "php": "~7.0.0|~7.1.0|~7.2.0|~7.3.0|~7.4.0",
        "magento/module-sales": "100.0.*",
        "magento/module-checkout": "100.0.*",
        "magento/module-payment": "100.0.*",
        "magento/framework": "100.0.*"
    },
    "autoload": {
        "files": ["registration.php"],
        "psr-4": {
            "CustomPaymentGateway\\Payment\\": ""
        }
    }
}

步骤 2: 配置模块

2.1 创建 module.xml

etc目录下创建module.xml文件,声明模块依赖:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="CustomPaymentGateway_Payment" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Payment"/>
            <module name="Magento_Checkout"/>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>

2.2 创建 config.xml

etc目录下创建config.xml文件,定义支付方法的默认配置:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <payment>
            <custompaymentgateway_payment>
                <active>1</active>
                <model>CustomPaymentGateway\Payment\Model\PaymentMethod</model>
                <title>Custom Payment Gateway</title>
                <payment_action>authorize_capture</payment_action>
                <order_status>pending_payment</order_status>
                <!-- 其他配置 -->
            </custompaymentgateway_payment>
        </payment>
    </default>
</config>

步骤 3: 实现支付接口

Model目录下创建PaymentMethod.php文件,实现Magento\Payment\Model\MethodInterface接口:

<?php
namespace CustomPaymentGateway\Payment\Model;

use Magento\Payment\Model\MethodInterface;
推荐文章