Prechádzať zdrojové kódy

debug(payment): 添加JSAPI支付全链路调试日志,定位请求参数和微信返回

MyFramework User 3 mesiacov pred
rodič
commit
2d2bc4a290

+ 23 - 1
server/src/modules/payment/payment.controller.ts

@@ -351,7 +351,20 @@ router.post('/wechat/openid', async (ctx: Context) => {
 // 创建微信 JSAPI 支付订单(公众号内支付)
 router.post('/wechat/jsapi', authMiddleware, async (ctx: Context) => {
   const userId = safeParseInt(ctx.state.user.userId);
-  const { planId, productType, quantity, period = 'monthly', openid } = ctx.request.body as {
+  const rawBody = ctx.request.body as any;
+
+  // ===== DEBUG: 完整请求日志 =====
+  console.log('[JSAPI-DEBUG] ========== 收到JSAPI支付请求 ==========');
+  console.log('[JSAPI-DEBUG] userId:', userId);
+  console.log('[JSAPI-DEBUG] body keys:', Object.keys(rawBody));
+  console.log('[JSAPI-DEBUG] body:', JSON.stringify(rawBody, null, 2));
+  console.log('[JSAPI-DEBUG] headers:', JSON.stringify({
+    'content-type': ctx.request.headers['content-type'],
+    'user-agent': ctx.request.headers['user-agent'],
+    'authorization': ctx.request.headers['authorization'] ? '***' : 'none',
+  }));
+
+  const { planId, productType, quantity, period = 'monthly', openid } = rawBody as {
     planId?: number;
     productType?: string;
     quantity?: number;
@@ -359,7 +372,10 @@ router.post('/wechat/jsapi', authMiddleware, async (ctx: Context) => {
     openid: string;
   };
 
+  console.log('[JSAPI-DEBUG] 解析后参数:', JSON.stringify({ planId, productType, quantity, period, openid: openid ? openid.substring(0, 8) + '...' : 'null' }));
+
   if (!openid) {
+    console.log('[JSAPI-DEBUG] ❌ 缺少openid');
     throw new BadRequestError('缺少用户标识(openid),请在微信中打开');
   }
 
@@ -380,6 +396,9 @@ router.post('/wechat/jsapi', authMiddleware, async (ctx: Context) => {
 
     const payParams = await PaymentService.generateWechatJsapiPayment(orderNo, amount, subject, openid);
 
+    console.log('[JSAPI-DEBUG] ✅ Token包支付创建成功, payParams keys:', Object.keys(payParams || {}));
+    console.log('[JSAPI-DEBUG] 返回前端数据:', JSON.stringify({ orderNo, amount, payParams: { ...payParams, paySign: payParams?.paySign?.substring(0, 20) + '...'  } }));
+
     ctx.body = {
       code: 0, message: '订单创建成功',
       data: { orderNo, amount, planName: subject, payParams }
@@ -425,6 +444,9 @@ router.post('/wechat/jsapi', authMiddleware, async (ctx: Context) => {
     openid
   );
 
+  console.log('[JSAPI-DEBUG] ✅ 套餐支付创建成功, payParams keys:', Object.keys(payParams || {}));
+  console.log('[JSAPI-DEBUG] 返回前端数据:', JSON.stringify({ orderNo, amount, planName: plan.name, payParams: { ...payParams, paySign: payParams?.paySign?.substring(0, 20) + '...' } }));
+
   ctx.body = {
     code: 0,
     message: '订单创建成功',

+ 17 - 9
server/src/modules/payment/payment.service.ts

@@ -832,7 +832,6 @@ export async function getWechatOpenid(code: string): Promise<string | null> {
 }
 
 // 生成微信 JSAPI 支付(公众号内支付)
-// 注意:SDK的transactions_jsapi已自动处理签名,result.data直接可用于wx.chooseWXPay
 export async function generateWechatJsapiPayment(
   orderNo: string,
   amount: number,
@@ -854,13 +853,18 @@ export async function generateWechatJsapiPayment(
   }
 
   try {
-    console.log('[WeChat JSAPI] 开始创建 JSAPI 支付订单:', { orderNo, amount, subject, openid });
+    const notifyUrl = process.env.WECHAT_NOTIFY_URL || `${process.env.BASE_URL || 'https://your-domain.com'}/api/payment/wechat/notify`;
+
+    // ===== DEBUG: 打印发送给微信的参数 =====
+    console.log('[JSAPI-DEBUG] ===== 调用微信下单接口 =====');
+    console.log('[JSAPI-DEBUG] 请求参数:', JSON.stringify({
+      description: subject,
+      out_trade_no: orderNo,
+      amount: { total: Math.round(amount * 100), currency: 'CNY' },
+      payer: { openid: openid.substring(0, 8) + '...' },
+      notify_url: notifyUrl,
+    }, null, 2));
 
-    // SDK 的 transactions_jsapi 会自动:
-    // 1. 添加 appid 和 mchid
-    // 2. 调用 /v3/pay/transactions/jsapi
-    // 3. 对 prepay_id 进行 RSA 签名
-    // 4. 返回可直接传给 wx.chooseWXPay 的参数
     const result: any = await wechat.transactions_jsapi({
       description: subject,
       out_trade_no: orderNo,
@@ -871,10 +875,14 @@ export async function generateWechatJsapiPayment(
       payer: {
         openid,
       },
-      notify_url: process.env.WECHAT_NOTIFY_URL || `${process.env.BASE_URL || 'https://your-domain.com'}/api/payment/wechat/notify`,
+      notify_url: notifyUrl,
     });
 
-    console.log('[WeChat JSAPI] 支付接口返回 status:', result.status);
+    // ===== DEBUG: 打印微信返回 =====
+    console.log('[JSAPI-DEBUG] 微信返回 status:', result.status);
+    console.log('[JSAPI-DEBUG] 微信返回 headers:', JSON.stringify(result.headers));
+    console.log('[JSAPI-DEBUG] 微信返回 data keys:', result.data ? Object.keys(result.data) : 'null');
+    console.log('[JSAPI-DEBUG] 微信返回完整 data:', JSON.stringify(result.data));
 
     // 检查返回状态
     if (result.status !== 200 && result.status !== '200') {