where('group', 'site')->column('value', 'name'); \think\Config::set('site', $site); $upload = include APP_PATH . 'extra/upload.php'; \think\Config::set('upload', $upload); echo "=== 开始执行微信支付接口多场景自动化测试 ===\n"; // 场景 1: 参数缺失/错误场景(不传 payment) echo "\n[测试场景 1] 参数校验:不传 payment\n"; $order = \app\admin\model\shopro\order\Order::get(21); $auth = \app\common\library\Auth::instance(); $auth->direct(20); $request = \think\Request::instance(); $request->post([ 'order_sn' => $order->order_sn, 'payment' => '', ]); $request->header(['platform' => 'WechatMiniProgram']); try { $c = new \addons\shopro\controller\Pay(); $c->prepay(); } catch (\think\exception\HttpResponseException $e) { $res = $e->getResponse()->getData(); echo "预期拦截返回: code={$res['code']}, msg={$res['msg']}\n"; if ($res['code'] !== 0 || strpos($res['msg'], '支付类型') === false) { throw new Exception("场景 1 失败"); } } // 场景 2: 订单不存在场景 echo "\n[测试场景 2] 业务校验:订单号不存在\n"; $request->post([ 'order_sn' => 'NON_EXISTENT_ORDER_SN_999999', 'payment' => 'wechat', ]); try { $c = new \addons\shopro\controller\Pay(); $c->prepay(); } catch (\think\exception\HttpResponseException $e) { $res = $e->getResponse()->getData(); echo "预期拦截返回: code={$res['code']}, msg={$res['msg']}\n"; if ($res['code'] !== 0) { throw new Exception("场景 2 失败"); } } // 场景 3: 正常发起微信支付与参数结构验证 echo "\n[等待 6 秒防连点窗口期后执行场景 3...]\n"; sleep(6); echo "[测试场景 3] 正常微信预支付拉取与返回结构校验\n"; $order = \app\admin\model\shopro\order\Order::get(21); $oldStatus = $order->status; $oldRemainFee = $order->remain_pay_fee; $order->status = 'unpaid'; $order->remain_pay_fee = 0.01; $order->save(); try { $request->post([ 'order_sn' => $order->order_sn, 'payment' => 'wechat', ]); $c = new \addons\shopro\controller\Pay(); $c->prepay(); } catch (\think\exception\HttpResponseException $e) { $res = $e->getResponse()->getData(); echo "返回状态: code={$res['code']}\n"; if ($res['code'] !== 1) { throw new Exception("场景 3 失败: " . json_encode($res)); } $payData = $res['data']['pay_data']; echo "prepay_id: " . $payData['package'] . PHP_EOL; echo "signType: " . $payData['signType'] . PHP_EOL; echo "timeStamp: " . $payData['timeStamp'] . PHP_EOL; if (empty($payData['package']) || empty($payData['paySign']) || $payData['signType'] !== 'RSA') { throw new Exception("场景 3 参数结构验证不通过"); } } finally { $order->status = $oldStatus; $order->remain_pay_fee = $oldRemainFee; $order->save(); } echo "\n=== 全部测试场景验证通过 [SUCCESS] ===\n";