diff --git a/video-gen-admin/src/pages/AdminPaymentConfig.tsx b/video-gen-admin/src/pages/AdminPaymentConfig.tsx index 22a33915..511ae365 100644 --- a/video-gen-admin/src/pages/AdminPaymentConfig.tsx +++ b/video-gen-admin/src/pages/AdminPaymentConfig.tsx @@ -27,6 +27,8 @@ const AdminPaymentConfig: React.FC = () => { wechat_cert_serial_no: map['payment_wechat_cert_serial_no'] || '', wechat_api_v3_key: map['payment_wechat_api_v3_key'] || '', wechat_notify_url: map['payment_wechat_notify_url'] || '', + wechat_public_key: map['payment_wechat_public_key'] || '', + wechat_public_key_id: map['payment_wechat_public_key_id'] || '', alipay_app_id: map['payment_alipay_app_id'] || '', alipay_private_key: map['payment_alipay_private_key'] || '', alipay_public_key: map['payment_alipay_public_key'] || '', @@ -58,6 +60,8 @@ const AdminPaymentConfig: React.FC = () => { payment_wechat_cert_serial_no: values.wechat_cert_serial_no || '', payment_wechat_api_v3_key: values.wechat_api_v3_key || '', payment_wechat_notify_url: values.wechat_notify_url || '', + payment_wechat_public_key: values.wechat_public_key || '', + payment_wechat_public_key_id: values.wechat_public_key_id || '', payment_alipay_enabled: String(alipayEnabled), payment_alipay_app_id: values.alipay_app_id || '', payment_alipay_private_key: values.alipay_private_key || '', @@ -188,6 +192,24 @@ const AdminPaymentConfig: React.FC = () => { + + + + + + diff --git a/video-gen-api/app/api/v1/payments.py b/video-gen-api/app/api/v1/payments.py index d85a6236..2a35cd01 100644 --- a/video-gen-api/app/api/v1/payments.py +++ b/video-gen-api/app/api/v1/payments.py @@ -112,8 +112,11 @@ async def wechat_callback(request: Request, db: AsyncSession = Depends(get_db)): cert_serial_no = db_configs.get("payment_wechat_cert_serial_no", "") api_v3_key = db_configs.get("payment_wechat_api_v3_key", "") appid = db_configs.get("payment_wechat_appid", "") + public_key = db_configs.get("payment_wechat_public_key", "") + public_key_id = db_configs.get("payment_wechat_public_key_id", "") + notify_url = db_configs.get("payment_wechat_notify_url", "") - client = _get_wechat_client(mch_id, private_key, cert_serial_no, api_v3_key, appid) + client = _get_wechat_client(mch_id, private_key, cert_serial_no, api_v3_key, appid, notify_url, public_key, public_key_id) if not client: logger.error("WeChat client not initialized for callback") return {"code": "SUCCESS", "message": "OK"} diff --git a/video-gen-api/app/services/payment.py b/video-gen-api/app/services/payment.py index 2568b0d2..79737a1b 100644 --- a/video-gen-api/app/services/payment.py +++ b/video-gen-api/app/services/payment.py @@ -357,7 +357,9 @@ def _get_wechat_client( cert_serial_no: str, api_v3_key: str, appid: str, - notify_url: str = "" + notify_url: str = "", + public_key: str = None, + public_key_id: str = None ): """Get or create a WeChat Pay client. Recreated if config changes.""" global _wechat_client, _wechat_mch_id @@ -375,28 +377,25 @@ def _get_wechat_client( return None try: - import os - # 创建并确保微信支付证书目录存在 - 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) - logger.info(f"mch_id={mch_id}, cert_serial_no={cert_serial_no}, appid={appid}, notify_url={notify_url}, api_v3_key={api_v3_key}, private_key={private_key}") - # 初始化微信支付客户端,cert_dir=None,让 SDK 在线获取证书 - _wechat_client = WeChatPay( - wechatpay_type=WeChatPayType.NATIVE, - mchid=mch_id, - private_key=private_key.strip(), - cert_serial_no=cert_serial_no, - appid=appid, - apiv3_key=api_v3_key, - notify_url=notify_url, - cert_dir=wechat_cert_dir, # 不缓存证书,每次都从微信服务器获取 - ) + # 初始化微信支付客户端 + wechatpay_args = { + "wechatpay_type": WeChatPayType.NATIVE, + "mchid": mch_id, + "private_key": private_key.strip(), + "cert_serial_no": cert_serial_no, + "appid": appid, + "apiv3_key": api_v3_key, + "notify_url": notify_url, + } + # 如果配置了 public_key 和 public_key_id,就使用它们,否则不设置 + if public_key and public_key_id: + wechatpay_args["public_key"] = public_key + wechatpay_args["public_key_id"] = public_key_id + logger.info("Using configured platform public key") + else: + logger.info("No platform public key configured, will try to download") + + _wechat_client = WeChatPay(**wechatpay_args) _wechat_mch_id = mch_id logger.info("WeChat Pay client initialized successfully") return _wechat_client @@ -424,11 +423,13 @@ def _create_wechat_order(order: PaymentOrder, db_configs: dict[str, str]) -> str api_v3_key = db_configs.get("payment_wechat_api_v3_key", "") appid = db_configs.get("payment_wechat_appid", "") notify_url = db_configs.get("payment_wechat_notify_url", "") + public_key = db_configs.get("payment_wechat_public_key", "") + public_key_id = db_configs.get("payment_wechat_public_key_id", "") if not all([mch_id, private_key, cert_serial_no, api_v3_key, appid]): logger.warning("WeChat payment config missing in database") return None - client = _get_wechat_client(mch_id, private_key, cert_serial_no, api_v3_key, appid, notify_url) + client = _get_wechat_client(mch_id, private_key, cert_serial_no, api_v3_key, appid, notify_url, public_key, public_key_id) if client is None: return None @@ -471,8 +472,11 @@ async def _close_wechat_order(db: AsyncSession, order: PaymentOrder, db_configs: cert_serial_no = db_configs.get("payment_wechat_cert_serial_no", "") api_v3_key = db_configs.get("payment_wechat_api_v3_key", "") appid = db_configs.get("payment_wechat_appid", "") + public_key = db_configs.get("payment_wechat_public_key", "") + public_key_id = db_configs.get("payment_wechat_public_key_id", "") + notify_url = db_configs.get("payment_wechat_notify_url", "") - client = _get_wechat_client(mch_id, private_key, cert_serial_no, api_v3_key, appid) + client = _get_wechat_client(mch_id, private_key, cert_serial_no, api_v3_key, appid, notify_url, public_key, public_key_id) if client is None: return False @@ -503,8 +507,11 @@ async def _query_wechat_order(db: AsyncSession, order: PaymentOrder, db_configs: cert_serial_no = db_configs.get("payment_wechat_cert_serial_no", "") api_v3_key = db_configs.get("payment_wechat_api_v3_key", "") appid = db_configs.get("payment_wechat_appid", "") + public_key = db_configs.get("payment_wechat_public_key", "") + public_key_id = db_configs.get("payment_wechat_public_key_id", "") + notify_url = db_configs.get("payment_wechat_notify_url", "") - client = _get_wechat_client(mch_id, private_key, cert_serial_no, api_v3_key, appid) + client = _get_wechat_client(mch_id, private_key, cert_serial_no, api_v3_key, appid, notify_url, public_key, public_key_id) if client is None: return None @@ -545,8 +552,11 @@ async def _refund_wechat_order( cert_serial_no = db_configs.get("payment_wechat_cert_serial_no", "") api_v3_key = db_configs.get("payment_wechat_api_v3_key", "") appid = db_configs.get("payment_wechat_appid", "") + public_key = db_configs.get("payment_wechat_public_key", "") + public_key_id = db_configs.get("payment_wechat_public_key_id", "") + notify_url = db_configs.get("payment_wechat_notify_url", "") - client = _get_wechat_client(mch_id, private_key, cert_serial_no, api_v3_key, appid) + client = _get_wechat_client(mch_id, private_key, cert_serial_no, api_v3_key, appid, notify_url, public_key, public_key_id) if client is None: return {"success": False, "message": "微信支付客户端初始化失败"}