callback.mt.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
  2. import { z } from 'zod';
  3. import { AppDataSource } from '@d8d/shared-utils';
  4. import { PaymentMtService } from '../../services/payment.mt.service.js';
  5. // 支付回调路由定义 - 多租户版本
  6. const paymentCallbackRoute = createRoute({
  7. method: 'post',
  8. path: '/',
  9. request: {
  10. body: {
  11. content: {
  12. 'text/plain': { schema: z.string() }
  13. }
  14. }
  15. },
  16. responses: {
  17. 200: {
  18. description: '回调处理成功',
  19. content: { 'text/plain': { schema: z.string() } }
  20. },
  21. 400: {
  22. description: '回调数据错误',
  23. content: { 'application/json': { schema: z.object({ message: z.string() }) } }
  24. },
  25. 500: {
  26. description: '服务器错误',
  27. content: { 'text/plain': { schema: z.string() } }
  28. }
  29. }
  30. });
  31. const app = new OpenAPIHono()
  32. .openapi(paymentCallbackRoute, async (c) => {
  33. try {
  34. // 获取原始请求体(用于签名验证)
  35. const rawBody = await c.req.text();
  36. console.log('原始请求体', rawBody)
  37. // 解析回调数据
  38. const callbackData = JSON.parse(rawBody);
  39. // 获取微信支付回调头信息
  40. const headers = {
  41. 'wechatpay-timestamp': c.req.header('wechatpay-timestamp') || '',
  42. 'wechatpay-nonce': c.req.header('wechatpay-nonce') || '',
  43. 'wechatpay-signature': c.req.header('wechatpay-signature') || '',
  44. 'wechatpay-serial': c.req.header('wechatpay-serial') || ''
  45. };
  46. // 创建支付服务实例
  47. const paymentService = new PaymentMtService(AppDataSource);
  48. // 处理支付回调
  49. await paymentService.handlePaymentCallback(callbackData, headers, rawBody);
  50. // 返回成功响应给微信支付
  51. return c.text('SUCCESS', 200);
  52. } catch (error) {
  53. console.error('支付回调处理失败:', error);
  54. // 返回失败响应给微信支付
  55. return c.text('FAIL', 500);
  56. }
  57. });
  58. export default app;