WechatPay.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\pay\storage;
  12. use crmeb\exceptions\AdminException;
  13. use crmeb\services\pay\BasePay;
  14. use crmeb\exceptions\PayException;
  15. use crmeb\services\pay\PayInterface;
  16. use crmeb\services\app\MiniProgramService;
  17. use crmeb\services\app\WechatService;
  18. use crmeb\services\SystemConfigService;
  19. use EasyWeChat\Payment\API;
  20. use EasyWeChat\Payment\Order;
  21. use EasyWeChat\Support\Collection;
  22. use Psr\Http\Message\ResponseInterface;
  23. /**
  24. * 微信支付
  25. * Class WechatPay
  26. * @package crmeb\services\pay\storage
  27. */
  28. class WechatPay extends BasePay implements PayInterface
  29. {
  30. protected function initialize(array $config)
  31. {
  32. // TODO: Implement initialize() method.
  33. }
  34. /**
  35. * 创建订单进行支付
  36. * @param string $orderId
  37. * @param string $totalFee
  38. * @param string $attach
  39. * @param string $body
  40. * @param string $detail
  41. * @param array $options
  42. * @return array|mixed|string
  43. */
  44. public function create(string $orderId, string $totalFee, string $attach, string $body, string $detail, array $options = [])
  45. {
  46. $this->authSetPayType();
  47. switch ($this->payType) {
  48. case Order::NATIVE:
  49. return WechatService::nativePay(null, $orderId, $totalFee, $attach, $body, $detail);
  50. case Order::APP:
  51. return WechatService::appPay($options['openid'], $orderId, $totalFee, $attach, $body, $detail);
  52. case Order::JSAPI:
  53. if (empty($options['openid'])) {
  54. throw new PayException('缺少openid');
  55. }
  56. if (request()->isRoutine()) {
  57. // 获取配置 判断是否为新支付
  58. if ($options['pay_new_weixin_open']) {
  59. return MiniProgramService::newJsPay($options['openid'], $orderId, $totalFee, $attach, $body, $detail, $options);
  60. }
  61. return MiniProgramService::jsPay($options['openid'], $orderId, $totalFee, $attach, $body, $detail);
  62. }
  63. return WechatService::jsPay($options['openid'], $orderId, $totalFee, $attach, $body, $detail);
  64. case 'h5':
  65. return WechatService::paymentPrepare(null, $orderId, $totalFee, $attach, $body, $detail, 'MWEB');
  66. default:
  67. throw new PayException('微信支付:支付类型错误');
  68. }
  69. }
  70. /**
  71. * 支付到零钱
  72. * @param string $openid
  73. * @param string $orderId
  74. * @param string $amount
  75. * @param array $options
  76. * @return bool|mixed
  77. */
  78. public function merchantPay(string $openid, string $orderId, string $amount, array $options = [])
  79. {
  80. return WechatService::merchantPay($openid, $orderId, $amount, $options['desc'] ?? '');
  81. }
  82. /**
  83. * 退款
  84. * @param string $outTradeNo
  85. * @param array $opt
  86. * @return Collection|mixed|ResponseInterface
  87. */
  88. public function refund(string $outTradeNo, array $opt = [])
  89. {
  90. if (!isset($opt['pay_price'])) throw new PayException(400730);
  91. $totalFee = floatval(bcmul($opt['pay_price'], 100, 0));
  92. $refundFee = isset($opt['refund_price']) ? floatval(bcmul($opt['refund_price'], 100, 0)) : null;
  93. $refundReason = $opt['desc'] ?? '';
  94. $refundNo = $opt['refund_id'] ?? $outTradeNo;
  95. $opUserId = $opt['op_user_id'] ?? null;
  96. $type = $opt['type'] ?? 'out_trade_no';
  97. /**
  98. * 仅针对老资金流商户使用
  99. * REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款(默认使用未结算资金退款)
  100. * REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款
  101. */
  102. $refundAccount = $opt['refund_account'] ?? 'REFUND_SOURCE_UNSETTLED_FUNDS';
  103. if (isset($opt['wechat'])) {
  104. $result = WechatService::refund($outTradeNo, $refundNo, $totalFee, $refundFee, $opUserId, $refundReason, $type, $refundAccount);
  105. } else {
  106. if ($opt['pay_new_weixin_open']) {
  107. $result = MiniProgramService::miniRefund($outTradeNo, $totalFee, $refundFee, $opt);
  108. } else {
  109. $result = MiniProgramService::refund($outTradeNo, $refundNo, $totalFee, $refundFee, $opUserId, $refundReason, $type, $refundAccount);
  110. }
  111. }
  112. if (!empty($opt['pay_new_weixin_open'])) {
  113. if ($result['errcode'] != 0) throw new AdminException($result['errmsg']);
  114. } else {
  115. if (isset($result['return_code']) && $result['return_code'] != 'SUCCESS') throw new AdminException($result['return_msg']);
  116. if (isset($result['result_code']) && $result['result_code'] != 'SUCCESS') throw new AdminException($result['err_code_des']);
  117. if (isset($result['status']) && $result['status'] != 'SUCCESS') throw new AdminException($result['status']);
  118. }
  119. }
  120. /**
  121. * 查询退款订单
  122. * @param string $outTradeNo
  123. * @param string $outRequestNo
  124. * @param array $other
  125. * @return Collection|mixed|ResponseInterface
  126. */
  127. public function queryRefund(string $outTradeNo, string $outRequestNo, array $other = [])
  128. {
  129. return WechatService::queryRefund($outTradeNo, $other['type'] ?? API::OUT_TRADE_NO);
  130. }
  131. /**
  132. * 异步回调
  133. * @return mixed|\Symfony\Component\HttpFoundation\Response
  134. * @throws \EasyWeChat\Core\Exceptions\FaultException
  135. */
  136. public function handleNotify()
  137. {
  138. return WechatService::handleNotify();
  139. }
  140. }