This commit is contained in:
@@ -41,12 +41,16 @@ def patched_request(self, *args, **kwargs):
|
||||
return original_request(self, *args, **kwargs)
|
||||
Core.request = patched_request
|
||||
|
||||
def get_wechat_pay_client(pay_type=WeChatPayType.NATIVE):
|
||||
def get_wechat_pay_client(pay_type=WeChatPayType.NATIVE, appid=None, config=None):
|
||||
"""
|
||||
获取微信支付 V3 客户端实例的辅助函数
|
||||
"""
|
||||
print(f"正在获取微信支付配置...")
|
||||
wechat_config = WeChatPayConfig.objects.filter(is_active=True).first()
|
||||
|
||||
wechat_config = config
|
||||
if not wechat_config:
|
||||
wechat_config = WeChatPayConfig.objects.filter(is_active=True).first()
|
||||
|
||||
if not wechat_config:
|
||||
print("错误: 数据库中没有激活的 WeChatPayConfig")
|
||||
return None, "支付配置未找到"
|
||||
@@ -55,7 +59,13 @@ def get_wechat_pay_client(pay_type=WeChatPayType.NATIVE):
|
||||
|
||||
# 1. 严格清理所有配置项的空格和换行符
|
||||
mch_id = str(wechat_config.mch_id).strip()
|
||||
appid = str(wechat_config.app_id).strip()
|
||||
|
||||
# 如果传入了 appid,优先使用传入的
|
||||
if not appid:
|
||||
appid = str(wechat_config.app_id).strip()
|
||||
else:
|
||||
appid = str(appid).strip()
|
||||
|
||||
apiv3_key = str(wechat_config.apiv3_key).strip()
|
||||
serial_no = str(wechat_config.mch_cert_serial_no).strip()
|
||||
notify_url = str(wechat_config.notify_url).strip()
|
||||
@@ -694,12 +704,23 @@ class OrderViewSet(viewsets.ModelViewSet):
|
||||
order.wechat_user = user
|
||||
order.save()
|
||||
|
||||
wechat_config = WeChatPayConfig.objects.filter(is_active=True).first()
|
||||
# 小程序 AppID
|
||||
miniprogram_appid = 'wxdf2ca73e6c0929f0'
|
||||
|
||||
# 尝试查找特定配置
|
||||
wechat_config = WeChatPayConfig.objects.filter(app_id=miniprogram_appid).first()
|
||||
if not wechat_config:
|
||||
wechat_config = WeChatPayConfig.objects.filter(is_active=True).first()
|
||||
|
||||
if not wechat_config:
|
||||
return Response({'error': '支付系统维护中'}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
|
||||
|
||||
# 初始化支付客户端
|
||||
wxpay, error_msg = get_wechat_pay_client(pay_type=WeChatPayType.JSAPI)
|
||||
# 初始化支付客户端,强制使用小程序 AppID
|
||||
wxpay, error_msg = get_wechat_pay_client(
|
||||
pay_type=WeChatPayType.JSAPI,
|
||||
appid=miniprogram_appid,
|
||||
config=wechat_config
|
||||
)
|
||||
if not wxpay:
|
||||
return Response({'error': error_msg}, status=500)
|
||||
|
||||
@@ -746,7 +767,7 @@ class OrderViewSet(viewsets.ModelViewSet):
|
||||
# 再次签名 (小程序端需要的签名)
|
||||
# 注意:WeChatPayV3 SDK 可能没有直接提供生成小程序签名的 helper,需手动计算
|
||||
# 签名串格式:appId\ntimeStamp\nnonceStr\npackage\n
|
||||
message_build = f"{wechat_config.app_id}\n{timestamp}\n{nonce_str}\n{package}\n"
|
||||
message_build = f"{miniprogram_appid}\n{timestamp}\n{nonce_str}\n{package}\n"
|
||||
|
||||
# 使用商户私钥签名
|
||||
# 注意:WeChatPayV3 对象的私钥属性名可能随版本变化,或者被封装
|
||||
|
||||
Reference in New Issue
Block a user