69 lines
1.8 KiB
Python
69 lines
1.8 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
|
||
import json
|
||
import hashlib
|
||
import aiohttp
|
||
|
||
class YsmPayApi:
|
||
"""易收米支付API工具类"""
|
||
|
||
@staticmethod
|
||
async def http_post(url, data):
|
||
"""
|
||
发送异步HTTP POST请求
|
||
|
||
Args:
|
||
url: 请求地址
|
||
data: 请求数据(JSON字符串)
|
||
|
||
Returns:
|
||
str: 响应内容
|
||
"""
|
||
headers = {
|
||
'Content-Type': 'application/json; charset=UTF-8',
|
||
'Accept': 'application/json',
|
||
'User-Agent': '*/*',
|
||
'Authorization': 'WECHATPAY2-SHA256-RSA2048 '
|
||
}
|
||
|
||
try:
|
||
async with aiohttp.ClientSession() as session:
|
||
async with session.post(url, data=data, headers=headers, ssl=False, timeout=30) as response:
|
||
return await response.text()
|
||
except Exception as e:
|
||
print(f"Error: {str(e)}")
|
||
return None
|
||
|
||
@staticmethod
|
||
def hash_sign(data, secret):
|
||
"""
|
||
生成签名
|
||
|
||
Args:
|
||
data: 要签名的数据字典
|
||
secret: 密钥
|
||
|
||
Returns:
|
||
str: 签名字符串
|
||
"""
|
||
# 按键名升序排序
|
||
sorted_data = dict(sorted(data.items()))
|
||
|
||
# 构造签名字符串
|
||
sign_str = ""
|
||
for key, value in sorted_data.items():
|
||
# 跳过hash字段和空值
|
||
if key == 'hash' or value is None or value == '':
|
||
continue
|
||
|
||
# 添加分隔符
|
||
if sign_str:
|
||
sign_str += '&'
|
||
|
||
# 拼接键值对
|
||
sign_str += f"{key}={value}"
|
||
|
||
# 添加密钥并计算SHA256哈希
|
||
sign_str += secret
|
||
return hashlib.sha256(sign_str.encode('utf-8')).hexdigest() |