From 00f48673143b1393ed2a24469ec4594489d7c529 Mon Sep 17 00:00:00 2001 From: aaron <> Date: Thu, 6 Feb 2025 00:00:05 +0900 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=80=80=E6=AC=BE=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E7=9A=84=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/merchant_order.py | 59 +++++++++++------------------ app/api/endpoints/wechat.py | 38 +++++++++---------- 2 files changed, 42 insertions(+), 55 deletions(-) diff --git a/app/api/endpoints/merchant_order.py b/app/api/endpoints/merchant_order.py index a929326..9490bf7 100644 --- a/app/api/endpoints/merchant_order.py +++ b/app/api/endpoints/merchant_order.py @@ -58,8 +58,7 @@ async def create_merchant_order( message=f"该商品限购{product.purchase_limit}次,您已达到限购次数" ) - original_amount = float(product.sale_price) - pay_amount = original_amount + # 生成订单号和核销码 while True: @@ -73,11 +72,12 @@ async def create_merchant_order( break # 创建订单 + pay_amount = float(product.sale_price) db_order = MerchantOrderDB( order_id=order_id, user_id=current_user.userid, merchant_product_id=order.merchant_product_id, - order_amount=original_amount, + order_amount=pay_amount, pay_amount=pay_amount, gift_points=float(product.sale_price) * (float(product.gift_points_rate) / 100) * settings.POINT_RATIO, status=MerchantOrderStatus.CREATED, @@ -180,13 +180,13 @@ async def verify_order( order.MerchantOrderDB.verify_user_id = current_user.userid order.MerchantOrderDB.status = MerchantOrderStatus.VERIFIED - # 使用账户管理器处理余额变更 + # 对商家进行结算 account_manager = AccountManager(db) settlement_amount = float(order.MerchantProductDB.settlement_amount) account_manager.change_balance( user_id=order.MerchantDB.user_id, amount=settlement_amount, - description=f"{order.MerchantProductDB.name} 订单核销", + description=f"团购券核销", transaction_id=order.MerchantOrderDB.order_id ) @@ -199,33 +199,6 @@ async def verify_order( db.rollback() return error_response(code=500, message=f"核销失败: {str(e)}") -@router.post("/{order_id}/unverify", response_model=ResponseModel) -async def set_order_unverified( - order_id: str, - db: Session = Depends(get_db), - current_user: UserDB = Depends(get_current_user) -): - """设置订单为未核销状态""" - order = db.query(MerchantOrderDB).filter( - MerchantOrderDB.order_id == order_id, - MerchantOrderDB.status == MerchantOrderStatus.CREATED # 只有已下单状态可以设为未核销 - ).first() - - if not order: - return error_response(code=404, message="订单不存在或状态不正确") - - # 更新状态为未核销 - order.status = MerchantOrderStatus.UNVERIFIED - - try: - db.commit() - return success_response( - message="状态更新成功", - data=MerchantOrderInfo.model_validate(order) - ) - except Exception as e: - db.rollback() - return error_response(code=500, message=f"状态更新失败: {str(e)}") @router.post("/{order_id}/refund/apply", response_model=ResponseModel) async def apply_refund( @@ -237,7 +210,7 @@ async def apply_refund( order = db.query(MerchantOrderDB).filter( MerchantOrderDB.order_id == order_id, MerchantOrderDB.user_id == current_user.userid, # 只能申请自己的订单 - MerchantOrderDB.status.in_([MerchantOrderStatus.CREATED, MerchantOrderStatus.UNVERIFIED]) # 只有未核销的订单可以退款 + MerchantOrderDB.status == MerchantOrderStatus.UNVERIFIED # 只有未核销的订单可以退款 ).first() if not order: @@ -276,9 +249,23 @@ async def confirm_refund( return error_response(code=404, message="订单不存在或状态不正确") try: - # 更新状态为已退款 - order.status = MerchantOrderStatus.REFUNDED - db.commit() + ## 如果订单有支付金额,则发起微信退款 + if order.pay_amount > 0: + # 退款 + wechat = WeChatClient() + try: + await wechat.apply_refund( + order_id=order.order_id, + total_amount=int(float(order.pay_amount) * 100), # 转换为分 + reason="用户申请退款" + ) + except Exception as e: + return error_response(code=500, message=f"退款失败: {str(e)}") + else: + # 更新状态为已退款 + order.status = MerchantOrderStatus.REFUNDED + db.commit() + return success_response( message="退款确认成功", data=MerchantOrderInfo.model_validate(order) diff --git a/app/api/endpoints/wechat.py b/app/api/endpoints/wechat.py index 385057c..b6b420d 100644 --- a/app/api/endpoints/wechat.py +++ b/app/api/endpoints/wechat.py @@ -325,16 +325,16 @@ async def refund_notify( # 更新订单状态 order.status = MerchantOrderStatus.REFUNDED - # 扣除积分 - points = int(float(order.pay_amount) * 10) # 按照支付金额计算积分 - point_manager = PointManager(db) - point_manager.deduct_points( - order.user_id, - points, - PointRecordType.CONSUME_RETURN, - f"订单退款,扣除{settings.POINT_ALIAS}", - order.order_id - ) + # 如果订单有赠送积分,则扣除积分 + if order.gift_points > 0: + point_manager = PointManager(db) + point_manager.deduct_points( + order.user_id, + order.gift_points, + PointRecordType.CONSUME_DEDUCT, + f"订单退款,返还积分", + order.order_id + ) elif out_trade_no.startswith('P'): # 商家在线买单 order = db.query(MerchantPayOrderDB).filter( @@ -349,15 +349,15 @@ async def refund_notify( order.status = MerchantPayOrderStatus.REFUNDED # 扣除积分 - points = int(float(order.amount) * 10) # 按照支付金额计算积分 - point_manager = PointManager(db) - point_manager.deduct_points( - order.user_id, - points, - PointRecordType.CONSUME_RETURN, - f"订单退款,扣除{settings.POINT_ALIAS}", - order.order_id - ) + if order.gift_points > 0: + point_manager = PointManager(db) + point_manager.deduct_points( + order.user_id, + order.gift_points, + PointRecordType.CONSUME_DEDUCT, + f"订单退款,返还积分", + order.order_id + ) else: return error_response(code=400, message="不支持的订单类型")