import logging import time from yuxi.channel.extensions.alipay.gateway import AlipayGateway from yuxi.channel.extensions.alipay.types import AlipayAccount, AlipayMode, AlipayProbeResult logger = logging.getLogger(__name__) class AlipayStatus: def __init__(self, gateway: AlipayGateway): self._gateway = gateway async def probe(self, account: AlipayAccount) -> AlipayProbeResult: start = time.monotonic() try: result = await self._gateway.request( account=account, method="alipay.open.public.life.account.get", ) latency_ms = (time.monotonic() - start) * 1000 return AlipayProbeResult( ok=True, app_id=account.app_id, name=account.name or result.get("name", ""), mode=account.mode, latency_ms=latency_ms, ) except Exception as e: latency_ms = (time.monotonic() - start) * 1000 return AlipayProbeResult( ok=False, app_id=account.app_id, name=account.name, mode=account.mode, latency_ms=latency_ms, error=str(e), ) def build_summary(self, account: AlipayAccount | None = None) -> dict: if not account: return {"status": "unconfigured"} configured = account.is_configured() return { "status": "configured" if configured else "unconfigured", "app_id": account.app_id, "name": account.name or account.app_id, "mode": account.mode.value, "dm_policy": account.dm_policy.value, "enabled": account.enabled, } async def check_ready(self, account: AlipayAccount | None = None) -> bool: if not account: return False if not account.is_configured(): return False if not account.enabled: return False result = await self.probe(account) return result.ok def collect_status_issues(self, account: AlipayAccount | None = None) -> list[str]: issues = [] if not account: issues.append("未配置支付宝账户") return issues if not account.app_id: issues.append("未设置 AppId") if not account.app_private_key: issues.append("未设置应用私钥") if not account.alipay_public_key: issues.append("未设置支付宝公钥") if account.mode == AlipayMode.SANDBOX: issues.append("当前使用沙箱环境") return issues