OrderPayServices.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 = [],string $openid = '')
  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. @file_put_contents("quanju.txt", $openid . "用户openid\r\n", 8);
  108. switch ($payType) {
  109. case PayServices::WEIXIN_PAY:
  110. // if ($openid == ''){
  111. // $openid = '';
  112. // if (request()->isWechat() || request()->isRoutine()) {
  113. // if (request()->isWechat()) {
  114. // $userType = 'wechat';
  115. // } else {
  116. // $userType = 'routine';
  117. // }
  118. // /** @var WechatUserServices $services */
  119. // $services = app()->make(WechatUserServices::class);
  120. // $openid = $services->uidToOpenid($orderInfo['pay_uid'] ?? $orderInfo['uid'], $userType);
  121. // if (!$openid) {
  122. // throw new ApiException(410275);
  123. // }
  124. // }
  125. // $options['openid'] = $openid;
  126. // }else{
  127. $options['openid'] = $openid;
  128. // }
  129. if (!$openid) {
  130. throw new ApiException(410275);
  131. }
  132. break;
  133. case PayServices::ALLIN_PAY:
  134. if ($wechat) {
  135. $options['wechat'] = $wechat;
  136. }
  137. break;
  138. }
  139. $site_name = sys_config('site_name');
  140. if (isset($orderInfo['member_type'])) {
  141. $body = Str::substrUTf8($site_name . '--' . $orderInfo['member_type'], 20);
  142. $successAction = "member";
  143. /** @var OtherOrderServices $otherOrderServices */
  144. $otherOrderServices = app()->make(OtherOrderServices::class);
  145. $otherOrderServices->update($orderInfo['id'], ['pay_type' => $payType]);
  146. } else {
  147. /** @var StoreOrderCartInfoServices $orderInfoServices */
  148. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  149. $body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
  150. $body = Str::substrUTf8($site_name . '--' . $body, 20);
  151. $successAction = "product";
  152. /** @var StoreOrderServices $orderServices */
  153. $orderServices = app()->make(StoreOrderServices::class);
  154. $orderServices->update($orderInfo['id'], ['pay_type' => $payType]);
  155. }
  156. if (!$body) {
  157. throw new ApiException(410276);
  158. }
  159. //发起支付
  160. $jsConfig = $this->payServices->pay($payType, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body, $options);
  161. //发起支付后处理返回参数
  162. $payInfo = $this->afterPay($orderInfo, $jsConfig, $payType);
  163. $statusType = $this->payStatus($payType);
  164. return [
  165. 'status' => $statusType,
  166. 'payInfo' => $payInfo,
  167. ];
  168. }
  169. /**
  170. * 支付发起后处理返回参数
  171. * @param $order
  172. * @param $jsConfig
  173. * @param string $payType
  174. * @return array
  175. * @author 等风来
  176. * @email 136327134@qq.com
  177. * @date 2023/2/15
  178. */
  179. public function afterPay($order, $jsConfig, string $payType)
  180. {
  181. $payKey = md5($order['order_id']);
  182. switch ($payType) {
  183. case PayServices::ALIAPY_PAY:
  184. if (request()->isPc()) $jsConfig->invalid = time() + 60;
  185. CacheService::set($payKey, ['order_id' => $order['order_id'], 'other_pay_type' => false], 300);
  186. break;
  187. case PayServices::ALLIN_PAY:
  188. if (request()->isWechat()) {
  189. $payUrl = AllinPay::UNITODER_H5UNIONPAY;
  190. }
  191. break;
  192. case PayServices::WEIXIN_PAY:
  193. if (isset($jsConfig['mweb_url'])) {
  194. $jsConfig['h5_url'] = $jsConfig['mweb_url'];
  195. }
  196. }
  197. return ['jsConfig' => $jsConfig, 'order_id' => $order['order_id'], 'pay_key' => $payKey, 'pay_url' => $payUrl ?? ''];
  198. }
  199. }