This commit is contained in:
2026-06-11 18:43:05 +08:00
parent d5729e828d
commit f2b4d7972e
+18 -6
View File
@@ -357,8 +357,7 @@ def _get_wechat_client(
cert_serial_no: str, cert_serial_no: str,
api_v3_key: str, api_v3_key: str,
appid: str, appid: str,
notify_url: str = "", notify_url: str = ""
cert_dir: str | None = None
): ):
"""Get or create a WeChat Pay client. Recreated if config changes.""" """Get or create a WeChat Pay client. Recreated if config changes."""
global _wechat_client, _wechat_mch_id global _wechat_client, _wechat_mch_id
@@ -368,6 +367,7 @@ def _get_wechat_client(
try: try:
from wechatpayv3 import WeChatPay, WeChatPayType from wechatpayv3 import WeChatPay, WeChatPayType
import os
except ImportError: except ImportError:
logger.error( logger.error(
"wechatpayv3 is not installed. " "wechatpayv3 is not installed. "
@@ -376,8 +376,15 @@ def _get_wechat_client(
return None return None
try: try:
# 直接传入原始私钥,让 SDK 自己处理格式化 # 创建并确保微信支付证书目录存在
# SDK 内部已有 format_private_key 函数处理各种格式 wechat_cert_dir = os.path.abspath(os.path.join(
os.path.dirname(os.path.dirname(__file__)),
"..",
"storage",
"wechat_certs"
))
os.makedirs(wechat_cert_dir, exist_ok=True)
logger.info("WeChat Pay platform certificates will be stored in: %s", wechat_cert_dir)
# 初始化微信支付客户端 # 初始化微信支付客户端
_wechat_client = WeChatPay( _wechat_client = WeChatPay(
@@ -388,13 +395,18 @@ def _get_wechat_client(
appid=appid, appid=appid,
apiv3_key=api_v3_key, apiv3_key=api_v3_key,
notify_url=notify_url, notify_url=notify_url,
cert_dir=cert_dir, cert_dir=wechat_cert_dir,
) )
_wechat_mch_id = mch_id _wechat_mch_id = mch_id
logger.info("WeChat Pay client initialized successfully") logger.info("WeChat Pay client initialized successfully")
return _wechat_client return _wechat_client
except Exception as e: except Exception as e:
logger.exception(f"Failed to initialize WeChat Pay client: {e}") logger.exception(
"Failed to initialize WeChat Pay client: %s\n"
"Please double-check your merchant private key configuration.\n"
"It should be from apiclient_key.pem, NOT apiclient_cert.pem!",
e
)
_wechat_client = None _wechat_client = None
_wechat_mch_id = None _wechat_mch_id = None
return None return None