支付宝支付功能实现
安装包
pip install alipay-sdk-python
pip install qrcode
代码实现
import qrcode from alipay.aop.api.AlipayClientConfig import AlipayClientConfig from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient from alipay.aop.api.domain.AlipayTradePrecreateModel import AlipayTradePrecreateModel from alipay.aop.api.domain.AlipayTradeQueryModel import AlipayTradeQueryModel from alipay.aop.api.domain.AlipayFundTransUniTransferModel import AlipayFundTransUniTransferModel from alipay.aop.api.request.AlipayTradePrecreateRequest import AlipayTradePrecreateRequest from alipay.aop.api.request.AlipayTradeQueryRequest import AlipayTradeQueryRequest from alipay.aop.api.request.AlipayFundTransUniTransferRequest import AlipayFundTransUniTransferRequest from alipay.aop.api.domain.Participant import Participant
url = 'https://openapi.alipay.com/gateway.do' # 支付宝网关 alipay_client_config = AlipayClientConfig() alipay_client_config.server_url = url alipay_client_config.app_id = '' # 应用id alipay_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 = '20220812052469' # 充值编号 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 random from Cryptodome.PublicKey import RSA from Cryptodome.Signature import pkcs1_15 from Cryptodome.Hash import SHA256 from cryptography.hazmat.primitives.ciphers.aead import AESGCM from 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 sign timestamp = str(int(time.time())) number_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_list random.shuffle(all_list) nonce_str = ''.join(random.choices(all_list, k=32)) # 随机数列 url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/native' #支付接口网关 serial_no = '' # 证书序列号 mchid = '' # 商户id data = { "mchid": '' #商户id , "out_trade_no": '202208115469782' # 充值编号, "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+args timestamp, # 时间戳 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)
微信充值查询:
mchid = '' # 商户id number_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_list random.shuffle(all_list) nonce_str = ''.join(random.choices(all_list, k=32)) # 32随机码 url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/' serial_no = '' # 安全证书序列号 query_url = r'{}{}?mchid={}'.format(url, out_trade_no, mchid) sign_str = '{}\n{}\n{}\n{}\n{}\n'.format( "GET", # HTTP请求方法 query_url.split(urlparse(query_url).netloc)[-1], timestamp, # 时间戳 nonce_str, # 请求随机串 '' ) 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.get(query_url, headers=headers) trade_state = res.json()['trade_state'] if trade_state == 'SUCCESS': print('充值成功')
支付宝充值查询:
alipay_client_config = AlipayClientConfig() alipay_client_config.server_url = '' #支付宝网关 alipay_client_config.app_id = '' # 支付宝应用id alipay_client_config.app_private_key = '' # 支付宝应用私钥 alipay_client_config.alipay_public_key = '' # 支付宝应用公钥 client = DefaultAlipayClient(alipay_client_config=alipay_client_config) model = AlipayTradeQueryModel() model.out_trade_no = out_trade_no # 订单编号 request_info = AlipayTradeQueryRequest(biz_model=model) response = client.execute(request_info) msg = json.loads(response)['msg'] if msg == 'Success': if json.loads(response)['trade_status'] == 'TRADE_SUCCESS': print('充值成功')