版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/linzi1994/article/details/83053456
支付宝开发平台登录
https://open.alipay.com/platform/home.htm
沙箱环境
- 是支付宝提供给开发者的模拟支付的环境
- 跟真实环境是分开的
- 沙箱应用:https://docs.open.alipay.com/200/105311
- 沙箱账号:https://openhome.alipay.com/platform/appDaily.htm?tab=account
支付宝开发者文档
- 文档主页:https://openhome.alipay.com/developmentDocument.htm
- 产品介绍:https://docs.open.alipay.com/270
- 快速接入:https://docs.open.alipay.com/270/105899/
- SDK:https://docs.open.alipay.com/270/106291/
- API列表:https://docs.open.alipay.com/270/105900/
- python对接支付宝SDK:https://github.com/fzlee/alipay/blob/master/README.zh-hans.md
- python对接支付宝SDK安装:
pip install python-alipay-sdk --upgrade
接入步骤
- 创建应用
- 配置密钥
- 搭建和配置开发环境
- 接口调用
配置秘钥
1. 生成应用的私钥和公钥
openssl
OpenSSL> genrsa -out app_private_key.pem 2048 # 私钥RSA2
OpenSSL> rsa -in app_private_key.pem -pubout -out app_public_key.pem # 导出公钥
OpenSSL> exit
2. 保存应用私钥文件
在应用中新建keys目录,用来保存秘钥文件。
将应用私钥文件app_private_key.pem复制到keys目录下。
3. 查看公钥
cat app_publict_key.pem
将公钥内容复制给支付宝
4. 保存支付宝公钥
在keys目录下新建alipay_public_key.pem文件,用于保存支付宝的公钥文件。
将支付宝的公钥内容复制到alipay_public_key.pem文件中
注意,还需要在公钥文件中补充开始与结束标志
-----BEGIN PUBLIC KEY-----
此处是公钥内容
-----END PUBLIC KEY-----
配置
# 支付宝
ALIPAY_APPID = "2016081600258081"
ALIPAY_URL = "https://openapi.alipaydev.com/gateway.do"
ALIPAY_DEBUG = True
接口调用
电脑网站支付
# 构造支付宝支付链接
alipay = AliPay(
appid=settings.ALIPAY_APPID, # 配置中的APPID
app_notify_url=None, # 默认回调url
app_private_key_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), "keys/app_private_key.pem"), # 应用私钥
alipay_public_key_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), "keys/alipay_public_key.pem"), # 支付宝公钥
sign_type="RSA2", # RSA 或者 RSA2
debug=settings.ALIPAY_DEBUG # 默认是False
)
order_string = alipay.api_alipay_trade_page_pay(
out_trade_no='20161112', # 订单号
total_amount='0.01', # 商品总金额
subject='测试订单'
return_url="https://example.com",
notify_url="https://example.com/notify" # 可选, 不填则使用默认notify url
)
验证回调通知
# 验证alipay的异步通知,data来自支付宝回调POST 给你的data,字典格式.
data = {
"subject": "测试订单",
"gmt_payment": "2016-11-16 11:42:19",
"charset": "utf-8",
"seller_id": "xxxx",
"trade_status": "TRADE_SUCCESS",
"buyer_id": "xxxx",
"auth_app_id": "xxxx",
"buyer_pay_amount": "0.01",
"version": "1.0",
"gmt_create": "2016-11-16 11:42:18",
"trade_no": "xxxx",
"fund_bill_list": "[{\"amount\":\"0.01\",\"fundChannel\":\"ALIPAYACCOUNT\"}]",
"app_id": "xxxx",
"notify_time": "2016-11-16 11:42:19",
"point_amount": "0.00",
"total_amount": "0.01",
"notify_type": "trade_status_sync",
"out_trade_no": "xxxx",
"buyer_logon_id": "xxxx",
"notify_id": "xxxx",
"seller_email": "xxxx",
"receipt_amount": "0.01",
"invoice_amount": "0.01",
"sign": "xxx"
}
signature = data.pop("sign")
alipay = AliPay(
appid=settings.ALIPAY_APPID, # 配置中的APPID
app_notify_url=None, # 默认回调url
app_private_key_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), "keys/app_private_key.pem"), # 应用私钥
alipay_public_key_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), "keys/alipay_public_key.pem"), # 支付宝公钥
sign_type="RSA2", # RSA 或者 RSA2
debug=settings.ALIPAY_DEBUG # 默认是False
)
success = alipay.verify(data, signature)
if success and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED" ):
print("trade succeed")
其他接口调用
python对接支付宝SDK:https://github.com/fzlee/alipay/blob/master/README.zh-hans.md