This commit is contained in:
aaron 2025-04-05 21:40:08 +08:00
parent a5fd2647e3
commit 5f12f60b45
4 changed files with 24 additions and 12 deletions

View File

@ -247,7 +247,8 @@ async def merchant_cancel_order(
wechat = WeChatClient()
await wechat.apply_refund(
order_id=order.order_id,
total_amount=int(float(order.pay_amount) * 100) if not settings.DEBUG else 1, # 转换为分
total_amount=int(float(order.pay_amount) * 100) if not settings.DEBUG else 1, # 转换为分,订单总金额
refund_amount=int(float(order.pay_amount) * 100) if not settings.DEBUG else 1, # 转换为分,退款金额
reason="商家取消订单"
)
return success_response(data=MerchantOrderInfo.model_validate(order))
@ -278,7 +279,8 @@ async def user_cancel_order(
wechat = WeChatClient()
await wechat.apply_refund(
order_id=order.order_id,
total_amount=int(float(order.pay_amount) * 100) if not settings.DEBUG else 1, # 转换为分
total_amount=int(float(order.pay_amount) * 100) if not settings.DEBUG else 1, # 转换为分,订单总金额
refund_amount=int(float(order.pay_amount) * 100) if not settings.DEBUG else 1, # 转换为分,退款金额
reason="用户取消订单"
)
return success_response(data=MerchantOrderInfo.model_validate(order))
@ -554,7 +556,8 @@ async def apply_refund(
wechat = WeChatClient()
await wechat.apply_refund(
order_id=order.order_id,
total_amount=int(float(order.pay_amount) * 100) if not settings.DEBUG else 1, # 转换为分
total_amount=int(float(order.pay_amount) * 100) if not settings.DEBUG else 1, # 转换为分,订单总金额
refund_amount=int(float(order.pay_amount) * 100) if not settings.DEBUG else 1, # 转换为分,退款金额
reason="用户申请退款"
)

View File

@ -912,7 +912,8 @@ async def cancel_order(
wechat = WeChatClient()
await wechat.apply_refund(
order_id=order.orderid,
total_amount=int(float(order.final_amount) * 100) if not settings.DEBUG else 1, # 转换为分
total_amount=int(float(order.final_amount) * 100) if not settings.DEBUG else 1, # 订单总金额(分)
refund_amount=int(float(order.final_amount) * 100) if not settings.DEBUG else 1, # 退款金额(分)
reason="用户取消订单"
)
@ -1174,7 +1175,8 @@ async def deliveryman_cancel_order(
wechat = WeChatClient()
await wechat.apply_refund(
order_id=order.orderid,
total_amount=int(float(order.final_amount) * 100) if not settings.DEBUG else 1, # 转换为分
total_amount=int(float(order.final_amount) * 100) if not settings.DEBUG else 1, # 订单总金额(分)
refund_amount=int(float(order.final_amount) * 100) if not settings.DEBUG else 1, # 退款金额(分)
reason="配送员取消订单"
)
@ -1616,8 +1618,9 @@ async def refund_order_amount(
wechat = WeChatClient()
await wechat.apply_refund(
order_id=order.orderid,
total_amount=int(float(request.amount) * 100) if not settings.DEBUG else 1, # 转换为分
reason="后台退款"
total_amount=int(float(order.final_amount + request.amount) * 100) if not settings.DEBUG else 1, # 原订单总金额(分)
refund_amount=int(float(request.amount) * 100) if not settings.DEBUG else 1, # 本次退款金额(分)
reason=request.reason or "后台退款"
)
db.commit()

View File

@ -370,6 +370,7 @@ class WeChatClient:
self,
order_id: str,
total_amount: int,
refund_amount: int = None,
reason: str = "用户申请退款"
) -> dict:
"""
@ -377,14 +378,19 @@ class WeChatClient:
Args:
order_id: 订单号(同时作为退款单号)
total_amount: 退款金额()
total_amount: 订单总金额()
refund_amount: 退款金额()默认为None时等于total_amount全额退款
reason: 退款原因
Returns:
dict: 退款结果
"""
try:
print(f"开始申请退款: order_id={order_id}, amount={total_amount}")
# 如果未指定退款金额,默认全额退款
if refund_amount is None:
refund_amount = total_amount
print(f"开始申请退款: order_id={order_id}, total_amount={total_amount}, refund_amount={refund_amount}")
# 生成随机字符串
nonce_str = generate_random_string()
timestamp = str(int(time.time()))
@ -396,17 +402,17 @@ class WeChatClient:
# 构建请求体
body = {
"out_trade_no": order_id, # 商户订单号,二选一
"out_refund_no": f"refund_{order_id}", # 商户退款单号
"out_refund_no": f"refund_{order_id}_{int(time.time())}", # 商户退款单号,加入时间戳避免重复
"reason": reason,
"notify_url": f"{settings.API_BASE_URL}/api/wechat/refund/notify",
"amount": {
"refund": total_amount, # 退款金额
"refund": refund_amount, # 退款金额
"total": total_amount, # 原订单金额
"currency": "CNY"
}
}
# 生成签名
# 生成签名
nonce_str, timestamp, signature = self.sign_message("POST", url_path, body)
# 构建认证头

Binary file not shown.