@H_403_5@v3支付
# from Crypto.PublicKey import RSA# import random# from Crypto.Signature import PKCS1_v1_5# from Crypto.Hash import SHA256# import base64# import timeimport timeimport randomfrom Cryptodome.PublicKey import RSAfrom Cryptodome.Signature import pkcs1_15 as PKCS1_v1_5from Cryptodome.Hash import SHA256import base64class Authorization(object): def __init__(self): self.mchID = '1431536202' self.serial_no = '证书序列号' # 生成欲签名字符串 def sign_str(self, method, url_path, timestamp, nonce_str, request_body): if request_body: # POST sign_List = [ method, url_path, timestamp, nonce_str, request_body ] return '\n'.join(sign_List) + '\n' else: # GET sign_List = [ method, url_path, timestamp, nonce_str ] return '\n'.join(sign_List) + '\n\n' # 生成随机字符串 def getNonceStr(self): data = "123456789zxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP" nonce_str = ''.join(random.sample(data, 30)) return nonce_str # 生成签名 def sign(self, sign_str): with open(r'./rsa_private_key.pem', 'r')as f: # 这里要注意的秘钥只能有三行 # -----BEGIN PRIVATE KEY----- # ******************秘钥只能在一行,不能换行***************** # -----END PRIVATE KEY----- private_key = f.read() f.close() pkey = RSA.importKey(private_key) h = SHA256.new(sign_str.encode('utf-8')) signature = PKCS1_v1_5.new(pkey).sign(h) sign = base64.b64encode(signature).decode() return sign # 生成 Authorization def authorization(self, method, url_path, nonce_str, timestamp, body=None): # 加密子串 signstr = self.sign_str(method=method, url_path=url_path, timestamp=timestamp, nonce_str=nonce_str, request_body=body) # print("加密原子串:" + signstr) # 加密后子串 s = self.sign(signstr) # print("加密后子串:" + s) authorization = 'WECHATPAY2-SHA256-RSA2048 ' \ 'mchID="{mchID}",' \ 'nonce_str="{nonce_str}",' \ 'signature="{sign}",' \ 'timestamp="{timestamp}",' \ 'serial_no="{serial_no}"'. \ format(mchID=self.mchID, nonce_str=nonce_str, sign=s, timestamp=timestamp, serial_no=self.serial_no ) return authorizationif __name__ == '__main__': method = "POST" url_path = "/v3/pay/transactions/JsAPI" timestamp = str(int(time.time())) nonce_str = Authorization().getNoncestr() body = "{'appID': '*********', 'mchID': '**********', 'description': '爱奇艺周卡', 'out_Trade_no': 'LY1111111111', 'notify_url': '******', 'amount': {'total': 100, 'currency': 'CNY'}, 'payer': {'openID': '***************'}}" authorization = Authorization().authorization(method=method, url_path=url_path, nonce_str=nonce_str, timestamp=timestamp) print(authorization) import requests def to_Json(kwargs): import Json return Json.dumps(kwargs, separators=(',', ':')) def requests_wx3(data, authorization, key_path="", cert_path=""): """ APIV3 版本的微信支付请求 """ headers = {'Content-Type': 'application/Json', 'Accept': 'application/Json', 'Authorization': authorization} response = requests.post(url, data=to_Json(data), headers=headers, cert=(cert_path, key_path)) print(response.text) print(requests_wx3(url='https://API.mch.weixin.qq.com/v3/pay/transactions/app', ))
@H_403_5@安卓支付
import timeimport randomfrom Cryptodome.PublicKey import RSAfrom Cryptodome.Signature import pkcs1_15from Cryptodome.Hash import SHA256from base64 import b64encodemchID = '1431536202'serial_no = '001'with open(r'./rsa_private_key.pem', 'r')as f: private_key = f.read()# private_key = "-----BEGIN PRIVATE KEY-----\n" \# "NIIEvgIBADANBgkqhkiG9w0B3QEFAA5CBKgwggSkAgEAAoIBAQC2l/hfX5g6qjo7" \# "BcqTa+LldQpd7m4ZZEKT2BxUpYPNJ/YoZO1c3qB/+CfQQlKJYPSEOsTnUeuAZ2AG" \# "z2LWuh/36gmiFX2Nw1pI0AkVwec8pB1+IqLGHzRsAaDJ++2J88TcWOuM5lOcBr6I" \# "VJOyl1ghYB4Dnuk2uID46rAZwWL3L2FZ+ktfdtePt4zOQrpILRuDNzR6TOcd1JER" \# "ylt8IDyoVTr69ALnQdhoNnofnrBK3R4sY5ON1BR01DpZM8yX8mmb6yoljp1VoP+5" \# "n8pCPVAqZJEbwfDH90naeMZLYfoCvdaJhxHLblviZN7v4VW6Fx/W43O9sRTNPukU" \# "W5pLBP57AgMBAAECggEBAI9U6iZLzx62A7HTUPq6ZMkEQB2OEyUhe9W8fjjAGJ9R" \# "8DWzRdRx+gGaVf54IX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qmksLCif8GrwlfJ" \# "Vrf3uyXTyY1QVRAGy9UIiM0RnsWDaGcRgNaz1K9YV3gqm7qHe2y8fzB0PLLw2TzY" \# "GUwxIttKpnEVlX1M59nmGFmQq08YNC+JRHZrvmmB0MyEBSA9lg0ugASdQQC5PGYa" \# "Rnn7p8VlUEauVmfpF6BTVZLQ\n" \# "-----END PRIVATE KEY-----"timestamp = str(int(time.time()))nonce_str = str(random.randint(100000, 10000000))def sign_str(method, url_path, request_body): """ 生成欲签名字符串 """ sign_List = [ method, url_path, timestamp, nonce_str, request_body ] return '\n'.join(sign_List) + '\n'def sign(sign_str): """ 生成签名 """ rsa_key = RSA.importKey(private_key) signer = pkcs1_15.new(rsa_key) digest = SHA256.new(sign_str.encode('utf8')) sign = b64encode(signer.sign(digest)).decode('utf8') return signdef authorization(method, url_path, request_body): """ 生成Authorization """ signstr = sign_str(method, url_path, request_body) s = sign(signstr) authorization = 'WECHATPAY2-SHA256-RSA2048 ' \ 'mchID="{mchID}",' \ 'nonce_str="{nonce_str}",' \ 'signature="{sign}",' \ 'timestamp="{timestamp}",' \ 'serial_no="{serial_no}"'.\ format(mchID=mchID, nonce_str=nonce_str, sign=s, timestamp=timestamp, serial_no=serial_no ) return authorizationif __name__ == '__main__': print(authorization('POST', '/v3/marketing/favor/users/openID/coupons', '{"stock_ID":"123","stock_creator_mchID":"1302430101","out_request_no":"20190522_001","appID":"your appID"}')) # Authorization WECHATPAY2-SHA256-RSA2048 mchID="1312030806",nonce_str="f0wwnSIuQN8yDr0U4bYKNmUgalcMUCLM",sig # nature="dcFTPfaAewd+UXuXv+VA+KeGW1coUG68PtklWtsMiFgal5GxiljGUVGV60gBnIo2La1R3cxf7mOb62q7xoab9mP1SZ5dP8L+amQ # 9vyl+ZYTaJOg31vtkDWMU0ILNqy96SuKy+5/Q2NSCQU0fBLMWU11vbSoA2ycEsCjDEknc8Hiw+vyKkV6iGyUNBMizfwZhJWdRcWDWeyxAy # 0rsaZKVOpeEyJ2xPQnLX8uB+gqCIO5+vE8KYJseXPGun+Zr6i5gl7i0O/BdBfY4BDRAZsrF5v7likptEbRwJ8+1IEvIT5LaUc5J5BnGM00 # 9BuzsZzK8cphhKvepVmA8Gy0gWvfDeA==",timestamp="1592375315",serial_no="14DFFAAAA79AF4BD56CC1O55E06246E95D3PAAS0"
总结 以上是内存溢出为你收集整理的python --微信v3支付全部内容,希望文章能够帮你解决python --微信v3支付所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)