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...

相关推荐
武大打工仔3 分钟前
从零开始手搓一个MVC框架
后端
开心猴爷8 分钟前
移动端网页调试实战 Cookie 丢失问题的排查与优化
后端
用户5724056148 分钟前
解析Json
后端
舒一笑9 分钟前
Mac 上安装并使用 frpc(FRP 内网穿透客户端)指南
后端·网络协议·程序员
每天学习一丢丢15 分钟前
Spring Boot + Vue 项目用宝塔面板部署指南
vue.js·spring boot·后端
邹小邹15 分钟前
Go 1.25 强势来袭:GC 速度飙升、并发测试神器上线,内存检测更精准!
后端·go
lichenyang45319 分钟前
管理项目服务器连接数据库
数据库·后端
生无谓21 分钟前
在Windows系统上安装多个JDK版本并切换
后端
唐叔在学习29 分钟前
万字长文深度解析HTTPS协议
后端·https
赵星星5201 小时前
透彻理解Java中的深拷贝与浅拷贝:从误区到最佳实践
java·后端