account_handler.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from decimal import Decimal
  2. from typing import Optional
  3. from redis.asyncio import Redis
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from app.core.logger import log
  6. from app.plugin.module_payment.notification.enums import AlipayNotifyMethodEnum
  7. from .base_handler import BaseHandler
  8. from ...points import PointsService
  9. from ...points.schema import PointsChangeSchema
  10. class AccountHandler(BaseHandler):
  11. """资金专户通知处理器"""
  12. async def handle(self, method: str, content: dict, auth: AuthSchema, redis: Redis) -> bool:
  13. """
  14. 处理资金专户相关通知
  15. 支持的通知类型:
  16. - alipay.commerce.ec.fund.change.notify (资金变更通知)
  17. - alipay.commerce.ec.trans.authorize.notify (转账授权通知)
  18. """
  19. log.info(f"开始处理资金专户通知: {method}")
  20. if method == AlipayNotifyMethodEnum.FUND_CHANGE_NOTIFY.value:
  21. return await self._handle_fund_change(content, auth)
  22. elif method == AlipayNotifyMethodEnum.TRANS_AUTHORIZE_NOTIFY.value:
  23. return await self._handle_trans_authorize(content, auth)
  24. log.warning(f"未知的资金专户通知类型: {method}")
  25. return False
  26. async def _handle_fund_change(self, content: dict, auth: AuthSchema) -> bool:
  27. """
  28. 处理资金变更通知
  29. """
  30. try:
  31. # 解析通知内容
  32. # fund_change_info = content.get("fund_change_info", {})
  33. # business_type = fund_change_info.get("business_type")
  34. # fund_status = fund_change_info.get("fund_status")
  35. # amount = fund_change_info.get("amount")
  36. # out_biz_no = fund_change_info.get("out_biz_no")
  37. # enterprise_id = fund_change_info.get("enterprise_id")
  38. # log.info(f"资金变更通知: 业务类型={business_type}, 状态={fund_status}, 金额={amount}")
  39. # # 只处理转账成功的通知
  40. # if business_type == "TRANSFER" and fund_status == "SUCCESS":
  41. # # 扣除积分
  42. # deduct_data = PointsChangeSchema(
  43. # points=Decimal(amount),
  44. # change_type="",
  45. # enterprise_id=enterprise_id,
  46. # business_id=out_biz_no,
  47. # business_type="TRANSFER",
  48. # remark=f"转账扣除积分: {amount}"
  49. # )
  50. # await PointsService.deduct_points_service(auth, deduct_data)
  51. # log.info(f"转账成功,扣除积分: {amount}")
  52. return True
  53. except Exception as e:
  54. log.error(f"处理资金变更通知失败: {e}")
  55. return False
  56. async def _handle_trans_authorize(self, content: dict, auth: AuthSchema) -> bool:
  57. """
  58. 处理转账授权通知
  59. """
  60. try:
  61. # 解析通知内容
  62. authorize_info = content.get("authorize_info", {})
  63. authorize_status = authorize_info.get("authorize_status")
  64. enterprise_id = authorize_info.get("enterprise_id")
  65. log.info(f"转账授权通知: 状态={authorize_status}, 企业ID={enterprise_id}")
  66. # 可以在这里处理授权状态变更
  67. # 例如更新数据库中的授权状态
  68. return True
  69. except Exception as e:
  70. log.error(f"处理转账授权通知失败: {e}")
  71. return False