input('id'); if (!$order_id) { return $this->error('无效的ID'); } //用户openid $openid = User::query()->where('id', $this->user_id)->value('openid'); //此处要用where,value()用find有BUG $order = Order::query()->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST])->find($order_id); if (!$order) { return $this->error('订单不存在或已支付'); } $config = config('wechat.payment.default'); $config = array_merge($config, [ 'app_id' => 'wxb35ef055a4dd8ad4', 'mch_id' => '1606181693', 'key' => 'lfyyhyz8888888888888888888888888', ]); $app = Factory::payment($config); try { $result = $app->order->unify([ 'body' => $order->title, 'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付 'total_fee' => 1, //TODO 测试暂时注释 round($price * 100), //支付金额单位为分 'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址 'trade_type' => 'JSAPI', 'openid' => $openid, // 'profit_sharing' => 'Y', //Y分账,N不分账,默认不分账,Y大写 ]); } catch (InvalidArgumentException | InvalidConfigException | GuzzleException $e) { return ['error' => $e->getMessage(), 'line' => $e->getLine()]; } if (empty($result['prepay_id'])) { return $result; } $jssdk = $app->jssdk; $payConfig = $jssdk->bridgeConfig($result['prepay_id'], false) + ['id' => $order->id, 'order_no' => $order->order_no]; // 返回数组 return $this->success($payConfig); } }