Pay.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. if(empty($openid)){
  128. $this->error('未获取到openidid');
  129. }
  130. $order_data['payer']['openid'] = $openid;
  131. }else{
  132. if (in_array($platform, ['WechatOfficialAccount', 'WechatMiniProgram'])) {
  133. if (isset($openid) && $openid) {
  134. // 如果传的有 openid
  135. $order_data['payer']['openid'] = $openid;
  136. } else {
  137. // 没有 openid 默认拿下单人的 openid
  138. $oauth = ThirdOauth::where([
  139. 'user_id' => $order->user_id,
  140. 'platform' => $payment,
  141. 'apptype' => lcfirst(str_replace($payment, '', $platform))
  142. ])->find();
  143. $order_data['payer']['openid'] = $oauth ? $oauth->openid : '';
  144. }
  145. if (empty($order_data['payer']['openid'])) {
  146. // 缺少 openid
  147. $this->error('miss_openid', -1);
  148. }
  149. }
  150. }
  151. $order_data['description'] = '商城订单支付';
  152. } else {
  153. $order_data['subject'] = '商城订单支付';
  154. }
  155. if($platform == ChannelEnum::CHANNEL_DOUYIN_MINI_PROGRAM){
  156. $order_data['out_order_no'] = $payModel->pay_sn;
  157. }
  158. // echo "<pre>";
  159. // print_r(lcfirst(str_replace($payment, '', $platform)));
  160. // echo "</pre>";die();
  161. // echo "<pre>";
  162. // print_r($order_data);
  163. // echo "</pre>";die();
  164. try {
  165. $payService = new PayService($payment, $platform);
  166. $result = $payService->pay($order_data);
  167. } catch (\Yansongda\Pay\Exception\Exception $e) {
  168. $this->error('支付失败' . (config('app_debug') ? ":" . $e->getMessage() : ',请重试'));
  169. } catch (HttpResponseException $e) {
  170. $data = $e->getResponse()->getData();
  171. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  172. $this->error('支付失败' . (config('app_debug') ? ":" . $message : ',请重试'));
  173. } catch (\Exception $e) {
  174. // 捕获构造函数错误和其他异常
  175. $errorMessage = $e->getMessage();
  176. Log::error('PayService创建失败: ' . $errorMessage);
  177. $this->error('支付服务初始化失败' . (config('app_debug') ? ":" . $errorMessage : ',请重试'));
  178. }
  179. $this->success('', [
  180. 'pay_data' => $result,
  181. ]);
  182. }
  183. /**
  184. * 处理余额混合支付
  185. * @param PayOperService $payOper
  186. * @param mixed $order
  187. * @param string $order_type
  188. * @param float $money
  189. * @return mixed
  190. */
  191. protected function handleBalanceMixedPayment($payOper, $order, $order_type, $money)
  192. {
  193. return Db::transaction(function () use ($payOper, $order, $order_type, $money) {
  194. // 加锁读订单
  195. $order = $order->lock(true)->find($order->id);
  196. // 余额支付
  197. $order = $payOper->money($order, $money, $order_type);
  198. return $order;
  199. });
  200. }
  201. /**
  202. * 处理纯余额支付
  203. * @param PayOperService $payOper
  204. * @param mixed $order
  205. * @param string $order_type
  206. * @return mixed
  207. * @throws Exception
  208. */
  209. protected function handleBalancePayment($payOper, $order, $order_type)
  210. {
  211. $order = Db::transaction(function () use ($payOper, $order, $order_type) {
  212. // 加锁读订单
  213. $order = $order->lock(true)->find($order->id);
  214. $order = $payOper->money($order, $order->remain_pay_fee, $order_type);
  215. return $order;
  216. });
  217. if ($order->status != $order::STATUS_PAID) {
  218. throw new Exception('订单支付失败');
  219. }
  220. return $order;
  221. }
  222. /**
  223. * 处理货到付款
  224. * @param PayOperService $payOper
  225. * @param mixed $order
  226. * @param string $order_type
  227. * @return mixed
  228. * @throws Exception
  229. */
  230. protected function handleOfflinePayment($payOper, $order, $order_type)
  231. {
  232. if (!isset($order->ext['offline_status']) || $order->ext['offline_status'] != 'enable') {
  233. throw new Exception('订单不支持货到付款');
  234. }
  235. return Db::transaction(function () use ($payOper, $order, $order_type) {
  236. // 加锁读订单
  237. $order = $order->lock(true)->find($order->id);
  238. $order = $payOper->offline($order, 0, $order_type); // 增加 0 记录
  239. return $order;
  240. });
  241. }
  242. /**
  243. * 处理第三方支付(微信、支付宝)
  244. * @param PayOperService $payOper
  245. * @param mixed $order
  246. * @param string $order_type
  247. * @param string $payment
  248. * @param string $platform
  249. * @param string $channel
  250. * @param string $openid
  251. * @return mixed
  252. * @throws Exception
  253. */
  254. protected function handleThirdPartyPayment($payModel, $order, $payment, $platform, $openid)
  255. {
  256. $order_data = [
  257. 'order_id' => $order->id,
  258. 'out_trade_no' => $payModel->pay_sn,
  259. 'total_amount' => $payModel->pay_fee, // 剩余支付金额
  260. ];
  261. // 微信支付需要 openid
  262. if ($payment == 'wechat') {
  263. $this->handleWechatPaymentData($order_data, $platform, $openid, $order);
  264. $order_data['description'] = '商城订单支付';
  265. } else {
  266. $order_data['subject'] = '商城订单支付';
  267. }
  268. // 创建支付服务
  269. $payService = new PayService($payment, $platform);
  270. try {
  271. $result = $payService->pay($order_data);
  272. } catch (\Yansongda\Pay\Exception\Exception $e) {
  273. throw new Exception('支付失败' . (config('app_debug') ? ":" . $e->getMessage() : ',请重试'));
  274. } catch (HttpResponseException $e) {
  275. $data = $e->getResponse()->getData();
  276. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  277. throw new Exception('支付失败' . (config('app_debug') ? ":" . $message : ',请重试'));
  278. }
  279. // APP 平台特殊处理
  280. if ($platform == 'App') {
  281. if ($payment == 'wechat') {
  282. // Yansongda\Supports\Collection,可当数组,可当字符串,这里不用处理
  283. } else {
  284. // Guzzle
  285. $result = $result->getBody()->getContents();
  286. }
  287. }
  288. return $result;
  289. }
  290. /**
  291. * 处理微信支付数据
  292. * @param array &$order_data
  293. * @param string $platform
  294. * @param string $openid
  295. * @param mixed $order
  296. * @throws Exception
  297. */
  298. protected function handleWechatPaymentData(&$order_data, $platform, $openid, $order)
  299. {
  300. // 微信公众号,小程序支付,必须有 openid
  301. if (in_array($platform, ['WechatOfficialAccount', 'WechatMiniProgram'])) {
  302. if (isset($openid) && $openid) {
  303. // 如果传的有 openid
  304. $order_data['payer']['openid'] = $openid;
  305. } else {
  306. // 没有 openid 默认拿下单人的 openid
  307. // 需要根据实际的第三方授权模型调整
  308. try {
  309. $oauthModel = '\\app\\common\\model\\Third';
  310. if (class_exists($oauthModel)) {
  311. $oauth = $oauthModel::where([
  312. 'user_id' => $order->user_id,
  313. 'platform' => 'wechat',
  314. 'apptype' => lcfirst(str_replace('wechat', '', $platform))
  315. ])->find();
  316. $order_data['payer']['openid'] = $oauth ? $oauth->openid : '';
  317. } else {
  318. $order_data['payer']['openid'] = '';
  319. }
  320. } catch (\Exception $e) {
  321. $order_data['payer']['openid'] = '';
  322. }
  323. }
  324. if (empty($order_data['payer']['openid'])) {
  325. // 缺少 openid
  326. throw new Exception('miss_openid', -1);
  327. }
  328. }
  329. }
  330. }