2026-03-18 17:18:23 +08:00
|
|
|
|
"""
|
|
|
|
|
|
统一支付服务门面(从 payment 迁入 app)
|
|
|
|
|
|
"""
|
2026-03-19 14:36:14 +08:00
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from typing import Dict, Optional
|
|
|
|
|
|
|
2026-04-08 15:37:09 +08:00
|
|
|
|
from app.core.logging import get_logger
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from app.features.payment.alipay_client import AlipayClient
|
2026-04-08 15:37:09 +08:00
|
|
|
|
from app.features.payment.payment_config import PaymentConfig
|
|
|
|
|
|
from app.features.payment.payment_exceptions import PaymentError
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from app.features.payment.schemas import NotifyResult, PaymentResult, PaymentStatus
|
2026-04-08 15:37:09 +08:00
|
|
|
|
from app.features.payment.wechat_client import WeChatPayClient
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-03-19 14:36:14 +08:00
|
|
|
|
def handle_wechat_notify(self, headers: Dict[str, str], body: str) -> NotifyResult:
|
2026-03-18 17:18:23 +08:00
|
|
|
|
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",
|
|
|
|
|
|
)
|