OrderPayServices.php 7.3 KB

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