status.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
  2. import { z } from 'zod';
  3. import { AppDataSource } from '@d8d/shared-utils';
  4. import { PaymentService } from '../../services/payment.service.js';
  5. // 支付状态查询路由定义
  6. const paymentStatusRoute = createRoute({
  7. method: 'get',
  8. path: '/',
  9. request: {
  10. query: z.object({
  11. orderId: z.string().transform(val => parseInt(val)).pipe(z.number().int().positive())
  12. })
  13. },
  14. responses: {
  15. 200: {
  16. description: '支付状态查询成功',
  17. content: { 'application/json': { schema: z.object({ paymentStatus: z.string() }) } }
  18. },
  19. 400: {
  20. description: '参数错误',
  21. content: { 'application/json': { schema: z.object({ message: z.string() }) } }
  22. },
  23. 500: {
  24. description: '服务器错误',
  25. content: { 'application/json': { schema: z.object({ message: z.string() }) } }
  26. }
  27. }
  28. });
  29. const app = new OpenAPIHono()
  30. .openapi(paymentStatusRoute, async (c) => {
  31. try {
  32. const { orderId } = c.req.valid('query');
  33. // 创建支付服务实例
  34. const paymentService = new PaymentService(AppDataSource);
  35. const paymentStatus = await paymentService.getPaymentStatus(orderId);
  36. return c.json({
  37. paymentStatus
  38. }, 200);
  39. } catch (error) {
  40. console.error('支付状态查询失败:', error);
  41. return c.json({
  42. message: error instanceof Error ? error.message : '支付状态查询失败'
  43. }, 500);
  44. }
  45. });
  46. export default app;