PayController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Http\Api;
  3. use App\Services\OrderService\ComboOrderService;
  4. use App\Services\OrderService\GoodsOrderService;
  5. use EasyWeChat\Factory;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Log;
  9. use Illuminate\Support\Facades\Request;
  10. class PayController extends HttpBaseController
  11. {
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->setWebsite(__NAMESPACE__);
  16. $this->isLoginJson();
  17. }
  18. /**
  19. * 换取支付code
  20. * @param $pmid
  21. * @return \Illuminate\Http\JsonResponse
  22. */
  23. public function getPayCode($pmid)
  24. {
  25. $agent_mid = Request::input('agent_mid');
  26. $product_type = Request::post('product_type', 1); // 1 服务商品 2 服务套餐
  27. $sku = Request::post('type');
  28. $payType = Request::post('pay_type', 'wechat');
  29. if (empty($sku)) {
  30. return responseMessage(2001, '参数错误');
  31. }
  32. //判断该产品是否存在
  33. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  34. if (empty($productInfo)) {
  35. return responseMessage(2003, "产品不存在");
  36. }
  37. $order_no = createNewOrderNo();
  38. Cache::put($order_no, [
  39. 'agent_mid' => $agent_mid,
  40. 'user_id' => $this->userId,
  41. 'product_type' => $product_type,
  42. 'payType' => $payType,
  43. 'sku' => $sku,
  44. 'product_id' => $productInfo->id,
  45. ], 24 * 3600);
  46. return responseMessage(1001, '', $order_no);
  47. }
  48. /**
  49. * 检测是否扫描支付
  50. *
  51. * @param $code
  52. * @return \Illuminate\Http\JsonResponse
  53. */
  54. public function checkPayScan($code)
  55. {
  56. $key = "pay_scan_" . $code;
  57. // Log::error("checkPayScan");
  58. if (Cache::get($key)) {
  59. Cache::forget($key);
  60. // Log::info("checkPayScan === true");
  61. return responseMessage(1001, '');
  62. } else {
  63. return responseMessage(2001, '');
  64. }
  65. }
  66. /**
  67. * 检测是否支付完成
  68. *
  69. * @param $code
  70. * @return \Illuminate\Http\JsonResponse
  71. */
  72. public function checkPayEnd($code)
  73. {
  74. $product_type = Request::post('product_type', 1); // 1 服务商品 2 服务套餐
  75. $orderService = $this->getOrderService($product_type, $this->userId);
  76. // DB::connection()->enableQueryLog();#开启执行日志
  77. $isExist = $orderService->getOrderInfo($code, ['user_id' => $this->userId, 'order_status' => 2], ['id']);
  78. // Log::info(" sql === ", DB::getQueryLog());
  79. if ($isExist) {
  80. // Log::info("checkPayEnd === true");
  81. // 删除订单号,防止重复提交
  82. Cache::forget($code);
  83. return responseMessage(1001, '');
  84. } else {
  85. // Log::info("checkPayEnd === false");
  86. return responseMessage(2001, '');
  87. }
  88. }
  89. /**
  90. * 根据订单号,获取订单服务
  91. *
  92. * @param string $productType
  93. * @param int $userId
  94. * @return ComboOrderService|GoodsOrderService|null
  95. */
  96. public function getOrderService(string $productType, int $userId): GoodsOrderService|ComboOrderService|null
  97. {
  98. if ($productType == 1) {
  99. return new GoodsOrderService($userId);
  100. } else {
  101. return new ComboOrderService($userId);
  102. }
  103. }
  104. /**
  105. * 微信小程序支付
  106. */
  107. public function wxpay()
  108. {
  109. $requestData = Request::all();
  110. $orderNo = $requestData['order_no'] ?? '';
  111. if (empty($orderNo)) {
  112. die('订单不存在或者已删除!');
  113. }
  114. //获取订单详情
  115. $info = $this->getOrderInfo($orderNo);
  116. if (empty($info)) {
  117. return responseMessage(2001, '订单不存在或者已删除');
  118. }
  119. $payParams = [
  120. 'body' => '订单号:' . $orderNo,
  121. 'out_trade_no' => $orderNo,
  122. 'total_fee' => $info['order_amount_total'] * 100,
  123. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  124. ];
  125. $payParams['openid'] = $this->userInfo['wx_openid'] ?? '';
  126. $config = config('wechat.payment.default');
  127. $app = Factory::payment($config);
  128. $app->scheme($orderNo);
  129. $jssdk = $app->jssdk;
  130. $result = $app->order->unify($payParams);
  131. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  132. $prepayId = $result['prepay_id'];
  133. $json = $jssdk->bridgeConfig($prepayId, false); // 返回 json 字符串,如果想返回数组,传第二个参数 false
  134. Log::driver('wepay')->info(__CLASS__ . '订单生成成功!', $result);
  135. return responseMessage(1001, 'success', $json);
  136. } elseif ($result['return_code'] == 'FAIL') {
  137. Log::driver('wepay')->error(__CLASS__ . '订单生成失败,请重试!', $result);
  138. return responseMessage(2006, '订单生成失败,请重试!' . $result['return_msg']);
  139. } else {
  140. Log::driver('wepay')->error(__CLASS__ . '订单生成失败,请重试!', $result);
  141. return responseMessage(2007, '订单生成失败,请重试!' . $result['err_code'] . ' ' . $result['err_code_des']);
  142. }
  143. }
  144. }