python --微信v3支付

v3支付

# from Crypto.PublicKey import RSA
# import random
# from Crypto.Signature import PKCS1_v1_5
# from Crypto.Hash import SHA256
# import base64
# import time
import time
import random
from Cryptodome.PublicKey import RSA
from Cryptodome.Signature import pkcs1_15 as PKCS1_v1_5
from Cryptodome.Hash import SHA256
import base64

class 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 authorization


if __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', ))

安卓支付

import time
import random
from Cryptodome.PublicKey import RSA
from Cryptodome.Signature import pkcs1_15
from Cryptodome.Hash import SHA256
from base64 import b64encode

mchid = '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+gGaVf54IXwvwdAwB+MhfkE0ZL/Tyd2PC4s7j0ZJol5G7Ddd/tOye4cx" \
#               "uOkL3USyuB7UhFgpx4RT88OYlYbsQtOmw6gW5D376bWBUu46rw1Dwbp8V7JQCRTJ" \
#               "K+vbMj1ytgP10fg/hTQNe7RWmd+KC+OGZbBH6MxybLoR+BOwQQpwGEgoEoY9Rmlm" \
#               "E/8poPvdY/PLIaS2ca/rsgJ/ZhiSx6BL0DAqVkKtM9U9T0LRoZmJcCp6YgLKGA0X" \
#               "BrnbMXczFHFVfjmK1jw4UqWbPICsrbhF4dtIuSqZbDECgYEA4M8J4T/grNaNhQTc" \
#               "x6q/ylFZB3I8Qoih2YeLx7/JOBEL66jNJhHVrgyF8JChlL0J8RGMg8KdxzZ0OTEw" \
#               "Q5ZK4mo93cxwqFh87X4KUNaicNveCsxuEL80Bbhst7NmJKi5T2VQMR9kFd+eTwx8" \
#               "c5aOHX5W4RIogKyNHqvCdEqHohMCgYEAz+1/NfRtqI2F68olrtdlbReaHtlCZxRJ" \
#               "3XhaIT5OGjXE/0upRRx6EgDh4f+wvqpFdlutPHFTSz4H/5rBGyZ7NJ5D44WO3Z7K" \
#               "SdY3YJ+5J6Nu+2OX6FGZWmqyW9kgYRthzClqNduBWbgu0gRC8R8wLg1W7xe9HN5e" \
#               "bc0HnhnBfvkCgYBSNMNbH/2rlkVn1/BX/yNk+zxAEdDhT49HuV4u6/3Lx8gBI9fo" \
#               "zOrDW4b7AhhkCICDK7SjVd5WQ55ab5dDj8jQZK384Q5tMPZ17fodt27tMClQ75Js" \
#               "A08lrFvtDOg6DbK9ysF5RQ5XRU9hfqJfrjVHqbRhVz+CVhbAmXRhDAPvC5KBgQCX" \
#               "kXuCvCvXi1qNF+1CN3eS/3p0dFEITOzPSXUB+KX8SyfQJbo9S9XcG9KM6NNRGVPL" \
#               "RGbSwZVDKvOvqoKLpRB4ucmpJ+mNubuh+Uqi36ubrnIvRFkum5TbKR3dADivMMOo" \
#               "jKQEoH75BN70bvDRTbfUShsN8ZAA5EQXbDbaU9IOGQKBgEr+xQMksLCif8GrwlfJ" \
#               "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 sign


def 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 authorization


if __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"
上一篇:接口的调用和返回值的处理(Post请求)


下一篇:oracle中decode 结合 sign 使用