Pay.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. use app\common\model\Third as ThirdOauth;
  12. use app\common\model\Order;
  13. use app\common\model\pay\Index as PayModel;
  14. use app\common\Enum\PayEnum;
  15. use Yansongda\Pay\Pay as YansongdaPay;
  16. use Psr\Http\Message\ResponseInterface;
  17. /**
  18. * 支付接口
  19. */
  20. class Pay extends Base
  21. {
  22. protected $noNeedLogin = ['notify'];
  23. protected $noNeedRight = '*';
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. }
  28. /**
  29. * 支付成功回调
  30. */
  31. public function notify()
  32. {
  33. Log::write('pay-notify-comein:');
  34. $payment = $this->request->param('payment');
  35. $platform = $this->request->param('platform');
  36. $payService = new PayService($payment, $platform);
  37. $result = $payService->notify(function ($data, $originData = []) use ($payment) {
  38. Log::write('pay-notify-data:' . json_encode($data));
  39. $out_trade_no = $data['out_trade_no'];
  40. // 查询 pay 交易记录
  41. $payModel = PayModel::where('pay_sn', $out_trade_no)->find();
  42. if (!$payModel || $payModel->status != PayEnum::PAY_STATUS_UNPAID) {
  43. // 订单不存在,或者订单已支付
  44. return YansongdaPay::$payment()->success();
  45. }
  46. Db::transaction(function () use ($payModel, $data, $originData, $payment) {
  47. $notify = [
  48. 'pay_sn' => $data['out_trade_no'],
  49. 'transaction_id' => $data['transaction_id'],
  50. 'notify_time' => $data['notify_time'],
  51. 'buyer_info' => $data['buyer_info'] ?? "",
  52. 'payment_json' => $originData ? json_encode($originData) : json_encode($data),
  53. 'pay_fee' => $data['pay_fee'], // 微信的已经*100处理过了
  54. 'pay_type' => $payment // 支付方式
  55. ];
  56. // pay 实例
  57. $payOper = new PayOperService($payModel->user_id);
  58. $payOper->notify($payModel, $notify);
  59. });
  60. return YansongdaPay::$payment()->success();
  61. });
  62. return $this->payResponse($result, $payment);
  63. }
  64. /**
  65. * 处理返回结果 tp5 不能直接 return YansongdaPay::$payment()->success()
  66. *
  67. * @param object|string $result
  68. * @param string|null $payment
  69. * @return void
  70. */
  71. private function payResponse($result = null, $payment = null)
  72. {
  73. if ($result instanceof ResponseInterface) {
  74. $content = $result->getBody()->getContents();
  75. $content = $payment == 'wechat' || $payment == 'douyin' ? json_decode($content, true) : $content;
  76. return response($content, 200, [], ($payment == 'wechat' || $payment == 'douyin' ? 'json' : ''));
  77. }
  78. return $result;
  79. }
  80. /**
  81. * 预支付订单接口
  82. * @return void
  83. */
  84. public function prepay()
  85. {
  86. $orderId = $this->request->post('orderId');
  87. $payment = $this->request->post('payment');
  88. // $openid = $this->request->post('openid', '');
  89. // $money = $this->request->post('money', 0);
  90. $logincode = $this->request->post('login_code', '');
  91. // $money = $money > 0 ? $money : 0;
  92. $platform = $this->request->header('platform');
  93. $userId = $this->auth->getUser()->id;
  94. // 获取订单实例
  95. $order = new Order();
  96. $order = $order->where('user_id', $userId)->where('id', $orderId)->find();
  97. if (!$order) {
  98. $this->error(__('No Results were found'));
  99. }
  100. // 验证订单状态
  101. if (!$order->canPayHandle()) {
  102. $this->error(__('订单状态不支持支付'));
  103. }
  104. // 验证支付类型
  105. if (!$payment || !in_array($payment, ['wechat', 'alipay', 'douyin'])) {
  106. $this->error('支付类型不能为空');
  107. }
  108. // 验证支付环境
  109. // if ($channel && !ChannelEnum::channelSupportsPayment($channel, $payment)) {
  110. // $this->error('当前渠道不支持该支付方式');
  111. // }
  112. $payOper = new PayOperService($this->auth->getUser());
  113. $orderType = $order->type;
  114. $payModel = $payOper->{$payment}($order, $order->amount, $orderType);
  115. $order->save(['pay_type' =>$payment]);
  116. $order_data = [
  117. 'order_id' => $order->id,
  118. 'out_trade_no' => $payModel->pay_sn,
  119. 'total_amount' => $payModel->pay_fee, // 剩余支付金额
  120. ];
  121. // 微信公众号,小程序支付,必须有 openid
  122. if ($payment == 'wechat') {
  123. // 如果传了 code
  124. if (!empty($logincode)) {
  125. $json = (new \app\common\library\Wechat\Service())->getWechatSession($logincode);
  126. $openid = $json['openid'] ?? '';
  127. $order_data['payer']['openid'] = $openid;
  128. }else{
  129. if (in_array($platform, ['WechatOfficialAccount', 'WechatMiniProgram'])) {
  130. if (isset($openid) && $openid) {
  131. // 如果传的有 openid
  132. $order_data['payer']['openid'] = $openid;
  133. } else {
  134. // 没有 openid 默认拿下单人的 openid
  135. $oauth = ThirdOauth::where([
  136. 'user_id' => $order->user_id,
  137. 'platform' => $payment,
  138. 'apptype' => lcfirst(str_replace($payment, '', $platform))
  139. ])->find();
  140. $order_data['payer']['openid'] = $oauth ? $oauth->openid : '';
  141. }
  142. if (empty($order_data['payer']['openid'])) {
  143. // 缺少 openid
  144. $this->error('miss_openid', -1);
  145. }
  146. }
  147. }
  148. $order_data['description'] = '商城订单支付';
  149. } else {
  150. $order_data['subject'] = '商城订单支付';
  151. }
  152. if($platform == ChannelEnum::CHANNEL_DOUYIN_MINI_PROGRAM){
  153. $order_data['out_order_no'] = $payModel->pay_sn;
  154. }
  155. // echo "<pre>";
  156. // print_r(lcfirst(str_replace($payment, '', $platform)));
  157. // echo "</pre>";die();
  158. // echo "<pre>";
  159. // print_r($order_data);
  160. // echo "</pre>";die();
  161. try {
  162. $payService = new PayService($payment, $platform);
  163. $result = $payService->pay($order_data);
  164. } catch (\Yansongda\Pay\Exception\Exception $e) {
  165. $this->error('支付失败' . (config('app_debug') ? ":" . $e->getMessage() : ',请重试'));
  166. } catch (HttpResponseException $e) {
  167. $data = $e->getResponse()->getData();
  168. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  169. $this->error('支付失败' . (config('app_debug') ? ":" . $message : ',请重试'));
  170. } catch (\Exception $e) {
  171. // 捕获构造函数错误和其他异常
  172. $errorMessage = $e->getMessage();
  173. Log::error('PayService创建失败: ' . $errorMessage);
  174. $this->error('支付服务初始化失败' . (config('app_debug') ? ":" . $errorMessage : ',请重试'));
  175. }
  176. $this->success('', [
  177. 'pay_data' => $result,
  178. ]);
  179. }
  180. /**
  181. * 处理余额混合支付
  182. * @param PayOperService $payOper
  183. * @param mixed $order
  184. * @param string $order_type
  185. * @param float $money
  186. * @return mixed
  187. */
  188. protected function handleBalanceMixedPayment($payOper, $order, $order_type, $money)
  189. {
  190. return Db::transaction(function () use ($payOper, $order, $order_type, $money) {
  191. // 加锁读订单
  192. $order = $order->lock(true)->find($order->id);
  193. // 余额支付
  194. $order = $payOper->money($order, $money, $order_type);
  195. return $order;
  196. });
  197. }
  198. /**
  199. * 处理纯余额支付
  200. * @param PayOperService $payOper
  201. * @param mixed $order
  202. * @param string $order_type
  203. * @return mixed
  204. * @throws Exception
  205. */
  206. protected function handleBalancePayment($payOper, $order, $order_type)
  207. {
  208. $order = Db::transaction(function () use ($payOper, $order, $order_type) {
  209. // 加锁读订单
  210. $order = $order->lock(true)->find($order->id);
  211. $order = $payOper->money($order, $order->remain_pay_fee, $order_type);
  212. return $order;
  213. });
  214. if ($order->status != $order::STATUS_PAID) {
  215. throw new Exception('订单支付失败');
  216. }
  217. return $order;
  218. }
  219. /**
  220. * 处理货到付款
  221. * @param PayOperService $payOper
  222. * @param mixed $order
  223. * @param string $order_type
  224. * @return mixed
  225. * @throws Exception
  226. */
  227. protected function handleOfflinePayment($payOper, $order, $order_type)
  228. {
  229. if (!isset($order->ext['offline_status']) || $order->ext['offline_status'] != 'enable') {
  230. throw new Exception('订单不支持货到付款');
  231. }
  232. return Db::transaction(function () use ($payOper, $order, $order_type) {
  233. // 加锁读订单
  234. $order = $order->lock(true)->find($order->id);
  235. $order = $payOper->offline($order, 0, $order_type); // 增加 0 记录
  236. return $order;
  237. });
  238. }
  239. /**
  240. * 处理第三方支付(微信、支付宝)
  241. * @param PayOperService $payOper
  242. * @param mixed $order
  243. * @param string $order_type
  244. * @param string $payment
  245. * @param string $platform
  246. * @param string $channel
  247. * @param string $openid
  248. * @return mixed
  249. * @throws Exception
  250. */
  251. protected function handleThirdPartyPayment($payModel, $order, $payment, $platform, $openid)
  252. {
  253. $order_data = [
  254. 'order_id' => $order->id,
  255. 'out_trade_no' => $payModel->pay_sn,
  256. 'total_amount' => $payModel->pay_fee, // 剩余支付金额
  257. ];
  258. // 微信支付需要 openid
  259. if ($payment == 'wechat') {
  260. $this->handleWechatPaymentData($order_data, $platform, $openid, $order);
  261. $order_data['description'] = '商城订单支付';
  262. } else {
  263. $order_data['subject'] = '商城订单支付';
  264. }
  265. // 创建支付服务
  266. $payService = new PayService($payment, $platform);
  267. try {
  268. $result = $payService->pay($order_data);
  269. } catch (\Yansongda\Pay\Exception\Exception $e) {
  270. throw new Exception('支付失败' . (config('app_debug') ? ":" . $e->getMessage() : ',请重试'));
  271. } catch (HttpResponseException $e) {
  272. $data = $e->getResponse()->getData();
  273. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  274. throw new Exception('支付失败' . (config('app_debug') ? ":" . $message : ',请重试'));
  275. }
  276. // APP 平台特殊处理
  277. if ($platform == 'App') {
  278. if ($payment == 'wechat') {
  279. // Yansongda\Supports\Collection,可当数组,可当字符串,这里不用处理
  280. } else {
  281. // Guzzle
  282. $result = $result->getBody()->getContents();
  283. }
  284. }
  285. return $result;
  286. }
  287. /**
  288. * 处理微信支付数据
  289. * @param array &$order_data
  290. * @param string $platform
  291. * @param string $openid
  292. * @param mixed $order
  293. * @throws Exception
  294. */
  295. protected function handleWechatPaymentData(&$order_data, $platform, $openid, $order)
  296. {
  297. // 微信公众号,小程序支付,必须有 openid
  298. if (in_array($platform, ['WechatOfficialAccount', 'WechatMiniProgram'])) {
  299. if (isset($openid) && $openid) {
  300. // 如果传的有 openid
  301. $order_data['payer']['openid'] = $openid;
  302. } else {
  303. // 没有 openid 默认拿下单人的 openid
  304. // 需要根据实际的第三方授权模型调整
  305. try {
  306. $oauthModel = '\\app\\common\\model\\Third';
  307. if (class_exists($oauthModel)) {
  308. $oauth = $oauthModel::where([
  309. 'user_id' => $order->user_id,
  310. 'platform' => 'wechat',
  311. 'apptype' => lcfirst(str_replace('wechat', '', $platform))
  312. ])->find();
  313. $order_data['payer']['openid'] = $oauth ? $oauth->openid : '';
  314. } else {
  315. $order_data['payer']['openid'] = '';
  316. }
  317. } catch (\Exception $e) {
  318. $order_data['payer']['openid'] = '';
  319. }
  320. }
  321. if (empty($order_data['payer']['openid'])) {
  322. // 缺少 openid
  323. throw new Exception('miss_openid', -1);
  324. }
  325. }
  326. }
  327. }