fix(pay): 支付校验参数错误以及后台任务
This commit is contained in:
@@ -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}
|
||||
)
|
||||
|
||||
@@ -129,13 +129,13 @@ async def wechat_notify(
|
||||
'status': base_schema.OrderStatusType.OK
|
||||
})
|
||||
result = {'status': 200, 'message': 'OK'}
|
||||
background_tasks.add_task(actions_after_payment, notify_params=params)
|
||||
background_tasks.add_task(actions_after_payment, notify_params=params, db_session=db_session)
|
||||
else:
|
||||
result = {'status': 400, 'message': '支付失败'}
|
||||
record.update({
|
||||
'status': base_schema.OrderStatusType.FAIL,
|
||||
})
|
||||
|
||||
await db_crud.update(data=record, filters={'order_id': record['out_trade_no']})
|
||||
await db_crud.update(data=record, filters={'order_id': record['order_id']})
|
||||
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user