Просмотр исходного кода

✨ feat(payment): 优化支付流程,从认证用户获取openid

- 移除请求参数中的openid字段,统一从认证用户信息中获取
- 添加用户openid检查,未绑定微信小程序时返回400错误
- 修改PaymentService.createPayment方法,将openid参数设为必填项
yourname 3 месяцев назад
Родитель
Сommit
d58e5d1dd8

+ 11 - 4
packages/server/src/api/payment/create.ts

@@ -9,8 +9,7 @@ import { ErrorSchema } from '../../utils/errorHandler';
 const PaymentCreateSchema = z.object({
   orderId: z.number().int().positive().describe('订单ID'),
   totalAmount: z.number().int().positive().describe('支付金额(分)'),
-  description: z.string().min(1).max(128).describe('支付描述'),
-  openid: z.string().optional().describe('用户OpenID')
+  description: z.string().min(1).max(128).describe('支付描述')
 });
 
 // 支付创建响应Schema
@@ -106,12 +105,20 @@ const app = new OpenAPIHono<AuthContext>()
       // 创建支付服务实例
       const paymentService = new PaymentService();
 
-      // 创建支付订单
+      // 检查用户是否有openid(小程序用户必需)
+      if (!user.openid) {
+        return c.json({
+          code: 400,
+          message: '用户未绑定微信小程序,无法进行支付'
+        }, 400);
+      }
+
+      // 创建支付订单,从认证用户中获取openid
       const paymentResult = await paymentService.createPayment(
         paymentData.orderId,
         paymentData.totalAmount,
         paymentData.description,
-        paymentData.openid
+        user.openid
       );
 
       return c.json(paymentResult, 200);

+ 1 - 1
packages/server/src/modules/payment/payment.service.ts

@@ -53,7 +53,7 @@ export class PaymentService {
     orderId: number,
     totalAmount: number,
     description: string,
-    openid?: string
+    openid: string
   ): Promise<{
     paymentId: string;
     timeStamp: string;