Pay.php 9.1 KB

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