支付宝支付功能实现
安装包
pip installalipay-sdk-python
pip install qrcode
代码实现
import qrcodefrom alipay.aop.api.AlipayClientConfig import AlipayClientConfigfrom alipay.aop.api.DefaultAlipayClient import DefaultAlipayClientfrom alipay.aop.api.domain.AlipayTradePrecreateModel import AlipayTradePrecreateModelfrom alipay.aop.api.domain.AlipayTradeQueryModel import AlipayTradeQueryModelfrom alipay.aop.api.domain.AlipayFundTransUniTransferModel import AlipayFundTransUniTransferModelfrom alipay.aop.api.request.AlipayTradePrecreateRequest import AlipayTradePrecreateRequestfrom alipay.aop.api.request.AlipayTradeQueryRequest import AlipayTradeQueryRequestfrom alipay.aop.api.request.AlipayFundTransUniTransferRequest import AlipayFundTransUniTransferRequestfrom alipay.aop.api.domain.Participant import Participant
url = '/gateway.do' # 支付宝网关alipay_client_config = AlipayClientConfig()alipay_client_config.server_url = urlalipay_client_config.app_id = '' # 应用idalipay_client_config.app_private_key = '' # 应用私钥alipay_client_config.alipay_public_key = '' # 支付宝公钥client = DefaultAlipayClient(alipay_client_config=alipay_client_config)model = AlipayTradePrecreateModel()model.out_trade_no = '0812052469' # 充值编号model.total_amount = 20.0 # 充值金额model.subject = "充值"model.body = "充值"model.timeout_express = '5m'request_info = AlipayTradePrecreateRequest(biz_model=model)response = client.execute(request_info)qr_code = json.loads(response)['qr_code']qrcode_name = os.path.join(os.getcwd(), 'upload', 'img_code', '{}.png'.format(out_trade_no))if not os.path.exists(os.path.join(os.getcwd(), 'upload', 'img_code')):os.makedirs(os.path.join(os.getcwd(), 'upload', 'img_code'))img = qrcode.make(qr_code)with open(qrcode_name, 'wb') as f:img.save(f)
微信扫码支付功能实现
import randomfrom Cryptodome.PublicKey import RSAfrom Cryptodome.Signature import pkcs1_15from Cryptodome.Hash import SHA256from cryptography.hazmat.primitives.ciphers.aead import AESGCMfrom base64 import b64encode
# 获取签名def get_sign(sign_str):# 证书路径apiclient_key = os.path.join('wxpay', 'apiclient_key.pem')rsa_key = RSA.importKey(open(apiclient_key).read())signer = pkcs1_15.new(rsa_key)digest = SHA256.new(sign_str.encode('utf8'))sign = b64encode(signer.sign(digest)).decode('utf-8')return signnumber_list = [str(num) for num in range(10)]letter_list = [chr(i) for i in range(97, 123)]letter_upper_list = [chr(i).upper() for i in range(97, 123)]all_list = number_list + letter_list + letter_upper_listrandom.shuffle(all_list)nonce_str = ''.join(random.choices(all_list, k=32)) # 随机数列url = 'https://api.mch./v3/pay/transactions/native' #支付接口网关serial_no = '' # 证书序列号mchid = '' # 商户iddata = {"mchid": '' #商户id ,"out_trade_no": '08115469782' # 充值编号,"appid": '', # 应用id"description": "充值", # 充值内容介绍"notify_url": '', # 微信支付接口回调 "amount": {"total": int(float(200)*100), # 以分为单位 "currency": "CNY"}}sign_str = '\n'.join(["POST", # HTTP请求方法url.split(urlparse(url).netloc)[-1], # path+argstimestamp, # 时间戳nonce_str, # 请求随机串json.dumps(data), '' # 请求报文主体])sign = get_sign(sign_str)headers = {'Content-Type': 'application/json; charset=UTF-8','Authorization': 'WECHATPAY2-SHA256-RSA2048 mchid="{}",nonce_str="{}",signature="{}",timestamp="{}",serial_no="{}"'.format(mchid, nonce_str, sign, timestamp, serial_no)}res = requests.post(url, data=json.dumps(data), headers=headers)code_url = res.json()['code_url']qrcode_name = os.path.join(os.getcwd(), 'upload', 'img_code', '{}.png'.format(out_trade_no))if not os.path.exists(os.path.join(os.getcwd(), 'upload', 'img_code')):os.makedirs(os.path.join(os.getcwd(), 'upload', 'img_code'))img = qrcode.make(code_url)# 保存二维码with open(qrcode_name, 'wb') as f:img.save(f)