微信支付 V3 接口的加密和验签需要使用证书进行操作,以下是示例代码:

加密:

import requests
import time
import json
import hashlib
import base64
import OpenSSL.crypto

# 读取证书
with open('apiclient_cert.pem', 'r') as f:
    cert = f.read()
with open('apiclient_key.pem', 'r') as f:
    key = f.read()

# 构建请求头
mchid = '商户号'
serial_no = '证书序列号'
nonce_str = str(int(time.time() * 1000))
body = {
    'mchid': mchid,
    'nonce_str': nonce_str,
    'sign_type': 'HMAC-SHA256'
}
body_json = json.dumps(body)

# 构建签名串
message = body_json + '\n' + 'https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi'
message = message.encode('utf-8')

# 生成签名
key = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, key)
sign = OpenSSL.crypto.sign(key, message, 'sha256')
sign = base64.b64encode(sign).decode('utf-8')

# 构建请求体
headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': 'WECHATPAY2-SHA256-RSA2048 '
                     'mchid="{mchid}", '
                     'serial_no="{serial_no}", '
                     'nonce_str="{nonce_str}", '
                     'timestamp="{timestamp}", '
                     'signature="{signature}"'.format(
        mchid=mchid,
        serial_no=serial_no,
        nonce_str=nonce_str,
        timestamp=str(int(time.time())),
        signature=sign
    )
}

url = 'https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi'
response = requests.post(url=url, headers=headers, data=body_json)
print(response.json())

验签:

import base64
import hashlib
import json
import OpenSSL.crypto


def verify_wechatpay_notify(signature, timestamp, nonce, body):
    with open('wechatpay_public.pem', 'r') as f:
        public_key = f.read()

    message = str(timestamp) + '\n' + str(nonce) + '\n' + body + '\n'
    message = message.encode('utf-8')

    signature = base64.b64decode(signature)
    pub_key = OpenSSL.crypto.load_publickey(OpenSSL.crypto.FILETYPE_PEM, public_key)
    result = OpenSSL.crypto.verify(pub_key, signature, message, 'sha256')

    return result == None


# 示例
timestamp = '1625862182'
nonce = '1234567890'
body = '{"id":"xxx","create_time":"xxxxx","resource_type":"crypto","event_type":"TRANSACTION.SUCCESS","summary":"支付成功","resource":{"algorithm":"AEAD_AES_256_GCM","original_type":"transaction","ciphertext":"xxxxx","nonce":"xxxx","associated_data":"transaction","plaintext":"xxxxx"},"mchid":"xxxx","appid":"xxxx","out_trade_no":"xxxx","transaction_id":"xxxx","trade_type":"JSAPI","trade_state":"SUCCESS","trade_state_desc":"支付成功","bank_type":"ICBC_DEBIT","attach":"附加数据","success_time":"xxxxx","payer":{"openid":"xxxx"},"amount":{"total":1,"payer_total":1,"currency":"CNY","payer_currency":"CNY"},"promotion_detail":[{"promotion_id":"xxxx","scope":"GLOBAL","type":"xxxx","amount":1}]}'
signature = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

result = verify_wechatpay_notify(signature, timestamp, nonce, body)
print(result)
帮我写一个微信支付v3接口的加密和验签

原文地址: https://www.cveoy.top/t/topic/OMu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录