修复退款流程的 bug
This commit is contained in:
parent
ebe74e2f7f
commit
00f4867314
@ -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:
|
||||
## 如果订单有支付金额,则发起微信退款
|
||||
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)
|
||||
|
||||
@ -325,14 +325,14 @@ async def refund_notify(
|
||||
# 更新订单状态
|
||||
order.status = MerchantOrderStatus.REFUNDED
|
||||
|
||||
# 扣除积分
|
||||
points = int(float(order.pay_amount) * 10) # 按照支付金额计算积分
|
||||
# 如果订单有赠送积分,则扣除积分
|
||||
if order.gift_points > 0:
|
||||
point_manager = PointManager(db)
|
||||
point_manager.deduct_points(
|
||||
order.user_id,
|
||||
points,
|
||||
PointRecordType.CONSUME_RETURN,
|
||||
f"订单退款,扣除{settings.POINT_ALIAS}",
|
||||
order.gift_points,
|
||||
PointRecordType.CONSUME_DEDUCT,
|
||||
f"订单退款,返还积分",
|
||||
order.order_id
|
||||
)
|
||||
|
||||
@ -349,13 +349,13 @@ async def refund_notify(
|
||||
order.status = MerchantPayOrderStatus.REFUNDED
|
||||
|
||||
# 扣除积分
|
||||
points = int(float(order.amount) * 10) # 按照支付金额计算积分
|
||||
if order.gift_points > 0:
|
||||
point_manager = PointManager(db)
|
||||
point_manager.deduct_points(
|
||||
order.user_id,
|
||||
points,
|
||||
PointRecordType.CONSUME_RETURN,
|
||||
f"订单退款,扣除{settings.POINT_ALIAS}",
|
||||
order.gift_points,
|
||||
PointRecordType.CONSUME_DEDUCT,
|
||||
f"订单退款,返还积分",
|
||||
order.order_id
|
||||
)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user