处理退款逻辑

This commit is contained in:
aaron 2025-02-15 18:49:52 +08:00
parent 02732fc606
commit 223a2c6e21
2 changed files with 50 additions and 37 deletions

View File

@ -325,10 +325,10 @@ async def refund_notify(
# 获取退款信息 # 获取退款信息
out_trade_no = data.get("out_trade_no") # 订单号 out_trade_no = data.get("out_trade_no") # 订单号
refund_status = data.get("refund_status") status = data.get("status")
success_time = data.get("success_time") success_time = data.get("success_time")
if not all([out_trade_no, refund_status]) or refund_status != "SUCCESS": if not all([out_trade_no, status]) or status != "SUCCESS":
return error_response(code=400, message="缺少必要的退款信息或退款未成功") return error_response(code=400, message="缺少必要的退款信息或退款未成功")
# 根据订单类型处理退款 # 根据订单类型处理退款
@ -343,6 +343,8 @@ async def refund_notify(
# 更新订单状态 # 更新订单状态
order.status = MerchantOrderStatus.REFUNDED order.status = MerchantOrderStatus.REFUNDED
order.refund_transaction_id = data.get("transaction_id")
order.refund_time = datetime.fromisoformat(success_time.replace('Z', '+00:00'))
# 如果订单有赠送积分,则扣除积分 # 如果订单有赠送积分,则扣除积分
if order.gift_points > 0: if order.gift_points > 0:

View File

@ -385,13 +385,20 @@ class WeChatClient:
Returns: Returns:
dict: 退款结果 dict: 退款结果
""" """
try:
print(f"开始申请退款: order_id={order_id}, amount={total_amount}")
# 生成随机字符串
nonce_str = generate_random_string()
timestamp = str(int(time.time()))
# 构建请求URL
url_path = "/v3/refund/domestic/refunds" url_path = "/v3/refund/domestic/refunds"
api_url = f"https://api.mch.weixin.qq.com{url_path}" api_url = f"https://api.mch.weixin.qq.com{url_path}"
# 构建请求数据 # 构建请求体
body = { body = {
"out_trade_no": order_id, # 原订单号 "out_trade_no": order_id, # 商户订单号,二选一
"out_refund_no": order_id, # 退款单号使用原订单号 "out_refund_no": f"refund_{order_id}", # 商户退款单号
"reason": reason, "reason": reason,
"notify_url": f"{settings.API_BASE_URL}/api/wechat/refund/notify", "notify_url": f"{settings.API_BASE_URL}/api/wechat/refund/notify",
"amount": { "amount": {
@ -402,9 +409,7 @@ class WeChatClient:
} }
# 生成签名 # 生成签名
nonce_str = str(uuid.uuid4()).replace('-', '') nonce_str, timestamp, signature = self.sign_message("POST", url_path, body)
timestamp = str(int(time.time()))
signature = self.sign_message("POST", url_path, body)
# 构建认证头 # 构建认证头
headers = { headers = {
@ -420,10 +425,15 @@ class WeChatClient:
) )
} }
try: # 打印签名相关信息
print(f"签名字符串: {nonce_str}, {timestamp}, {signature}")
print(f"Authorization: {headers['Authorization']}")
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.post(api_url, json=body, headers=headers) as response: async with session.post(api_url, json=body, headers=headers) as response:
print(f"退款响应状态码: {response.status}")
result = await response.json() result = await response.json()
print(f"退款响应内容: {result}")
if response.status != 200: if response.status != 200:
raise Exception(f"退款申请失败: {result.get('message')}") raise Exception(f"退款申请失败: {result.get('message')}")
@ -435,6 +445,7 @@ class WeChatClient:
return result return result
except Exception as e: except Exception as e:
print(f"申请退款异常: {str(e)}")
raise Exception(f"申请退款失败: {str(e)}") raise Exception(f"申请退款失败: {str(e)}")
async def send_subscribe_message( async def send_subscribe_message(