1
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import logging
|
||||
import os
|
||||
import json
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
# 尝试设置 SSL 证书路径
|
||||
@@ -351,6 +352,23 @@ _wechat_client = None
|
||||
_wechat_mch_id = None
|
||||
|
||||
|
||||
def _parse_wechat_result(result) -> dict:
|
||||
"""解析微信支付SDK返回的result为字典"""
|
||||
data = {}
|
||||
if isinstance(result, dict):
|
||||
data = result
|
||||
elif isinstance(result, str):
|
||||
try:
|
||||
data = json.loads(result)
|
||||
except:
|
||||
pass
|
||||
elif hasattr(result, 'get'):
|
||||
data = result
|
||||
elif hasattr(result, '__dict__'):
|
||||
data = result.__dict__
|
||||
return data
|
||||
|
||||
|
||||
def _get_wechat_client(
|
||||
mch_id: str,
|
||||
private_key: str,
|
||||
@@ -445,10 +463,13 @@ def _create_wechat_order(order: PaymentOrder, db_configs: dict[str, str]) -> str
|
||||
"payer_client_ip": "127.0.0.1",
|
||||
}
|
||||
)
|
||||
result = result or {}
|
||||
if code == 200 and result.get('code_url'):
|
||||
|
||||
data = _parse_wechat_result(result)
|
||||
|
||||
if code == 200 and data.get('code_url'):
|
||||
# 注意:微信返回的 code_url 可能需要进一步处理成二维码图片地址
|
||||
return result.get('code_url')
|
||||
logger.info(f"WeChat order created successfully: order_no={order.order_no}")
|
||||
return data.get('code_url')
|
||||
else:
|
||||
logger.error(
|
||||
f"WeChat pay failed: order_no={order.order_no}, "
|
||||
@@ -519,13 +540,13 @@ async def _query_wechat_order(db: AsyncSession, order: PaymentOrder, db_configs:
|
||||
|
||||
try:
|
||||
code, result = client.query(out_trade_no=order.order_no)
|
||||
result = result or {}
|
||||
if code == 200 and result.get('trade_state'):
|
||||
data = _parse_wechat_result(result)
|
||||
if code == 200 and data.get('trade_state'):
|
||||
logger.info(
|
||||
f"WeChat query succeeded: order_no={order.order_no}, "
|
||||
f"trade_state={result.get('trade_state')}"
|
||||
f"trade_state={data.get('trade_state')}"
|
||||
)
|
||||
return result
|
||||
return data
|
||||
else:
|
||||
logger.error(
|
||||
f"WeChat query failed: order_no={order.order_no}, "
|
||||
@@ -575,11 +596,12 @@ async def _refund_wechat_order(
|
||||
},
|
||||
reason=refund_reason
|
||||
)
|
||||
result = result or {}
|
||||
|
||||
if code == 200 and result.get('status') == 'SUCCESS':
|
||||
data = _parse_wechat_result(result)
|
||||
|
||||
if code == 200 and data.get('status') == 'SUCCESS':
|
||||
logger.info(f"WeChat refund succeeded: order_no={order.order_no}")
|
||||
return {"success": True, "refund_id": result.get('refund_id')}
|
||||
return {"success": True, "refund_id": data.get('refund_id')}
|
||||
else:
|
||||
logger.error(
|
||||
f"WeChat refund failed: order_no={order.order_no}, "
|
||||
@@ -587,7 +609,7 @@ async def _refund_wechat_order(
|
||||
)
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"微信退款失败: code={code}, {result.get('code', '')}"
|
||||
"message": f"微信退款失败: code={code}, {data.get('code', '')}"
|
||||
}
|
||||
except Exception as e:
|
||||
logger.exception(f"WeChat refund exception: order_no={order.order_no}")
|
||||
|
||||
Reference in New Issue
Block a user