feat(pay): 支付后事件处理
This commit is contained in:
69
core/routers/pay/actions.py
Normal file
69
core/routers/pay/actions.py
Normal file
@@ -0,0 +1,69 @@
|
||||
# -*- coding = utf-8 -*-
|
||||
# @Time : 2025/2/27 上午12:15
|
||||
# @File : actions.py
|
||||
# @Software : PyCharm
|
||||
# @Author : xingc
|
||||
# @Desc :
|
||||
import logging
|
||||
from datetime import timedelta, datetime
|
||||
|
||||
from fastapi import Depends
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from core.base.schema import ProductCategory
|
||||
from core.db.crud import CRUD
|
||||
from core.db.engine import get_async_session
|
||||
from core.db.models import User, Order, Product
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def actions_after_payment(
|
||||
notify_params: dict,
|
||||
db_session: AsyncSession = Depends(get_async_session)
|
||||
) -> None:
|
||||
try:
|
||||
db_crud = CRUD(db_session, Order)
|
||||
order_id = notify_params['out_trade_no']
|
||||
order: Order = await db_crud.get(filters={'order_id': order_id})
|
||||
if not order:
|
||||
logger.error(f'Order not found -> {order_id}')
|
||||
return
|
||||
|
||||
product: Product = await db_crud.get(model=Product, filters={'product_id': order.product_id})
|
||||
if not product:
|
||||
logger.warning(f'Product not found -> {order.product_id}')
|
||||
return
|
||||
|
||||
user: User = await db_crud.get(model=User, filters={'user_id': order.user_id})
|
||||
if not user:
|
||||
logger.warning(f'User not found -> {order.order_id}')
|
||||
return
|
||||
|
||||
if product.action == ProductCategory.VIP:
|
||||
vip_expire_date = (
|
||||
user.vip_expire_date or datetime.now()
|
||||
) + timedelta(days=product.number)
|
||||
await db_crud.update(
|
||||
model=User,
|
||||
data={
|
||||
'vip_expire_date': vip_expire_date
|
||||
},
|
||||
filters={'user_id': order.user_id}
|
||||
)
|
||||
elif product.action == ProductCategory.POINTS:
|
||||
points = (
|
||||
user.points or 0
|
||||
) + product.number
|
||||
await db_crud.update(
|
||||
model=User,
|
||||
data={
|
||||
'points': points
|
||||
},
|
||||
filters={'user_id': order.user_id}
|
||||
)
|
||||
else:
|
||||
logger.error(f'Unknown action -> {product.action}')
|
||||
except Exception as e:
|
||||
logger.error(f'Payment Actions Exception occurred -> {e}')
|
||||
logger.exception(e)
|
||||
Reference in New Issue
Block a user