接口文档:https://docs.open.alipay.com/204/105465/
一、签名方法示例:「代码中的私钥是随机生成无法使用,做示例用」
def sign(params):
sort_param = sorted([(key, unicode(value, 'UTF-8').encode('UTF-8')) for key, value in params.iteritems()], key=lambda x: x[0])
content = '&'.join(['='.join(x) for x in sort_param])
priKey = '''-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAqZWHu3pBiKj5DDod4uDg0Tnmr6wDXzDogVjqZMVr6Khf+4qG
ENFyFDAWiycHQwFh6UzyRFDFiiBh2TS3IyBMQ6Noow32bOXDM/ObfiFRSYO9iCLv
Q+SU3r2S3dOx+l7YkYJBiM9stKuTlA3r6Ma9Rq9j26cIQLR/YMWGDV/MQy/AS0l2
fa97hK/coKiope3ILTJnhknCli5oGBi2aoLx1mT+OMMguSFku2Pd9tgE7ZLABYrk
0Bz1Qq4O3UiUrGrpe0d3/hBAbZIu4blZsp4YbBxSRy+VH9KqcJ/vsKXp1d8mMjV0
QuacZBdOHscWu7/wz4xbQgu6E8iEXT21a9JpQQIDAQABAoIBAQCgGw6kz83jajSH
eK8SrmFS56mGHY6pZpF83SeesE0qwRytbaEZa8SajrQZ+GUAu+Nm0kDAgsmdmOkI
mp50yV93VGXL27jWIy5idBJPyMDn+TsNtn3SyZJLoQtx5xXXXYh2CFY7BiqJgJu6
YlJh2JCGlbxfepluDXtdL99Z7SH6IyaszW1ffaQvoI6OuQpWj/Mo79VIMipCztFT
sMWW/O6ySuz2+DqQ8ShgMsCP9LMOGkhWtw7JaOGjSH6ZqOropQ3rFiFF0HI3ILjV
s/e6chOIpwCcG3wzt5yd4s/GNo6FkKNsKVRv8GN1RlFjp6TtCfQSX34Isz2CBrCr
gyyBwQlhAoGBAN+fJ2D/fk8Iny3uwhhz8mK1HThFisAwMMKWN9f49/xBlrZy1rV2
oRdo7WmNfCewbKBjPIdFMR+aH7vuJd+3ofiTY3Dn94ZnzIDW/7tici8blKNlh6NF
6LnlzdoMWqPvChB6q6YZwTWpSnDVtUBpMlS1PXwr7cQ0zPrpgPcWFaEFAoGBAMIj
ZglbsJ3WRD4Paq8UKHzCWp5CpLOeFO504uyPvFY6EKPD8vueVkKb2zLBdu1a4iCG
CIkmO91raY0F7TUYM5oX7Rys+2AiX+QEZlWGjgsU0xQg2dUsfVjTC9Hdwv/QHO55
Ye84zpmr1xbjEbgM1WojPy3bsoKfG6He1NkgCQwNAoGAaZo6OXdOSbpzmXhoFq4i
p/SJwPouRQY88JeJ9abwTSMYLj7bIETOXETD4RXcrM2G7tMOUX8ebKMXG2OiZRzO
E5pe2oHhBYrdK3bcu7wI2jA3KFaZEETCW4+nf7JX15TZ9uBHUSBWeqXgrppthuZm
y1PvnkUMepzItDEzIvw96JUCgYBILPrpbcI63Acg/4BNOPRYdbVU8zfiY1oq0R/w
jPubHxlCmT4D1/j/6IvU0eJSGiDJsKsJukI1bpKeUF7BB49fsZeAurr2IbF+xzf4
gf7bFdHnwNLbkx/C3yucyZKOoqOu8Ue8tWTx8tYTdzYGNBc/z3zT/DQgpeCTDJIb
avKSHQKBgCZwht04cyFDqv1brXJ1VAE6XJKDGopQbEBdCKg/C2HXfeHx2H47bkNU
Vv5hmuGZc70Xu+aol0KLVWU1/zFSvO8f4I8eTYq1H6qR4ml2chvBVjf3uT82gUbQ
EduCPhYXAlyL2sm51/CHtZW1+GVQxHLD7zXm1g4cExmJCmvdwV7B
-----END RSA PRIVATE KEY-----'''
private_key = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, priKey)
return b64encode(OpenSSL.crypto.sign(private_key, content, 'sha256'))
二、主函数:「 params['app_id']需要配置成您的开发者id」 ./signThing.txt就是生成的请求串
#encoding:utf-8
from base64 import b64encode
import json
import urllib
import OpenSSL
if '__main__' == __name__:
params = {}
params['app_id'] = 'XXX'
params['method'] = 'alipay.trade.app.pay'
params['format'] = 'json'
params['timestamp'] = '2017-07-07 10:05:56'
params['charset'] = 'UTF-8'
params['version'] = '1.0'
params['sign_type'] = 'RSA2'
params['biz_content'] = {}
params['biz_content']['body'] = '非sdk原生php服务端生成请求订单'
params['biz_content']['subject'] = 'php代码示例'
params['biz_content']['out_trade_no'] = '201723123123124124'
params['biz_content']['timeout_express']='10m'
params['biz_content']['total_amount'] = '0.01'
params['biz_content']['product_code'] = 'QUICK_MSECURITY_PAY'
params['biz_content']['goods_type'] = '0'
params['biz_content']=json.dumps(params['biz_content'], separators=(',', ':'))
params['sign']=rsa_sign(params);
sort_param = sorted([(key, unicode(value, 'UTF-8').encode('UTF-8')) for key, value in params.iteritems()], key=lambda x: x[0])
file = open('./signThing.txt', 'wb')
file.write(urllib.urlencode(sort_param))
file.close()
原文链接: https://openclub.alipay.com/read.php?tid=1223&fid=2