bill_handler.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from redis.asyncio import Redis
  2. from app.api.v1.module_system.auth.schema import AuthSchema
  3. from app.core.logger import log
  4. from app.plugin.module_payment.account.service import AccountService
  5. from ..schemas import ConsumeChangeContent, VoucherChangeContent
  6. from .base_handler import BaseHandler
  7. class BillHandler(BaseHandler[dict]):
  8. """账单变动通知处理器"""
  9. async def handle(self, method: str, content: dict, auth: AuthSchema, redis: Redis) -> bool:
  10. """
  11. 处理账单变动通知
  12. """
  13. try:
  14. notify_data = ConsumeChangeContent(**content)
  15. except Exception as e:
  16. log.error(f"解析账单通知内容失败: {e}")
  17. return False
  18. try:
  19. return await self._process_bill(notify_data, auth)
  20. except Exception as e:
  21. log.error(f"处理账单变动通知异常: {e}")
  22. return False
  23. async def _process_bill(self, data: ConsumeChangeContent, auth: AuthSchema) -> bool:
  24. """处理账单"""
  25. log.info(
  26. f"账单变动: pay_no={data.pay_no}, "
  27. f"enterprise_id={data.enterprise_id}, "
  28. f"consume_amount={data.consume_amount}, "
  29. f"consume_type={data.consume_type}"
  30. )
  31. if data.consume_type == "TRANSFER":
  32. await AccountService.update_transfer_status_service(
  33. auth, data.pay_no, "SUCCESS", data.model_dump(exclude_none=True)
  34. )
  35. return True
  36. async def _handle_trans_change(self, content: dict, auth: AuthSchema) -> bool:
  37. """
  38. 处理转账变动通知
  39. ● 功能说明:转账结果通知,单笔交易账单信息变化时通知。
  40. ○ 通知中"consume_type":"TRANSFER",可以用于判断转账是否成功。
  41. """
  42. return False
  43. class VoucherHandler(BaseHandler[dict]):
  44. """凭证变动通知处理器"""
  45. async def handle(self, method: str, content: dict, auth: AuthSchema, redis: Redis) -> bool:
  46. """
  47. 处理凭证变动通知
  48. """
  49. try:
  50. notify_data = VoucherChangeContent(**content)
  51. except Exception as e:
  52. log.error(f"解析凭证通知内容失败: {e}")
  53. return False
  54. action = notify_data.action
  55. voucher_id = notify_data.voucher_id
  56. log.info(f"处理凭证变动通知: action={action}, voucher_id={voucher_id}")
  57. try:
  58. return await self._process_voucher(notify_data, auth)
  59. except Exception as e:
  60. log.error(f"处理凭证变动通知异常: {e}")
  61. return False
  62. async def _process_voucher(self, data: VoucherChangeContent, auth: AuthSchema) -> bool:
  63. """处理凭证"""
  64. log.info(
  65. f"凭证变动: voucher_id={data.voucher_id}, "
  66. f"enterprise_id={data.enterprise_id}, "
  67. f"action={data.action}"
  68. )
  69. return True