Files
life-echo/api/app/features/payment/router.py
Kevin a3f61fcc0f feat(api+app): 对话阶段化、回忆录流水线与客户端会话体验
- DB: segments 用户输入文本(Alembic 0002)
- Chat: 阶段检测/阶段提示/回复限制,编排与访谈/画像 prompts 调整
- Memoir: 忠实度检查 agent,叙事与分类等链路更新
- Core: agent 日志、Alembic 启动、LangChain/日志/配置等
- Story: time_hints;Memory 检索与相关测试
- Expo: 助手头像、会话页与消息拆分、实时会话与文案/i18n
- Docs/scripts/tests: 迁移脚本、LLM JSON/记忆检索文档、新增单测
2026-03-26 12:13:36 +08:00

102 lines
3.3 KiB
Python

from app.core.logging import get_logger
from typing import List
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import PlainTextResponse
from app.core.dependencies import get_current_user
from app.features.payment.deps import get_payment_order_service
from app.features.payment.order_service import PaymentOrderService
from app.features.payment.schemas import (
CreateOrderRequest,
CreateOrderResponse,
OrderListResponse,
OrderStatusResponse,
)
from app.features.plan.deps import get_plan_service
from app.features.plan.service import PlanService
from app.features.user.models import User
logger = get_logger(__name__)
router = APIRouter(
prefix="/api/payment",
tags=["payment"],
responses={
401: {"description": "认证失败"},
404: {"description": "订单不存在"},
503: {"description": "支付服务暂不可用"},
},
)
@router.post("/create-order", response_model=CreateOrderResponse)
async def create_order(
request: CreateOrderRequest,
current_user: User = Depends(get_current_user),
service: PaymentOrderService = Depends(get_payment_order_service),
plan_service: PlanService = Depends(get_plan_service),
):
plan = next(
(p for p in plan_service.get_plans_for_api() if p.id == request.plan_id), None
)
if plan is None:
raise HTTPException(status_code=400, detail="无效的套餐 ID")
return await service.create_order(
user_id=current_user.id,
user_subscription_type=current_user.subscription_type,
plan_id=plan.id,
plan_display_name=plan.display_name,
plan_price=plan.price,
plan_currency=plan.currency,
payment_method=request.payment_method,
)
@router.post("/notify/wechat", include_in_schema=False)
async def wechat_notify(
request: Request,
service: PaymentOrderService = Depends(get_payment_order_service),
):
try:
headers = dict(request.headers)
body = await request.body()
return await service.handle_wechat_notify(
headers=headers, body=body.decode("utf-8")
)
except Exception as e:
logger.exception("微信支付回调处理失败: {}", e)
return {"code": "FAIL", "message": str(e)}
@router.post("/notify/alipay", include_in_schema=False)
async def alipay_notify(
request: Request,
service: PaymentOrderService = Depends(get_payment_order_service),
):
try:
form_data = await request.form()
params = {key: value for key, value in form_data.items()}
result = await service.handle_alipay_notify(params=params)
return PlainTextResponse(result)
except Exception as e:
logger.exception("支付宝回调处理失败: {}", e)
return PlainTextResponse("fail")
@router.get("/order/{order_id}/status", response_model=OrderStatusResponse)
async def get_order_status(
order_id: str,
current_user: User = Depends(get_current_user),
service: PaymentOrderService = Depends(get_payment_order_service),
):
return await service.get_order_status(order_id, current_user.id)
@router.get("/orders", response_model=List[OrderListResponse])
async def list_orders(
current_user: User = Depends(get_current_user),
service: PaymentOrderService = Depends(get_payment_order_service),
):
return await service.list_orders(current_user.id)