支付宝支付流程
2.新建支付宝应用
# 访问"支付宝开发平台"登录,可以访问开发者中心
https://open.alipay.com/platform/home.htm
# 可以参考"电脑网站支付" 熟悉电脑支付整体流程
https://docs.open.alipay.com/270/105899/
2.1 第一步:创建应用(这里使用沙箱环境测试)
沙箱环境说明
线上环境需要创建应用,因为我们不是企业,没有资质,所以只能申请沙箱环境
简单来讲沙箱环境就是给开发者使用的测试环境 沙箱环境地址:
https://openhome.alipay.com/platform/appDaily.htm?tab=info
沙箱环境地址: https://openhome.alipay.com/platform/appDaily.htm?tab=info
支付宝开放平台: https://open.alipay.com/platform/home.htm 支付宝沙箱环境: https://openhome.alipay.com/platform/appDaily.htm?tab=info 支付宝开发者文档:https://openhome.alipay.com/developmentDocument.htm 电脑网站支付流程:https://docs.open.alipay.com/270 生成签名:https://docs.open.alipay.com/291/106103/ python-alipay-adk : https://github.com/fzlee/alipay/blob/master/README.zh-hans.md
3.2 说明
阿里官方没有提供python对接支付的sdk,但是python库中有非官方的sdk包可以使用
python-alipay-adk : https://github.com/fzlee/alipay/blob/master/README.zh-hans.md
使用起来非常简单,只要传入几个必要的参数就可以完成
由于支付对安全要求很高,所以要理解对接支付宝安全的流程: 公钥加密、私钥解密 这八个字
3.3 app_private_key.pem app私钥
-----BEGIN RSA PRIVATE KEY-----
MIICXwIBAAKBgQCq0LFBc9yy8exEepMAXF4BoeaKaa/GJ8AZYh2eEhIro7Z9e6XK5mYXrfVwHbWKO4EhD4SKYrmQx+qlDvkvLNJjzBpG3q1MoiqYVRSpu7IH1F0igHnvHyvzepjIqPzWcgnZDsEHSgeOkoL2ffCG7eIqyo5O9xIHTvScBVzihabXFwIDAQABAoGBAKB5w62cLiGnirKi/Wv/Wwnpkm9C7zIYsi6msPCKRavd+FpXSeEfJ5bjhYw5PQ6K7Lukmn5wVamhR96d/JjHYTd74d6qhPAyorCGYSbLdNmtXT/kL2WeLclt4hfdYQM54eR0q0DPNc7Q4THnw8s2jpxdEpa3nKmyGhl3BgsrWpZxAkEA0l3c3TrxC7l2dcA7uYw3I2PYt7ORLdjdJ5eIK5+xnZ5UQHsPwTKN/OHj67gHJ1NcFdzcfCWjUQq6NY/fCBrnwwJBAM/ecrgLmwN5ZLF2J0nwX6EvyMr7Q6IpRNxIeAyhze00Xg1VL9jExzYbM9jFzezrSG7UJC2n+LlVpcR10GJnsh0CQQClkcSsxqBDRxRKNyOKtSYMj6BZbVY9jGr7p1NAam8EDEJpgbHzsBN6zltyAiR3Oi/G8U8D0yfIODY4w+SZX3u7AkEAmHH8Ugt42LhrZ1ZOhAQ8Ldr8eBiDxmZbsO2Y1TLI8iBIpaCG4YOW0ZBrmpueZRU8XsNH8Q/R8iHk20rH05NW5QJBAL51dx/IEQOZOUAwnlYWV4aE3XGmCyP8hP4fjQA0po7+CRV9Y9D4F44dtHvQylhLXP0DuAHyT47z+7JDannZdE8=
-----END RSA PRIVATE KEY-----
3.4 alipay_public_key.pem 支付宝公钥
-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB -----END PUBLIC KEY-----
3.5 测试生成支付链接
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# -*- coding: utf-8 -*- from alipay import AliPay app_private_key_string = open('app_private_key.pem').read() alipay_public_key_string = open( 'alipay_public_key.pem').read() def get_alipay_url(): alipay=AliPay( appid="2016102600762709", # 沙箱appid app_notify_url=None, # 默认回调url app_private_key_string=app_private_key_string, # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥, alipay_public_key_string=alipay_public_key_string, sign_type="RSA", # RSA 或者 RSA2 debug=True, # 默认False,我们是沙箱,所以改成True(让访问沙箱环境支付宝地址) ) # 调用支付接口,生成支付链接 order_string = alipay.api_alipay_trade_page_pay( out_trade_no = "201612226", # 订单id,应该从前端获取 total_amount=str(0.01), # 订单总金额 subject="测试阿里云付款", # 付款标题信息 return_url=None, # 付款成功回调地址(可以为空) notify_url=None # 付款成功后异步通知地址(可以为空) ) pay_url = "https://openapi.alipaydev.com/gateway.do?" + order_string print( pay_url) # 将这个url复制到浏览器,就会打开支付宝支付页面 if __name__ == '__main__': get_alipay_url() # query_pay()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
''' trade_query 查询用户付款信息 返回信息详情请看官方:https://docs.open.alipay.com/api_1/alipay.trade.query { "code": "10000", # code=10000代表支付成功 "msg": "Success", "buyer_logon_id": "foq***@sandbox.com", "buyer_pay_amount": "0.00", "buyer_user_id": "2088102180228981", "buyer_user_type": "PRIVATE", "invoice_amount": "0.00", "out_trade_no": "20161112", "point_amount": "0.00", "receipt_amount": "0.00", "send_pay_date": "2020-02-20 21:20:48", "total_amount": "0.01", "trade_no": "2020022022001428981000087565", "trade_status": "TRADE_SUCCESS" } '''
生成链接地址如下
https://excashier.alipaydev.com/standard/auth.htm?
payOrderId=79d3a024f4904f58af434843b945132f.00