diff --git a/video-gen-api/app/services/payment.py b/video-gen-api/app/services/payment.py index 43c232cc..49d6cf67 100644 --- a/video-gen-api/app/services/payment.py +++ b/video-gen-api/app/services/payment.py @@ -357,8 +357,7 @@ def _get_wechat_client( cert_serial_no: str, api_v3_key: str, appid: str, - notify_url: str = "", - cert_dir: str | None = None + notify_url: str = "" ): """Get or create a WeChat Pay client. Recreated if config changes.""" global _wechat_client, _wechat_mch_id @@ -368,6 +367,7 @@ def _get_wechat_client( try: from wechatpayv3 import WeChatPay, WeChatPayType + import os except ImportError: logger.error( "wechatpayv3 is not installed. " @@ -376,8 +376,15 @@ def _get_wechat_client( return None 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( @@ -388,13 +395,18 @@ def _get_wechat_client( appid=appid, apiv3_key=api_v3_key, notify_url=notify_url, - cert_dir=cert_dir, + cert_dir=wechat_cert_dir, ) _wechat_mch_id = mch_id logger.info("WeChat Pay client initialized successfully") return _wechat_client 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_mch_id = None return None