某平台app订单提交支付可获取支付参数跳转支付宝,但只拼接里面参数不能够在web中打开支付链接。
通过抓包发现数据包中将alipay_sdk参数访问阿里接口 加密生成了mclient域名的支付连接,可在 浏览器打开此链接,从而实现支付宝app支付转换到h5来支付。
from flask import Flask, request, jsonify
import json
import requests
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
import base64
from Crypto.Cipher import DES3
from Crypto.Util.Padding import pad, unpad
class AiliPay(object):
def __init__(self):
self.private_key = '''
###################################################
'''
self.public_key = '''
########################################'''
self.key = '########'
def encrypt_3des(self,data):
cipher = DES3.new(self.key.encode(), DES3.MODE_ECB)
padded_data = pad(data.encode(), DES3.block_size)
encrypted_data = cipher.encrypt(padded_data)
return encrypted_data
def decrypt_3des(self,data):
ct_bytes = base64.b64decode(data)
cipher = DES3.new(self.key.encode(), DES3.MODE_ECB)
plain_text = unpad(cipher.decrypt(ct_bytes), DES3.block_size)
return plain_text.decode()
def rsa_encrypt(self, message):
cipher = Cipher_pkcs1_v1_5.new(RSA.importKey(self.public_key))
cipher_text = base64.b64encode(cipher.encrypt(message.encode())).decode()
return cipher_text
def rsa_decrypt(self, text):
cipher = Cipher_pkcs1_v1_5.new(RSA.importKey(self.private_key))
retval = cipher.decrypt(base64.b64decode(text), 'ERROR').decode('utf-8')
return retval
def convert_alipay_to_h5(self,alipay_sdk):
json_form = '{"tid":"######","user_agent":"Msp/9.1.5 (Android 12;Linux 4.4.146;zh_CN;http;540*960;21.0;WIFI;8#####;######################fruicqrn;r2agza5c56pzmev;<unknown ssid>;02:00:00:00:00:00)","has_alipay":false,"has_msp_app":false,"external_info":"' + alipay_sdk + '","app_key":"########","utdid":"########","new_client_key":"#####","action":{"type":"cashier","method":"main"},"gzip":true}'
encrypted_data = base64.b64encode(self.encrypt_3des(json_form)).decode()
req_data = parameter1 + parameter3 + encrypted_data
url = 'http://mcgw.alipay.com/gateway.do'
data = {"data": {"device": "GOOGLE", "namespace": "com.alipay.mobilecashier", "api_name": "com.alipay.mcpay",
"api_version": "4.0.2", "params": {"req_data": req_data}}}
headers = {
'Accept-Charset': 'UTF-8',
'Connection': 'Keep-Alive',
'Content-Type': 'application/octet-stream;binary/octet-stream',
'Host': 'mcgw.alipay.com',
'User-Agent': 'msp'
}
response = requests.post(url, headers=headers, json=data, verify=False)
json_data = json.loads(response.text)
res_data = self.rsa_encrypt(json_data['data']['params']['res_data'])
json_data = json.loads(res_data)
if __name__ == '__main__':
pay = AiliPay()
pay.convert_alipay_to_h5("alipay_sdk=alipay-easysdk-java&app_id=#############")