修复退款流程的 bug
This commit is contained in:
parent
ebe74e2f7f
commit
00f4867314
@ -58,8 +58,7 @@ async def create_merchant_order(
|
|||||||
message=f"该商品限购{product.purchase_limit}次,您已达到限购次数"
|
message=f"该商品限购{product.purchase_limit}次,您已达到限购次数"
|
||||||
)
|
)
|
||||||
|
|
||||||
original_amount = float(product.sale_price)
|
|
||||||
pay_amount = original_amount
|
|
||||||
|
|
||||||
# 生成订单号和核销码
|
# 生成订单号和核销码
|
||||||
while True:
|
while True:
|
||||||
@ -73,11 +72,12 @@ async def create_merchant_order(
|
|||||||
break
|
break
|
||||||
|
|
||||||
# 创建订单
|
# 创建订单
|
||||||
|
pay_amount = float(product.sale_price)
|
||||||
db_order = MerchantOrderDB(
|
db_order = MerchantOrderDB(
|
||||||
order_id=order_id,
|
order_id=order_id,
|
||||||
user_id=current_user.userid,
|
user_id=current_user.userid,
|
||||||
merchant_product_id=order.merchant_product_id,
|
merchant_product_id=order.merchant_product_id,
|
||||||
order_amount=original_amount,
|
order_amount=pay_amount,
|
||||||
pay_amount=pay_amount,
|
pay_amount=pay_amount,
|
||||||
gift_points=float(product.sale_price) * (float(product.gift_points_rate) / 100) * settings.POINT_RATIO,
|
gift_points=float(product.sale_price) * (float(product.gift_points_rate) / 100) * settings.POINT_RATIO,
|
||||||
status=MerchantOrderStatus.CREATED,
|
status=MerchantOrderStatus.CREATED,
|
||||||
@ -180,13 +180,13 @@ async def verify_order(
|
|||||||
order.MerchantOrderDB.verify_user_id = current_user.userid
|
order.MerchantOrderDB.verify_user_id = current_user.userid
|
||||||
order.MerchantOrderDB.status = MerchantOrderStatus.VERIFIED
|
order.MerchantOrderDB.status = MerchantOrderStatus.VERIFIED
|
||||||
|
|
||||||
# 使用账户管理器处理余额变更
|
# 对商家进行结算
|
||||||
account_manager = AccountManager(db)
|
account_manager = AccountManager(db)
|
||||||
settlement_amount = float(order.MerchantProductDB.settlement_amount)
|
settlement_amount = float(order.MerchantProductDB.settlement_amount)
|
||||||
account_manager.change_balance(
|
account_manager.change_balance(
|
||||||
user_id=order.MerchantDB.user_id,
|
user_id=order.MerchantDB.user_id,
|
||||||
amount=settlement_amount,
|
amount=settlement_amount,
|
||||||
description=f"{order.MerchantProductDB.name} 订单核销",
|
description=f"团购券核销",
|
||||||
transaction_id=order.MerchantOrderDB.order_id
|
transaction_id=order.MerchantOrderDB.order_id
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -199,33 +199,6 @@ async def verify_order(
|
|||||||
db.rollback()
|
db.rollback()
|
||||||
return error_response(code=500, message=f"核销失败: {str(e)}")
|
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)
|
@router.post("/{order_id}/refund/apply", response_model=ResponseModel)
|
||||||
async def apply_refund(
|
async def apply_refund(
|
||||||
@ -237,7 +210,7 @@ async def apply_refund(
|
|||||||
order = db.query(MerchantOrderDB).filter(
|
order = db.query(MerchantOrderDB).filter(
|
||||||
MerchantOrderDB.order_id == order_id,
|
MerchantOrderDB.order_id == order_id,
|
||||||
MerchantOrderDB.user_id == current_user.userid, # 只能申请自己的订单
|
MerchantOrderDB.user_id == current_user.userid, # 只能申请自己的订单
|
||||||
MerchantOrderDB.status.in_([MerchantOrderStatus.CREATED, MerchantOrderStatus.UNVERIFIED]) # 只有未核销的订单可以退款
|
MerchantOrderDB.status == MerchantOrderStatus.UNVERIFIED # 只有未核销的订单可以退款
|
||||||
).first()
|
).first()
|
||||||
|
|
||||||
if not order:
|
if not order:
|
||||||
@ -276,9 +249,23 @@ async def confirm_refund(
|
|||||||
return error_response(code=404, message="订单不存在或状态不正确")
|
return error_response(code=404, message="订单不存在或状态不正确")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 更新状态为已退款
|
## 如果订单有支付金额,则发起微信退款
|
||||||
order.status = MerchantOrderStatus.REFUNDED
|
if order.pay_amount > 0:
|
||||||
db.commit()
|
# 退款
|
||||||
|
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(
|
return success_response(
|
||||||
message="退款确认成功",
|
message="退款确认成功",
|
||||||
data=MerchantOrderInfo.model_validate(order)
|
data=MerchantOrderInfo.model_validate(order)
|
||||||
|
|||||||
@ -325,16 +325,16 @@ async def refund_notify(
|
|||||||
# 更新订单状态
|
# 更新订单状态
|
||||||
order.status = MerchantOrderStatus.REFUNDED
|
order.status = MerchantOrderStatus.REFUNDED
|
||||||
|
|
||||||
# 扣除积分
|
# 如果订单有赠送积分,则扣除积分
|
||||||
points = int(float(order.pay_amount) * 10) # 按照支付金额计算积分
|
if order.gift_points > 0:
|
||||||
point_manager = PointManager(db)
|
point_manager = PointManager(db)
|
||||||
point_manager.deduct_points(
|
point_manager.deduct_points(
|
||||||
order.user_id,
|
order.user_id,
|
||||||
points,
|
order.gift_points,
|
||||||
PointRecordType.CONSUME_RETURN,
|
PointRecordType.CONSUME_DEDUCT,
|
||||||
f"订单退款,扣除{settings.POINT_ALIAS}",
|
f"订单退款,返还积分",
|
||||||
order.order_id
|
order.order_id
|
||||||
)
|
)
|
||||||
|
|
||||||
elif out_trade_no.startswith('P'): # 商家在线买单
|
elif out_trade_no.startswith('P'): # 商家在线买单
|
||||||
order = db.query(MerchantPayOrderDB).filter(
|
order = db.query(MerchantPayOrderDB).filter(
|
||||||
@ -349,15 +349,15 @@ async def refund_notify(
|
|||||||
order.status = MerchantPayOrderStatus.REFUNDED
|
order.status = MerchantPayOrderStatus.REFUNDED
|
||||||
|
|
||||||
# 扣除积分
|
# 扣除积分
|
||||||
points = int(float(order.amount) * 10) # 按照支付金额计算积分
|
if order.gift_points > 0:
|
||||||
point_manager = PointManager(db)
|
point_manager = PointManager(db)
|
||||||
point_manager.deduct_points(
|
point_manager.deduct_points(
|
||||||
order.user_id,
|
order.user_id,
|
||||||
points,
|
order.gift_points,
|
||||||
PointRecordType.CONSUME_RETURN,
|
PointRecordType.CONSUME_DEDUCT,
|
||||||
f"订单退款,扣除{settings.POINT_ALIAS}",
|
f"订单退款,返还积分",
|
||||||
order.order_id
|
order.order_id
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return error_response(code=400, message="不支持的订单类型")
|
return error_response(code=400, message="不支持的订单类型")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user