Pay.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Log;
  5. use think\Exception;
  6. use think\exception\HttpResponseException;
  7. use app\common\Service\Pay\PayService;
  8. use app\common\Service\Pay\PayOperService;
  9. use app\common\Enum\ChannelEnum;
  10. use app\common\Service\OrderService;
  11. /**
  12. * 支付接口
  13. */
  14. class Pay extends Base
  15. {
  16. protected $noNeedLogin = ['notify'];
  17. protected $noNeedRight = '*';
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. }
  22. /**
  23. *
  24. * 支付回调
  25. * @return void
  26. */
  27. public function notify()
  28. {
  29. $type = $this->request->param('type');
  30. $paytype = $this->request->param('paytype');
  31. if ($type == 'notify') {
  32. $pay = \addons\epay\library\Service::checkNotify($paytype);
  33. if (!$pay) {
  34. echo '签名错误';
  35. \think\Log::write('签名错误', 'epay',);
  36. return;
  37. }
  38. $data = $pay->verify();
  39. // try {
  40. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  41. PayOperService::settle($data['out_trade_no'], $payamount, $paytype == 'alipay' ? $data['trade_no'] : $data['transaction_id']);
  42. // } catch (\Exception $e) {
  43. // \think\Log::write($e->getMessage(), 'epay');
  44. // }
  45. echo $pay->success();
  46. }// pc 支付
  47. elseif ($type == 'return') {
  48. // $order_sn = $this->request->param('order_sn');
  49. // $this->redirect("index/shop.order/detail", ['orderid' => $order_sn]);
  50. }
  51. return;
  52. }
  53. /**
  54. * 预支付订单接口
  55. * @return void
  56. */
  57. public function prepay()
  58. {
  59. $user = $this->auth->getUser();
  60. $orderId = $this->request->post('orderId');
  61. $payment = $this->request->post('payment');
  62. $openid = $this->request->post('openid', '');
  63. $money = $this->request->post('money', 0);
  64. $money = $money > 0 ? $money : 0;
  65. $platform = $this->request->header('platform');
  66. try {
  67. // 获取订单实例
  68. $order = OrderService::getDetail($orderId);
  69. if (!$order) {
  70. $this->error(__('No Results were found'));
  71. }
  72. // 验证订单状态
  73. if (!$order->canPayHandle) {
  74. $this->error(__('订单状态不支持支付'));
  75. }
  76. // 验证支付类型
  77. if (!$payment || !in_array($payment, ['wechat', 'alipay', 'money', 'offline'])) {
  78. $this->error('支付类型不能为空');
  79. }
  80. // 验证支付环境
  81. // if ($channel && !ChannelEnum::channelSupportsPayment($channel, $payment)) {
  82. // $this->error('当前渠道不支持该支付方式');
  83. // }
  84. $payOper = new PayOperService();
  85. $orderType = $order->type;
  86. $payModel = $payOper->{$payment}($order, $order->amount, $orderType);
  87. // 处理微信支付宝(第三方)付款
  88. $result = $this->handleThirdPartyPayment($payModel, $order, $orderType, $payment, $platform, $openid);
  89. $this->success('', [
  90. 'pay_data' => $result,
  91. ]);
  92. } catch (\Exception $e) {
  93. Log::error('预支付订单失败: ' . $e->getMessage(), [
  94. 'order_sn' => $order_sn,
  95. 'payment' => $payment,
  96. 'user_id' => $user->id ?? 0
  97. ]);
  98. $this->error($e->getMessage());
  99. }
  100. }
  101. /**
  102. * 处理余额混合支付
  103. * @param PayOperService $payOper
  104. * @param mixed $order
  105. * @param string $order_type
  106. * @param float $money
  107. * @return mixed
  108. */
  109. protected function handleBalanceMixedPayment($payOper, $order, $order_type, $money)
  110. {
  111. return Db::transaction(function () use ($payOper, $order, $order_type, $money) {
  112. // 加锁读订单
  113. $order = $order->lock(true)->find($order->id);
  114. // 余额支付
  115. $order = $payOper->money($order, $money, $order_type);
  116. return $order;
  117. });
  118. }
  119. /**
  120. * 处理纯余额支付
  121. * @param PayOperService $payOper
  122. * @param mixed $order
  123. * @param string $order_type
  124. * @return mixed
  125. * @throws Exception
  126. */
  127. protected function handleBalancePayment($payOper, $order, $order_type)
  128. {
  129. $order = Db::transaction(function () use ($payOper, $order, $order_type) {
  130. // 加锁读订单
  131. $order = $order->lock(true)->find($order->id);
  132. $order = $payOper->money($order, $order->remain_pay_fee, $order_type);
  133. return $order;
  134. });
  135. if ($order->status != $order::STATUS_PAID) {
  136. throw new Exception('订单支付失败');
  137. }
  138. return $order;
  139. }
  140. /**
  141. * 处理货到付款
  142. * @param PayOperService $payOper
  143. * @param mixed $order
  144. * @param string $order_type
  145. * @return mixed
  146. * @throws Exception
  147. */
  148. protected function handleOfflinePayment($payOper, $order, $order_type)
  149. {
  150. if (!isset($order->ext['offline_status']) || $order->ext['offline_status'] != 'enable') {
  151. throw new Exception('订单不支持货到付款');
  152. }
  153. return Db::transaction(function () use ($payOper, $order, $order_type) {
  154. // 加锁读订单
  155. $order = $order->lock(true)->find($order->id);
  156. $order = $payOper->offline($order, 0, $order_type); // 增加 0 记录
  157. return $order;
  158. });
  159. }
  160. /**
  161. * 处理第三方支付(微信、支付宝)
  162. * @param PayOperService $payOper
  163. * @param mixed $order
  164. * @param string $order_type
  165. * @param string $payment
  166. * @param string $platform
  167. * @param string $channel
  168. * @param string $openid
  169. * @return mixed
  170. * @throws Exception
  171. */
  172. protected function handleThirdPartyPayment($payModel, $order, $payment, $platform, $openid)
  173. {
  174. $order_data = [
  175. 'order_id' => $order->id,
  176. 'out_trade_no' => $payModel->pay_sn,
  177. 'total_amount' => $payModel->pay_fee, // 剩余支付金额
  178. ];
  179. // 微信支付需要 openid
  180. if ($payment == 'wechat') {
  181. $this->handleWechatPaymentData($order_data, $platform, $openid, $order);
  182. $order_data['description'] = '商城订单支付';
  183. } else {
  184. $order_data['subject'] = '商城订单支付';
  185. }
  186. // 创建支付服务
  187. $payService = new PayService($payment, $platform);
  188. try {
  189. $result = $payService->pay($order_data);
  190. } catch (\Yansongda\Pay\Exception\Exception $e) {
  191. throw new Exception('支付失败' . (config('app_debug') ? ":" . $e->getMessage() : ',请重试'));
  192. } catch (HttpResponseException $e) {
  193. $data = $e->getResponse()->getData();
  194. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  195. throw new Exception('支付失败' . (config('app_debug') ? ":" . $message : ',请重试'));
  196. }
  197. // APP 平台特殊处理
  198. if ($platform == 'App') {
  199. if ($payment == 'wechat') {
  200. // Yansongda\Supports\Collection,可当数组,可当字符串,这里不用处理
  201. } else {
  202. // Guzzle
  203. $result = $result->getBody()->getContents();
  204. }
  205. }
  206. return $result;
  207. }
  208. /**
  209. * 处理微信支付数据
  210. * @param array &$order_data
  211. * @param string $platform
  212. * @param string $openid
  213. * @param mixed $order
  214. * @throws Exception
  215. */
  216. protected function handleWechatPaymentData(&$order_data, $platform, $openid, $order)
  217. {
  218. // 微信公众号,小程序支付,必须有 openid
  219. if (in_array($platform, ['WechatOfficialAccount', 'WechatMiniProgram'])) {
  220. if (isset($openid) && $openid) {
  221. // 如果传的有 openid
  222. $order_data['payer']['openid'] = $openid;
  223. } else {
  224. // 没有 openid 默认拿下单人的 openid
  225. // 需要根据实际的第三方授权模型调整
  226. try {
  227. $oauthModel = '\\app\\common\\model\\Third';
  228. if (class_exists($oauthModel)) {
  229. $oauth = $oauthModel::where([
  230. 'user_id' => $order->user_id,
  231. 'platform' => 'wechat',
  232. 'apptype' => lcfirst(str_replace('wechat', '', $platform))
  233. ])->find();
  234. $order_data['payer']['openid'] = $oauth ? $oauth->openid : '';
  235. } else {
  236. $order_data['payer']['openid'] = '';
  237. }
  238. } catch (\Exception $e) {
  239. $order_data['payer']['openid'] = '';
  240. }
  241. }
  242. if (empty($order_data['payer']['openid'])) {
  243. // 缺少 openid
  244. throw new Exception('miss_openid', -1);
  245. }
  246. }
  247. }
  248. }