account_handler.py 3.6 KB

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