bill_handler.py 3.0 KB

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