setWebsite(__NAMESPACE__); $this->isLoginJson(); } /** * 换取支付code * @param $pmid * @return \Illuminate\Http\JsonResponse */ public function getPayCode($pmid) { $agent_mid = Request::input('agent_mid'); $product_type = Request::post('product_type', 1); // 1 服务商品 2 服务套餐 $sku = Request::post('type'); $payType = Request::post('pay_type', 'wechat'); if (empty($sku)) { return responseMessage(2001, '参数错误'); } //判断该产品是否存在 $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first(); if (empty($productInfo)) { return responseMessage(2003, "产品不存在"); } $order_no = createNewOrderNo(); Cache::put($order_no, [ 'agent_mid' => $agent_mid, 'user_id' => $this->userId, 'product_type' => $product_type, 'payType' => $payType, 'sku' => $sku, 'product_id' => $productInfo->id, ], 24 * 3600); return responseMessage(1001, '', $order_no); } /** * 检测是否扫描支付 * * @param $code * @return \Illuminate\Http\JsonResponse */ public function checkPayScan($code) { $key = "pay_scan_" . $code; // Log::error("checkPayScan"); if (Cache::get($key)) { Cache::forget($key); // Log::info("checkPayScan === true"); return responseMessage(1001, ''); } else { return responseMessage(2001, ''); } } /** * 检测是否支付完成 * * @param $code * @return \Illuminate\Http\JsonResponse */ public function checkPayEnd($code) { $product_type = Request::post('product_type', 1); // 1 服务商品 2 服务套餐 $orderService = $this->getOrderService($product_type, $this->userId); // DB::connection()->enableQueryLog();#开启执行日志 $isExist = $orderService->getOrderInfo($code, ['user_id' => $this->userId, 'order_status' => 2], ['id']); // Log::info(" sql === ", DB::getQueryLog()); if ($isExist) { // Log::info("checkPayEnd === true"); // 删除订单号,防止重复提交 Cache::forget($code); return responseMessage(1001, ''); } else { // Log::info("checkPayEnd === false"); return responseMessage(2001, ''); } } /** * 根据订单号,获取订单服务 * * @param string $productType * @param int $userId * @return ComboOrderService|GoodsOrderService|null */ public function getOrderService(string $productType, int $userId): GoodsOrderService|ComboOrderService|null { if ($productType == 1) { return new GoodsOrderService($userId); } else { return new ComboOrderService($userId); } } /** * 微信小程序支付 */ public function wxpay() { $requestData = Request::all(); $orderNo = $requestData['order_no'] ?? ''; if (empty($orderNo)) { die('订单不存在或者已删除!'); } //获取订单详情 $info = $this->getOrderInfo($orderNo); if (empty($info)) { return responseMessage(2001, '订单不存在或者已删除'); } $payParams = [ 'body' => '订单号:' . $orderNo, 'out_trade_no' => $orderNo, 'total_fee' => $info['order_amount_total'] * 100, 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型 ]; $payParams['openid'] = $this->userInfo['wx_openid'] ?? ''; $config = config('wechat.payment.default'); $app = Factory::payment($config); $app->scheme($orderNo); $jssdk = $app->jssdk; $result = $app->order->unify($payParams); if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') { $prepayId = $result['prepay_id']; $json = $jssdk->bridgeConfig($prepayId, false); // 返回 json 字符串,如果想返回数组,传第二个参数 false Log::driver('wepay')->info(__CLASS__ . '订单生成成功!', $result); return responseMessage(1001, 'success', $json); } elseif ($result['return_code'] == 'FAIL') { Log::driver('wepay')->error(__CLASS__ . '订单生成失败,请重试!', $result); return responseMessage(2006, '订单生成失败,请重试!' . $result['return_msg']); } else { Log::driver('wepay')->error(__CLASS__ . '订单生成失败,请重试!', $result); return responseMessage(2007, '订单生成失败,请重试!' . $result['err_code'] . ' ' . $result['err_code_des']); } } }