OrderPayServices.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 app\services\pay;
  12. use app\services\order\OtherOrderServices;
  13. use app\services\order\StoreOrderCartInfoServices;
  14. use app\services\order\StoreOrderServices;
  15. use app\services\wechat\WechatUserServices;
  16. use crmeb\exceptions\ApiException;
  17. use crmeb\services\CacheService;
  18. use crmeb\services\pay\extend\allinpay\AllinPay;
  19. use crmeb\utils\Str;
  20. use think\exception\ValidateException;
  21. /**
  22. * 订单发起支付
  23. * Class OrderPayServices
  24. * @package app\services\pay
  25. */
  26. class OrderPayServices
  27. {
  28. /**
  29. * 支付
  30. * @var PayServices
  31. */
  32. protected $payServices;
  33. public function __construct(PayServices $services)
  34. {
  35. $this->payServices = $services;
  36. }
  37. /**
  38. * 获取支付方式
  39. * @param string $payType
  40. * @return string
  41. * @author 等风来
  42. * @email 136327134@qq.com
  43. * @date 2023/2/15
  44. */
  45. public function getPayType(string $payType)
  46. {
  47. $allinPay = (int)sys_config('allin_pay_status') == 1;
  48. //微信支付没有开启,通联支付开启,用户访问端在小程序或者公众号的时候,使用通联微信H5支付
  49. if ($payType == PayServices::WEIXIN_PAY) {
  50. $wechat_pay_type = (int)sys_config('wechat_pay_type', 0);
  51. if ($wechat_pay_type == 1 && $allinPay && (request()->isRoutine() || request()->isWechat())) {
  52. $payType = PayServices::ALLIN_PAY;
  53. }
  54. }
  55. //支付宝没有开启,通联支付开了,用户使用支付宝支付,并且在app端访问的时候,使用通联app支付宝支付
  56. if ($payType == PayServices::ALIAPY_PAY) {
  57. $alipay_pay_type = (int)sys_config('alipay_pay_type', 0);
  58. if ($alipay_pay_type == 1 && $allinPay && request()->isApp()) {
  59. $payType = PayServices::ALLIN_PAY;
  60. }
  61. }
  62. return $payType;
  63. }
  64. /**
  65. * 获取返回类型
  66. * @param string $payType
  67. * @return string
  68. * @author 等风来
  69. * @email 136327134@qq.com
  70. * @date 2023/2/15
  71. */
  72. public function payStatus(string $payType)
  73. {
  74. if ($payType == PayServices::WEIXIN_PAY) {
  75. if (request()->isH5()) {
  76. $payStstus = 'wechat_h5_pay';
  77. } else if (request()->isPc()) {
  78. $payStstus = 'wechat_pc_pay';
  79. } else {
  80. $payStstus = 'wechat_pay';
  81. }
  82. } else if ($payType == PayServices::ALIAPY_PAY) {
  83. $payStstus = 'alipay_pay';
  84. } else if ($payType == PayServices::ALLIN_PAY) {
  85. $payStstus = 'allinpay_pay';
  86. } else {
  87. throw new ValidateException('获取支付返回类型失败');
  88. }
  89. return $payStstus;
  90. }
  91. /**
  92. * 发起支付前
  93. * @param array $orderInfo
  94. * @param string $payType
  95. * @param array $options
  96. * @return array
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. * @author 等风来
  101. * @email 136327134@qq.com
  102. * @date 2023/2/15
  103. */
  104. public function beforePay(array $orderInfo, string $payType, array $options = [])
  105. {
  106. $payType = $this->getPayType($payType);
  107. if ($orderInfo['paid']) {
  108. throw new ApiException(410174);
  109. }
  110. if ($orderInfo['pay_price'] <= 0) {
  111. throw new ApiException(410274);
  112. }
  113. switch ($payType) {
  114. case PayServices::WEIXIN_PAY:
  115. $openid = '';
  116. if (request()->isWechat() || request()->isRoutine()) {
  117. if (request()->isWechat()) {
  118. $userType = 'wechat';
  119. } else {
  120. $userType = 'routine';
  121. }
  122. /** @var WechatUserServices $services */
  123. $services = app()->make(WechatUserServices::class);
  124. $openid = $services->uidToOpenid($orderInfo['pay_uid'] ?? $orderInfo['uid'], $userType);
  125. if (!$openid) {
  126. throw new ApiException(410275);
  127. }
  128. }
  129. $options['openid'] = $openid;
  130. break;
  131. }
  132. $site_name = sys_config('site_name');
  133. if (isset($orderInfo['member_type'])) {
  134. $body = Str::substrUTf8($site_name . '--' . $orderInfo['member_type'], 20);
  135. $successAction = "member";
  136. /** @var OtherOrderServices $otherOrderServices */
  137. $otherOrderServices = app()->make(OtherOrderServices::class);
  138. $otherOrderServices->update($orderInfo['id'], ['pay_type' => $payType]);
  139. } else {
  140. /** @var StoreOrderCartInfoServices $orderInfoServices */
  141. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  142. $body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
  143. $body = Str::substrUTf8($site_name . '--' . $body, 20);
  144. $successAction = "product";
  145. /** @var StoreOrderServices $orderServices */
  146. $orderServices = app()->make(StoreOrderServices::class);
  147. $orderServices->update($orderInfo['id'], ['pay_type' => $payType]);
  148. }
  149. if (!$body) {
  150. throw new ApiException(410276);
  151. }
  152. //发起支付
  153. $jsConfig = $this->payServices->payV2($payType, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body, $options);
  154. //发起支付后处理返回参数
  155. $payInfo = $this->afterPay($orderInfo, $jsConfig, $payType);
  156. $statusType = $this->payStatus($payType);
  157. return [
  158. 'status' => $statusType,
  159. 'payInfo' => $payInfo,
  160. ];
  161. }
  162. /**
  163. * 支付发起后处理返回参数
  164. * @param $order
  165. * @param $jsConfig
  166. * @param string $payType
  167. * @param array $options
  168. * @return array
  169. * @author 等风来
  170. * @email 136327134@qq.com
  171. * @date 2023/2/15
  172. */
  173. public function afterPay($order, $jsConfig, string $payType, array $options = [])
  174. {
  175. $payKey = md5($order['order_id']);
  176. switch ($payType) {
  177. case PayServices::ALIAPY_PAY:
  178. $isCode = $options['isCode'] ?? '';
  179. if ($isCode && !($jsConfig->invalid ?? false)) $jsConfig->invalid = time() + 60;
  180. CacheService::set($payKey, ['order_id' => $order['order_id'], 'other_pay_type' => false], 300);
  181. break;
  182. case PayServices::ALLIN_PAY:
  183. if (request()->isWechat()) {
  184. $jsConfig['pay_url'] = AllinPay::UNITODER_H5UNIONPAY;
  185. }
  186. }
  187. return ['jsConfig' => $jsConfig, 'order_id' => $order['order_id'], 'pay_key' => $payKey];
  188. }
  189. }