chore/ 删除无用文件
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
支付订单门面:持有 db + 底层 payment 客户端,提供 create_order / 回调 / 查询。
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from app.core.logging import get_logger
|
||||
import time
|
||||
@@ -38,6 +39,7 @@ def _generate_order_no() -> str:
|
||||
|
||||
def _get_legacy_payment_service():
|
||||
from app.features.payment.deps import get_payment_service
|
||||
|
||||
return get_payment_service()
|
||||
|
||||
|
||||
@@ -65,13 +67,20 @@ class PaymentOrderService:
|
||||
if plan.price <= 0:
|
||||
raise HTTPException(status_code=400, detail="免费套餐无需支付")
|
||||
if payment_method not in ("wechat", "alipay"):
|
||||
raise HTTPException(status_code=400, detail="不支持的支付方式,仅支持 wechat / alipay")
|
||||
raise HTTPException(
|
||||
status_code=400, detail="不支持的支付方式,仅支持 wechat / alipay"
|
||||
)
|
||||
|
||||
client = _get_legacy_payment_service()
|
||||
if not client.is_method_available(payment_method):
|
||||
if payment_method == "alipay":
|
||||
raise HTTPException(status_code=503, detail="支付宝支付接口正在开发中,暂时不可用")
|
||||
raise HTTPException(status_code=503, detail=f"{payment_method} 支付暂不可用,请选择其他支付方式")
|
||||
raise HTTPException(
|
||||
status_code=503, detail="支付宝支付接口正在开发中,暂时不可用"
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail=f"{payment_method} 支付暂不可用,请选择其他支付方式",
|
||||
)
|
||||
|
||||
amount_fen = int(plan_price * 100)
|
||||
order_no = _generate_order_no()
|
||||
@@ -100,7 +109,9 @@ class PaymentOrderService:
|
||||
except asyncio.TimeoutError:
|
||||
order.status = "failed"
|
||||
await self._db.flush()
|
||||
raise HTTPException(status_code=504, detail="微信支付初始化超时,请稍后重试。")
|
||||
raise HTTPException(
|
||||
status_code=504, detail="微信支付初始化超时,请稍后重试。"
|
||||
)
|
||||
except Exception as e:
|
||||
order.status = "failed"
|
||||
await self._db.flush()
|
||||
@@ -128,15 +139,24 @@ class PaymentOrderService:
|
||||
except PaymentError as e:
|
||||
order.status = "failed"
|
||||
await self._db.flush()
|
||||
raise HTTPException(status_code=500, detail=f"创建支付订单失败: {e.message}")
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"创建支付订单失败: {e.message}"
|
||||
)
|
||||
except Exception as e:
|
||||
order.status = "failed"
|
||||
await self._db.flush()
|
||||
logger.exception("创建支付订单异常: %s", e)
|
||||
raise HTTPException(status_code=500, detail=f"创建支付订单异常: {type(e).__name__}: {e!s}")
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"创建支付订单异常: {type(e).__name__}: {e!s}"
|
||||
)
|
||||
|
||||
await self._db.commit()
|
||||
logger.info("订单创建成功: order_no=%s, payment_method=%s, amount_fen=%s", order_no, payment_method, amount_fen)
|
||||
logger.info(
|
||||
"订单创建成功: order_no=%s, payment_method=%s, amount_fen=%s",
|
||||
order_no,
|
||||
payment_method,
|
||||
amount_fen,
|
||||
)
|
||||
return CreateOrderResponse(
|
||||
order_id=order_no,
|
||||
payment_method=payment_method,
|
||||
@@ -157,18 +177,29 @@ class PaymentOrderService:
|
||||
order.status = "paid"
|
||||
order.trade_no = trade_no
|
||||
order.paid_at = now
|
||||
user_result = await self._db.execute(select(User).where(User.id == order.user_id))
|
||||
user_result = await self._db.execute(
|
||||
select(User).where(User.id == order.user_id)
|
||||
)
|
||||
user = user_result.scalar_one_or_none()
|
||||
if user:
|
||||
duration_days = SUBSCRIPTION_DURATION_DAYS.get(order.plan_id, 365)
|
||||
if user.subscription_expires_at and user.subscription_expires_at > now:
|
||||
user.subscription_expires_at = user.subscription_expires_at + timedelta(days=duration_days)
|
||||
user.subscription_expires_at = user.subscription_expires_at + timedelta(
|
||||
days=duration_days
|
||||
)
|
||||
else:
|
||||
user.subscription_expires_at = now + timedelta(days=duration_days)
|
||||
user.subscription_type = order.plan_id
|
||||
logger.info("用户 %s 订阅已升级为 %s,到期: %s", user.id, order.plan_id, user.subscription_expires_at)
|
||||
logger.info(
|
||||
"用户 %s 订阅已升级为 %s,到期: %s",
|
||||
user.id,
|
||||
order.plan_id,
|
||||
user.subscription_expires_at,
|
||||
)
|
||||
await self._db.commit()
|
||||
logger.info("支付成功处理完成: 订单 %s, 第三方交易号 %s", out_trade_no, trade_no)
|
||||
logger.info(
|
||||
"支付成功处理完成: 订单 %s, 第三方交易号 %s", out_trade_no, trade_no
|
||||
)
|
||||
|
||||
async def handle_wechat_notify(self, headers: dict, body: str) -> dict:
|
||||
client = _get_legacy_payment_service()
|
||||
@@ -183,14 +214,20 @@ class PaymentOrderService:
|
||||
async def handle_alipay_notify(self, params: dict) -> str:
|
||||
client = _get_legacy_payment_service()
|
||||
notify_result = client.handle_alipay_notify(params=params)
|
||||
if notify_result.success and notify_result.trade_status in ("TRADE_SUCCESS", "TRADE_FINISHED", "SUCCESS"):
|
||||
if notify_result.success and notify_result.trade_status in (
|
||||
"TRADE_SUCCESS",
|
||||
"TRADE_FINISHED",
|
||||
"SUCCESS",
|
||||
):
|
||||
await self.handle_payment_success(
|
||||
notify_result.out_trade_no,
|
||||
notify_result.trade_no,
|
||||
)
|
||||
return "success"
|
||||
|
||||
async def get_order_status(self, order_id: str, user_id: str) -> OrderStatusResponse:
|
||||
async def get_order_status(
|
||||
self, order_id: str, user_id: str
|
||||
) -> OrderStatusResponse:
|
||||
result = await self._db.execute(
|
||||
select(Order).where(Order.id == order_id, Order.user_id == user_id)
|
||||
)
|
||||
@@ -212,7 +249,9 @@ class PaymentOrderService:
|
||||
|
||||
async def list_orders(self, user_id: str) -> list[OrderListResponse]:
|
||||
result = await self._db.execute(
|
||||
select(Order).where(Order.user_id == user_id).order_by(Order.created_at.desc())
|
||||
select(Order)
|
||||
.where(Order.user_id == user_id)
|
||||
.order_by(Order.created_at.desc())
|
||||
)
|
||||
orders = result.scalars().all()
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user