fix(pay): 支付校验参数错误以及后台任务

This commit is contained in:
xingc
2025-05-09 19:32:49 +08:00
parent f7eff392b0
commit 72b40e4476
2 changed files with 18 additions and 12 deletions

View File

@@ -10,7 +10,7 @@ from datetime import timedelta, datetime
from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession
from core.base.schema import ProductCategory
from core.base.schema import ProductCategory, ProductAction, OrderStatusType
from core.db.crud import CRUD
from core.db.engine import get_async_session
from core.db.models import User, Order, Product, PointsTransaction
@@ -20,7 +20,7 @@ logger = logging.getLogger(__name__)
async def actions_after_payment(
notify_params: dict,
db_session: AsyncSession = Depends(get_async_session)
db_session: AsyncSession
) -> None:
try:
db_crud = CRUD(db_session, Order)
@@ -30,6 +30,10 @@ async def actions_after_payment(
logger.error(f'Order not found -> {order_id}')
return
if order.status != OrderStatusType.WAIT_PAYMENT:
logger.error(f'Order not wait_payment -> {order_id}')
return
product: Product = await db_crud.get(model=Product, filters={'id': order.product_id})
if not product:
logger.warning(f'Product not found -> {order.product_id}')
@@ -56,18 +60,20 @@ async def actions_after_payment(
async def actions_handler(db_crud: CRUD, product: Product, user: User):
if product.action == ProductCategory.VIP:
vip_expire_date = (
user.vip_expire_date or datetime.now()
) + timedelta(days=product.number)
if product.action == ProductAction.VIP_RENEWAL:
old_vip_expire_date = user.vip_expire_date or datetime.now().date()
if old_vip_expire_date < datetime.now().date():
old_vip_expire_date = datetime.now().date()
vip_expire_date = old_vip_expire_date + timedelta(days=product.number)
await db_crud.update(
model=User,
data={
'vip_expire_date': vip_expire_date.date()
'vip_expire_date': vip_expire_date
},
filters={'user_id': user.user_id}
)
elif product.action == ProductCategory.POINTS:
elif product.action == ProductAction.RECHARGE_POINTS:
points = (
user.points or 0
) + product.number
@@ -84,7 +90,7 @@ async def actions_handler(db_crud: CRUD, product: Product, user: User):
else:
transaction_type = 1
await db_crud.update(
await db_crud.create(
model=PointsTransaction,
data={
'user_id': user.user_id,
@@ -92,7 +98,7 @@ async def actions_handler(db_crud: CRUD, product: Product, user: User):
'points': product.number,
'product_id': product.id,
'balance': points,
'description': f'{product.name} - {product.number}'
'description': f'{product.name} {product.number}'
},
filters={'id': -1}
)