WeChatPayService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop100%开源免费商用电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | Gitee下载:https://gitee.com/likeshop_gitee/likeshop
  10. // | 访问官网:https://www.likemarket.net
  11. // | 访问社区:https://home.likemarket.net
  12. // | 访问手册:http://doc.likemarket.net
  13. // | 微信公众号:好象科技
  14. // | 好象科技开发团队 版权所有 拥有最终解释权
  15. // +----------------------------------------------------------------------
  16. // | Author: LikeShopTeam
  17. // +----------------------------------------------------------------------
  18. namespace app\common\service\pay;
  19. use app\common\enum\PayEnum;
  20. use app\common\model\PayConfig;
  21. use app\common\service\pay\wechat\WechatPayV2Default;
  22. use app\common\service\pay\wechat\WechatPayV3Default;
  23. /**
  24. * 微信支付
  25. * Class WeChatPayService
  26. * @package app\common\server
  27. */
  28. class WeChatPayService extends BasePayService
  29. {
  30. /**
  31. * easyWeChat实例
  32. * @var
  33. */
  34. protected $pay;
  35. /**
  36. * 初始化微信配置
  37. * @param $terminal //用户终端
  38. * @param null $userId //用户id(获取授权openid)
  39. * @throws \Exception
  40. */
  41. public function __construct($terminal, $userId = null)
  42. {
  43. $config = PayConfig::where('pay_way', PayEnum::WECHAT_PAY)->findOrEmpty()->toArray();
  44. if (empty($config['id'])) {
  45. throw new \Exception('请配置好支付设置');
  46. }
  47. $initData = [
  48. 'terminal' => $terminal,
  49. 'config' => $config,
  50. 'pay_way' => PayEnum::WECHAT_PAY,
  51. 'user_id' => $userId
  52. ];
  53. switch ($config['config']['merchant_type'] . '-' . $config['config']['interface_version']) {
  54. // 普通商户 v2
  55. case 'ordinary_merchant-v2':
  56. $this->pay = new WechatPayV2Default($initData);
  57. break;
  58. // 普通商户 v3
  59. case 'ordinary_merchant-v3':
  60. // 服务商子商户 v3
  61. case 'partner_sub_merchant-v3':
  62. $this->pay = new WechatPayV3Default($initData);
  63. break;
  64. // 服务商 v3
  65. case 'partner_merchant-v3':
  66. throw new \Exception('不支持 服务商的支付方式');
  67. break;
  68. default:
  69. throw new \Exception('不支持的支付方式');
  70. }
  71. }
  72. function realPay()
  73. {
  74. return $this->pay;
  75. }
  76. function pay($form, $order)
  77. {
  78. return $this->pay->pay($form, $order);
  79. }
  80. /**
  81. * @notes 支付回调
  82. * @return \Symfony\Component\HttpFoundation\Response
  83. * @throws \EasyWeChat\Kernel\Exceptions\Exception
  84. * @author 段誉
  85. * @date 2021/8/13 14:19
  86. */
  87. public function notify()
  88. {
  89. return $this->pay->payNotify();
  90. }
  91. /**
  92. * @notes 退款
  93. * @param $data //微信订单号、商户退款单号、订单金额、退款金额、其他参数
  94. * @return bool
  95. * @author 段誉
  96. * @date 2021/8/4 14:57
  97. */
  98. public function refund($data)
  99. {
  100. $this->pay->set_params([ 'refund_data' => $data ]);
  101. return $this->pay->refund();
  102. }
  103. function queryRefund($refund)
  104. {
  105. $this->pay->set_params([ 'query_refund_data' => $refund ]);
  106. return $this->pay->queryRefund();
  107. }
  108. }