PHP 支付系统扩展实战:从微信 / 支付宝到银联的多驱动架构设计

目前已拥有支付宝支付、微信支付、微信支付v3、通联支付

支付配置

dart 复制代码
return [
    //默认支付模式
    'default' => 'wechat_pay',
    //支付方式
    'payType' => ['weixin' => '微信支付', 'yue' => '余额支付', 'offline' => '线下支付'],
    //提现方式
    'extractType' => ['alipay', 'bank', 'weixin'],
    //配送方式
    'deliveryType' => ['send' => '商家配送', 'express' => '快递配送'],
    //驱动模式
    'stores' => [
        //微信支付
        'wechat_pay' => [],
        //支付宝支付
        'ali_pay' => []
    ]
];

驱动里面可以配置支付的额外参数

配置参数接收

scala 复制代码
namespace crmeb\services\pay\storage;

use crmeb\services\pay\BasePay;
use crmeb\services\pay\PayInterface;

class WechatPay extends BasePay implements PayInterface
{

    protected function initialize(array $config)
    {
        //$config取值config('pay.stores.wechat_pay');
    }
}

扩展入口

扩展需要继承BaseManager,设置空间名,设置默认扩展

scala 复制代码
namespace crmeb\services\pay;


use crmeb\basic\BaseManager;
use crmeb\services\pay\storage\AliPay;
use crmeb\services\pay\storage\WechatPay;
use think\facade\Config;

/**
 * 第三方支付
 * Class Pay
 * @package crmeb\services\pay
 * @mixin AliPay
 * @mixin WechatPay
 */
class Pay extends BaseManager
{
    /**
     * 空间名
     * @var string
     */
    protected $namespace = '\crmeb\services\pay\storage\';

    /**
     * 默认驱动
     * @return mixed
     */
    protected function getDefaultDriver()
    {
        return Config::get('pay.default', 'wechat_pay');
    }

}

使用方法

默认支付为微信支付,如果需要切换其他支付传入第一个参数支付方式,目前支付方式有:wechat_pay、ali_pay两种支付方式。

php 复制代码
$pay = new Pay();

//创建订单支付
$pay->create(string $orderId, string $totalFee, string $attach, string $body, string $detail, array $options = []);

//支付到零钱
$pay->merchantPay(string $openid, string $orderId, string $amount, array $options = []);

//退款
$pay->refund(string $outTradeNo, string $totalAmount, string $refund_id, array $options = []);

//查询退款
$pay->queryRefund(string $outTradeNo, string $outRequestNo, array $other = []);

//订单异步回调
$pay->handleNotify();

扩展支付插件

首先增加支付类型配置

修改config/pay.php

dart 复制代码
return [
    //驱动模式
    'stores' => [
        //微信支付
        'wechat_pay' => [],
        //支付宝支付
        'ali_pay' => [],
        //银联支付
        'union_pay '=>[],
    ]
];

举例:增加银联支付扩展

创建文件:/crmeb/services/pay/storage/UnionPay.php

需要完成以下接口

php 复制代码
namespace crmeb\services\pay\storage;

use crmeb\services\pay\BasePay;
use crmeb\services\pay\PayInterface;

class UnionPay extends BasePay implements PayInterface
{

    protected function initialize(array $config)
    {
        //$config取值config('pay.stores.wechat_pay');
    }

    //创建订单支付
    public function create(string $orderId, string $totalFee, string $attach, string $body, string $detail, array $options = [])
    {

    }

    //支付到零钱
    public function merchantPay(string $openid, string $orderId, string $amount, array $options = [])
    {

    }

    //退款
    public function refund(string $outTradeNo, string $totalAmount, string $refund_id, array $options = [])
    {

    }

    //查询退款订单
    public function queryRefund(string $outTradeNo, string $outRequestNo, array $other = [])
    {

    }

    //异步回调
    public static function handleNotify()
    {

    }
}
银联扩展使用方法

增加银联扩展后,需要前台支付的时候传入pay_type=union_pay,就会自动调用银联扩展

php 复制代码
use crmeb\services\pay;

//实例化银联支付扩展
$pay = new Pay('union_pay');

//给银联支付扩展传参
//$pay = new Pay('union_pay',[
//    'appid'=>'998845566',
//    'key'=>'123456'
//]);
//接受参数
//class UnionPay extends BasePay implements PayInterface
//{
//
//    protected function initialize(array $config)
//    {
//        //接受银联支付传的appid和key参数
//    }
//}

//进行创建订单发起支付
$pay->create(string $orderId, string $totalFee, string $attach, string $body, string $detail, array $options = []);

附件:gitee.com/ZhongBangKe...

相关推荐
序安InToo3 分钟前
第6课|注释与代码风格
后端·操作系统·嵌入式
xyy1233 分钟前
C#: Newtonsoft.Json 到 System.Text.Json 迁移避坑指南
后端
洋洋技术笔记5 分钟前
Spring Boot Web MVC配置详解
spring boot·后端
JxWang056 分钟前
VS Code 配置 Markdown 环境
后端
navms9 分钟前
搞懂线程池,先把 Worker 机制啃明白
后端
JxWang059 分钟前
离线数仓的优化及重构
后端
Nyarlathotep011310 分钟前
gin01:初探gin的启动
后端·go
JxWang0511 分钟前
安卓手机配置通用多屏协同及自动化脚本
后端
JxWang0512 分钟前
Windows Terminal 配置 oh-my-posh
后端
SimonKing28 分钟前
OpenCode AI编程助手如何添加Skills,优化项目!
java·后端·程序员