1
This commit is contained in:
@@ -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": "微信支付客户端初始化失败"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user