Pay.php 9.2 KB

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