登录  /  注册
博主信息
博文 5
粉丝 0
评论 0
访问量 50
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
PHP 支付系统扩展实战:从微信 / 支付宝到银联的多驱动架构设计
い独霸天下う
原创
13人浏览过

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

支付配置

  1. return [
  2. //默认支付模式
  3. 'default' => 'wechat_pay',
  4. //支付方式
  5. 'payType' => ['weixin' => '微信支付', 'yue' => '余额支付', 'offline' => '线下支付'],
  6. //提现方式
  7. 'extractType' => ['alipay', 'bank', 'weixin'],
  8. //配送方式
  9. 'deliveryType' => ['send' => '商家配送', 'express' => '快递配送'],
  10. //驱动模式
  11. 'stores' => [
  12. //微信支付
  13. 'wechat_pay' => [],
  14. //支付宝支付
  15. 'ali_pay' => []
  16. ]
  17. ];

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

配置参数接收

  1. namespace crmeb\services\pay\storage;
  2. use crmeb\services\pay\BasePay;
  3. use crmeb\services\pay\PayInterface;
  4. class WechatPay extends BasePay implements PayInterface
  5. {
  6. protected function initialize(array $config)
  7. {
  8. //$config取值config('pay.stores.wechat_pay');
  9. }
  10. }

扩展入口

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

  1. namespace crmeb\services\pay;
  2. use crmeb\basic\BaseManager;
  3. use crmeb\services\pay\storage\AliPay;
  4. use crmeb\services\pay\storage\WechatPay;
  5. use think\facade\Config;
  6. /**
  7. * 第三方支付
  8. * Class Pay
  9. * @package crmeb\services\pay
  10. * @mixin AliPay
  11. * @mixin WechatPay
  12. */
  13. class Pay extends BaseManager
  14. {
  15. /**
  16. * 空间名
  17. * @var string
  18. */
  19. protected $namespace = '\crmeb\services\pay\storage\';
  20. /**
  21. * 默认驱动
  22. * @return mixed
  23. */
  24. protected function getDefaultDriver()
  25. {
  26. return Config::get('pay.default', 'wechat_pay');
  27. }
  28. }

使用方法

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

  1. $pay = new Pay();
  2. //创建订单支付
  3. $pay->create(string $orderId, string $totalFee, string $attach, string $body, string $detail, array $options = []);
  4. //支付到零钱
  5. $pay->merchantPay(string $openid, string $orderId, string $amount, array $options = []);
  6. //退款
  7. $pay->refund(string $outTradeNo, string $totalAmount, string $refund_id, array $options = []);
  8. //查询退款
  9. $pay->queryRefund(string $outTradeNo, string $outRequestNo, array $other = []);
  10. //订单异步回调
  11. $pay->handleNotify();

扩展支付插件

首先增加支付类型配置

修改config/pay.php

  1. return [
  2. //驱动模式
  3. 'stores' => [
  4. //微信支付
  5. 'wechat_pay' => [],
  6. //支付宝支付
  7. 'ali_pay' => [],
  8. //银联支付
  9. 'union_pay '=>[],
  10. ]
  11. ];

举例:增加银联支付扩展

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

需要完成以下接口

  1. namespace crmeb\services\pay\storage;
  2. use crmeb\services\pay\BasePay;
  3. use crmeb\services\pay\PayInterface;
  4. class UnionPay extends BasePay implements PayInterface
  5. {
  6. protected function initialize(array $config)
  7. {
  8. //$config取值config('pay.stores.wechat_pay');
  9. }
  10. //创建订单支付
  11. public function create(string $orderId, string $totalFee, string $attach, string $body, string $detail, array $options = [])
  12. {
  13. }
  14. //支付到零钱
  15. public function merchantPay(string $openid, string $orderId, string $amount, array $options = [])
  16. {
  17. }
  18. //退款
  19. public function refund(string $outTradeNo, string $totalAmount, string $refund_id, array $options = [])
  20. {
  21. }
  22. //查询退款订单
  23. public function queryRefund(string $outTradeNo, string $outRequestNo, array $other = [])
  24. {
  25. }
  26. //异步回调
  27. public static function handleNotify()
  28. {
  29. }
  30. }
银联扩展使用方法

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

  1. use crmeb\services\pay;
  2. //实例化银联支付扩展
  3. $pay = new Pay('union_pay');
  4. //给银联支付扩展传参
  5. //$pay = new Pay('union_pay',[
  6. // 'appid'=>'998845566',
  7. // 'key'=>'123456'
  8. //]);
  9. //接受参数
  10. //class UnionPay extends BasePay implements PayInterface
  11. //{
  12. //
  13. // protected function initialize(array $config)
  14. // {
  15. // //接受银联支付传的appid和key参数
  16. // }
  17. //}
  18. //进行创建订单发起支付
  19. $pay->create(string $orderId, string $totalFee, string $attach, string $body, string $detail, array $options = []);

附件:https://gitee.com/ZhongBangKeJi/CRMEB

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学