From 72b40e4476157b9cc09ef06f07011ccbf2b70531 Mon Sep 17 00:00:00 2001 From: xingc Date: Fri, 9 May 2025 19:32:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(pay):=20=E6=94=AF=E4=BB=98=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E5=8F=82=E6=95=B0=E9=94=99=E8=AF=AF=E4=BB=A5=E5=8F=8A?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/routers/pay/actions.py | 26 ++++++++++++++++---------- core/routers/pay/routes.py | 4 ++-- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/core/routers/pay/actions.py b/core/routers/pay/actions.py index 5d8424f..13bc912 100644 --- a/core/routers/pay/actions.py +++ b/core/routers/pay/actions.py @@ -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} ) diff --git a/core/routers/pay/routes.py b/core/routers/pay/routes.py index 0a0d05c..029c059 100644 --- a/core/routers/pay/routes.py +++ b/core/routers/pay/routes.py @@ -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