增加支付宝支付相关页面和程序
This commit is contained in:
@@ -1,13 +1,22 @@
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
logger = logging.getLogger("videogen")
|
||||
|
||||
from app.dependencies import get_db, get_current_user
|
||||
from app.models.user import User
|
||||
from app.models.payment_order import PaymentOrder
|
||||
from app.models.recharge_package import RechargePackage
|
||||
from app.schemas.payment import RechargeRequest, PaymentOrderOut
|
||||
from app.services.payment import create_recharge_order, verify_wechat_callback, verify_alipay_callback, process_payment_success
|
||||
from app.services.payment import (
|
||||
create_recharge_order,
|
||||
verify_wechat_callback,
|
||||
verify_alipay_callback,
|
||||
process_payment_success_by_order_no,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/payments", tags=["payments"])
|
||||
|
||||
@@ -18,6 +27,9 @@ async def recharge(
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
if req.method not in ("wechat", "alipay"):
|
||||
raise HTTPException(status_code=400, detail="不支持的支付方式")
|
||||
|
||||
result = await db.execute(
|
||||
select(RechargePackage).where(
|
||||
RechargePackage.id == req.plan,
|
||||
@@ -35,6 +47,7 @@ async def recharge(
|
||||
price=pkg.price,
|
||||
label=pkg.name,
|
||||
bonus_credits=pkg.bonus_credits,
|
||||
method=req.method,
|
||||
)
|
||||
return order
|
||||
|
||||
@@ -42,30 +55,35 @@ async def recharge(
|
||||
@router.post("/wechat/callback")
|
||||
async def wechat_callback(request: Request, db: AsyncSession = Depends(get_db)):
|
||||
data = await request.json()
|
||||
if not await verify_wechat_callback(data):
|
||||
if not await verify_wechat_callback(data, db):
|
||||
raise HTTPException(status_code=400, detail="签名验证失败")
|
||||
order_no = data.get("out_trade_no")
|
||||
result = await db.execute(
|
||||
select(PaymentOrder).where(PaymentOrder.order_no == order_no).limit(1)
|
||||
)
|
||||
order = result.scalar_one_or_none()
|
||||
if order:
|
||||
await process_payment_success(db, order.id)
|
||||
if order_no:
|
||||
await process_payment_success_by_order_no(db, order_no)
|
||||
return {"code": "SUCCESS", "message": "OK"}
|
||||
|
||||
|
||||
@router.post("/alipay/callback")
|
||||
async def alipay_callback(request: Request, db: AsyncSession = Depends(get_db)):
|
||||
data = await request.form()
|
||||
if not await verify_alipay_callback(dict(data)):
|
||||
form_data = await request.form()
|
||||
data = dict(form_data)
|
||||
logger.info(f"Alipay callback received: {list(data.keys())}")
|
||||
|
||||
# Verify signature first
|
||||
if not await verify_alipay_callback(data, db):
|
||||
raise HTTPException(status_code=400, detail="签名验证失败")
|
||||
|
||||
# Check trade_status – only "TRADE_SUCCESS" and "TRADE_FINISHED" mean paid
|
||||
trade_status = data.get("trade_status", "")
|
||||
if trade_status not in ("TRADE_SUCCESS", "TRADE_FINISHED"):
|
||||
logger.info(f"Alipay callback trade_status={trade_status}, ignoring")
|
||||
return "success"
|
||||
|
||||
order_no = data.get("out_trade_no")
|
||||
result = await db.execute(
|
||||
select(PaymentOrder).where(PaymentOrder.order_no == order_no).limit(1)
|
||||
)
|
||||
order = result.scalar_one_or_none()
|
||||
if order:
|
||||
await process_payment_success(db, order.id)
|
||||
trade_no = data.get("trade_no", "")
|
||||
if order_no:
|
||||
await process_payment_success_by_order_no(db, order_no, trade_no)
|
||||
|
||||
return "success"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user