This commit is contained in:
@@ -24,6 +24,8 @@ import json
|
|||||||
import os
|
import os
|
||||||
import base64
|
import base64
|
||||||
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
||||||
|
from cryptography.hazmat.primitives import hashes, serialization
|
||||||
|
from cryptography.hazmat.primitives.asymmetric import padding
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
import requests
|
import requests
|
||||||
import random
|
import random
|
||||||
@@ -127,6 +129,8 @@ def get_wechat_pay_client(pay_type=WeChatPayType.NATIVE, appid=None, config=None
|
|||||||
notify_url=notify_url,
|
notify_url=notify_url,
|
||||||
cert_dir=cert_dir
|
cert_dir=cert_dir
|
||||||
)
|
)
|
||||||
|
# 保存私钥内容以便后续手动签名使用
|
||||||
|
wxpay._private_key_content = private_key
|
||||||
return wxpay, None
|
return wxpay, None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return None, str(e)
|
return None, str(e)
|
||||||
@@ -769,12 +773,26 @@ class OrderViewSet(viewsets.ModelViewSet):
|
|||||||
# 签名串格式:appId\ntimeStamp\nnonceStr\npackage\n
|
# 签名串格式:appId\ntimeStamp\nnonceStr\npackage\n
|
||||||
message_build = f"{miniprogram_appid}\n{timestamp}\n{nonce_str}\n{package}\n"
|
message_build = f"{miniprogram_appid}\n{timestamp}\n{nonce_str}\n{package}\n"
|
||||||
|
|
||||||
# 使用商户私钥签名
|
print(f"待签名字符串:\n{repr(message_build)}")
|
||||||
# 注意:WeChatPayV3 对象的私钥属性名可能随版本变化,或者被封装
|
|
||||||
# 这里我们不直接访问私钥,而是利用 SDK 提供的 sign 方法
|
|
||||||
|
|
||||||
# 实际上 wechatpayv3 库提供了 sign 方法
|
# 手动签名
|
||||||
signature = wxpay.sign(message_build)
|
from cryptography.hazmat.backends import default_backend
|
||||||
|
|
||||||
|
private_key_obj = serialization.load_pem_private_key(
|
||||||
|
wxpay._private_key_content.encode('utf-8'),
|
||||||
|
password=None,
|
||||||
|
backend=default_backend()
|
||||||
|
)
|
||||||
|
|
||||||
|
signature = base64.b64encode(
|
||||||
|
private_key_obj.sign(
|
||||||
|
message_build.encode('utf-8'),
|
||||||
|
padding.PKCS1v15(),
|
||||||
|
hashes.SHA256()
|
||||||
|
)
|
||||||
|
).decode('utf-8')
|
||||||
|
|
||||||
|
print(f"生成的签名: {signature}")
|
||||||
|
|
||||||
return Response({
|
return Response({
|
||||||
'timeStamp': timestamp,
|
'timeStamp': timestamp,
|
||||||
|
|||||||
Reference in New Issue
Block a user