Parcourir la source

在支付路由中新增身份验证中间件,更新支付服务逻辑以从系统配置中获取价格,优化单片购买和VIP订单的价格获取方式,确保价格配置的灵活性和准确性。

wuyi il y a 3 mois
Parent
commit
804a3c519e
2 fichiers modifiés avec 21 ajouts et 18 suppressions
  1. 2 0
      src/routes/payment.routes.ts
  2. 19 18
      src/services/payment.service.ts

+ 2 - 0
src/routes/payment.routes.ts

@@ -16,12 +16,14 @@ export default async function paymentRoutes(fastify: FastifyInstance) {
   // 创建会员购买订单
   fastify.post<{ Body: CreatePaymentOrderParams }>(
     '/vip/create',
+    { onRequest: [authenticate] },
     paymentController.createPaymentOrder.bind(paymentController)
   )
 
   // 创建单片购买订单
   fastify.post<{ Body: CreateSinglePaymentOrderParams }>(
     '/single/create',
+    { onRequest: [authenticate] },
     paymentController.createSinglePaymentOrder.bind(paymentController)
   )
 

+ 19 - 18
src/services/payment.service.ts

@@ -22,6 +22,7 @@ import { PaginationResponse } from '../dto/common.dto'
 import { MemberService } from './member.service'
 import { UserService } from './user.service'
 import { TeamService } from './team.service'
+import { SysConfigService } from './sys-config.service'
 import { VipLevel } from '../entities/member.entity'
 
 export class PaymentService {
@@ -33,6 +34,7 @@ export class PaymentService {
   private memberService: MemberService
   private userService: UserService
   private teamService: TeamService
+  private sysConfigService: SysConfigService
 
   private static readonly ORDER_TYPE_TO_VIP_LEVEL_MAP: Record<OrderType, VipLevel> = {
     [OrderType.SINGLE_TIP]: VipLevel.FREE,
@@ -65,6 +67,7 @@ export class PaymentService {
     this.memberService = new MemberService(app)
     this.userService = new UserService(app)
     this.teamService = new TeamService(app)
+    this.sysConfigService = new SysConfigService(app)
   }
 
   private generateSign(params: Record<string, any>, key: string): string {
@@ -156,22 +159,16 @@ export class PaymentService {
       if (!user) {
         throw new Error('用户不存在')
       }
-      const vipPrices: Record<string, string> = {
-        single: '0.10',
-        hourly: '1.00',
-        daily: '5.00',
-        weekly: '20.00',
-        monthly: '50.00',
-        quarterly: '120.00',
-        yearly: '400.00',
-        lifetime: '1000.00'
-      }
+      // 从团队配置中获取价格
+      const configs = await this.sysConfigService.getUserTeamConfigs(params.userId)
+      const config = configs.find((c: any) => c.name === params.type)
 
-      const price = vipPrices[params.type]
-      if (!price) {
-        throw new Error('不支持的订单类型')
+      if (!config) {
+        throw new Error(`价格错误`)
       }
 
+      const price = config.value
+
       const out_trade_no = `${Date.now()}${params.userId}${randomInt(0, 1e4).toString().padStart(4, '0')}`
 
       const paymentParams: CreatePaymentParams = {
@@ -184,10 +181,7 @@ export class PaymentService {
         money: price,
         client_ip: params.ip
       }
-      console.log('paymentParams:', paymentParams)
-
       const result = await this.createOrder(paymentParams)
-      console.log('result:', result)
 
       // 创建收入记录
       try {
@@ -248,8 +242,15 @@ export class PaymentService {
         throw new Error('用户不存在')
       }
 
-      // 单片购买固定价格
-      const price = '10.00'
+      // 从团队配置中获取单片购买价格
+      const configs = await this.sysConfigService.getUserTeamConfigs(params.userId)
+      const config = configs.find((c: any) => c.name === 'single')
+
+      if (!config) {
+        throw new Error('价格错误')
+      }
+
+      const price = config.value
       const out_trade_no = `${Date.now()}${params.userId}${params.resourceId}${randomInt(0, 1e4)
         .toString()
         .padStart(4, '0')}`