Files
life-echo/api/app/features/payment/payment_facade.py
2026-03-19 14:36:40 +08:00

93 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
统一支付服务门面(从 payment 迁入 app
"""
from app.core.logging import get_logger
from typing import Dict, Optional
from app.features.payment.payment_config import PaymentConfig
from app.features.payment.wechat_client import WeChatPayClient
from app.features.payment.alipay_client import AlipayClient
from app.features.payment.schemas import NotifyResult, PaymentResult, PaymentStatus
from app.features.payment.payment_exceptions import PaymentError, PaymentConfigError
logger = get_logger(__name__)
PAYMENT_METHOD_WECHAT = "wechat"
PAYMENT_METHOD_ALIPAY = "alipay"
SUPPORTED_METHODS = {PAYMENT_METHOD_WECHAT, PAYMENT_METHOD_ALIPAY}
class PaymentService:
def __init__(self, config: PaymentConfig):
self._config = config
self._wechat_client: Optional[WeChatPayClient] = None
self._alipay_client: Optional[AlipayClient] = None
@property
def wechat_client(self) -> WeChatPayClient:
if self._wechat_client is None:
self._wechat_client = WeChatPayClient(self._config.wechat)
return self._wechat_client
@property
def alipay_client(self) -> AlipayClient:
if self._alipay_client is None:
self._alipay_client = AlipayClient(self._config.alipay)
return self._alipay_client
def create_payment(
self,
method: str,
out_trade_no: str,
total_amount: int,
description: str,
) -> PaymentResult:
self._validate_method(method)
if method == PAYMENT_METHOD_WECHAT:
return self.wechat_client.create_app_order(
out_trade_no=out_trade_no,
total_amount=total_amount,
description=description,
)
return self.alipay_client.create_app_order(
out_trade_no=out_trade_no,
total_amount=total_amount,
subject=description,
)
def handle_wechat_notify(self, headers: Dict[str, str], body: str) -> NotifyResult:
return self.wechat_client.verify_notify(headers=headers, body=body)
def handle_alipay_notify(self, params: Dict[str, str]) -> NotifyResult:
return self.alipay_client.verify_notify(params=params)
def query_payment(self, method: str, out_trade_no: str) -> PaymentStatus:
self._validate_method(method)
if method == PAYMENT_METHOD_WECHAT:
return self.wechat_client.query_order(out_trade_no=out_trade_no)
return self.alipay_client.query_order(out_trade_no=out_trade_no)
def close_payment(self, method: str, out_trade_no: str) -> bool:
self._validate_method(method)
if method == PAYMENT_METHOD_WECHAT:
return self.wechat_client.close_order(out_trade_no=out_trade_no)
return self.alipay_client.close_order(out_trade_no=out_trade_no)
def is_method_available(self, method: str) -> bool:
if method == PAYMENT_METHOD_WECHAT:
return self._config.wechat.is_configured
if method == PAYMENT_METHOD_ALIPAY:
if getattr(self._config, "alipay_under_development", True):
return False
return self._config.alipay.is_configured
return False
@staticmethod
def _validate_method(method: str):
if method not in SUPPORTED_METHODS:
raise PaymentError(
f"不支持的支付方式: {method}",
code="UNSUPPORTED_METHOD",
)