fix(pay): 支付校验参数错误以及后台任务
This commit is contained in:
@@ -10,7 +10,7 @@ from datetime import timedelta, datetime
|
|||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
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.crud import CRUD
|
||||||
from core.db.engine import get_async_session
|
from core.db.engine import get_async_session
|
||||||
from core.db.models import User, Order, Product, PointsTransaction
|
from core.db.models import User, Order, Product, PointsTransaction
|
||||||
@@ -20,7 +20,7 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
async def actions_after_payment(
|
async def actions_after_payment(
|
||||||
notify_params: dict,
|
notify_params: dict,
|
||||||
db_session: AsyncSession = Depends(get_async_session)
|
db_session: AsyncSession
|
||||||
) -> None:
|
) -> None:
|
||||||
try:
|
try:
|
||||||
db_crud = CRUD(db_session, Order)
|
db_crud = CRUD(db_session, Order)
|
||||||
@@ -30,6 +30,10 @@ async def actions_after_payment(
|
|||||||
logger.error(f'Order not found -> {order_id}')
|
logger.error(f'Order not found -> {order_id}')
|
||||||
return
|
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})
|
product: Product = await db_crud.get(model=Product, filters={'id': order.product_id})
|
||||||
if not product:
|
if not product:
|
||||||
logger.warning(f'Product not found -> {order.product_id}')
|
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):
|
async def actions_handler(db_crud: CRUD, product: Product, user: User):
|
||||||
if product.action == ProductCategory.VIP:
|
if product.action == ProductAction.VIP_RENEWAL:
|
||||||
vip_expire_date = (
|
old_vip_expire_date = user.vip_expire_date or datetime.now().date()
|
||||||
user.vip_expire_date or datetime.now()
|
if old_vip_expire_date < datetime.now().date():
|
||||||
) + timedelta(days=product.number)
|
old_vip_expire_date = datetime.now().date()
|
||||||
|
|
||||||
|
vip_expire_date = old_vip_expire_date + timedelta(days=product.number)
|
||||||
await db_crud.update(
|
await db_crud.update(
|
||||||
model=User,
|
model=User,
|
||||||
data={
|
data={
|
||||||
'vip_expire_date': vip_expire_date.date()
|
'vip_expire_date': vip_expire_date
|
||||||
},
|
},
|
||||||
filters={'user_id': user.user_id}
|
filters={'user_id': user.user_id}
|
||||||
)
|
)
|
||||||
elif product.action == ProductCategory.POINTS:
|
elif product.action == ProductAction.RECHARGE_POINTS:
|
||||||
points = (
|
points = (
|
||||||
user.points or 0
|
user.points or 0
|
||||||
) + product.number
|
) + product.number
|
||||||
@@ -84,7 +90,7 @@ async def actions_handler(db_crud: CRUD, product: Product, user: User):
|
|||||||
else:
|
else:
|
||||||
transaction_type = 1
|
transaction_type = 1
|
||||||
|
|
||||||
await db_crud.update(
|
await db_crud.create(
|
||||||
model=PointsTransaction,
|
model=PointsTransaction,
|
||||||
data={
|
data={
|
||||||
'user_id': user.user_id,
|
'user_id': user.user_id,
|
||||||
@@ -92,7 +98,7 @@ async def actions_handler(db_crud: CRUD, product: Product, user: User):
|
|||||||
'points': product.number,
|
'points': product.number,
|
||||||
'product_id': product.id,
|
'product_id': product.id,
|
||||||
'balance': points,
|
'balance': points,
|
||||||
'description': f'{product.name} - {product.number}'
|
'description': f'{product.name} {product.number}'
|
||||||
},
|
},
|
||||||
filters={'id': -1}
|
filters={'id': -1}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -129,13 +129,13 @@ async def wechat_notify(
|
|||||||
'status': base_schema.OrderStatusType.OK
|
'status': base_schema.OrderStatusType.OK
|
||||||
})
|
})
|
||||||
result = {'status': 200, 'message': '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:
|
else:
|
||||||
result = {'status': 400, 'message': '支付失败'}
|
result = {'status': 400, 'message': '支付失败'}
|
||||||
record.update({
|
record.update({
|
||||||
'status': base_schema.OrderStatusType.FAIL,
|
'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
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user