至于申请的流程,我就不说了,大家去:https://www.alipay.com 官网去看看申请流程
点击进入“手机网站支付”,然后页面底部“申请接入“,等待审核通过后
就可以新建一个网页应用了,地址是:https://openhome.alipay.com/platform/appManage.htm#/apps
然后需要填写一个授权回调地址,以及选择的加签方式,加密方式的工具,可以点击支付宝给我们做好的,生成秘钥地址是:https://docs.open.alipay.com/291/105971
WINDOWS加密工具生成器下载
MAC_OSX加密工具生成器下载
我用的后端语言是Python,所以可以去这里下载支付宝的工程师为我们写好的Python的包,我们只需要调用它的类即可
https://pypi.org/project/alipay-sdk-python/
pip install alipay-sdk-python
然后就是官方API文档:https://docs.open.alipay.com/203/105288/
代码如下:
from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
from alipay.aop.api.domain.AlipayTradeWapPayModel import AlipayTradeWapPayModel
from alipay.aop.api.request.AlipayTradeWapPayRequest import AlipayTradeWapPayRequest
# 支付宝的PyPI
# https://pypi.org/project/alipay-sdk-python/
# https://docs.open.alipay.com/203/105286/
AliPay_APP_ID = "Your APP ID"
AliPay_server_url = 'https://openapi.alipay.com/gateway.do'
def get_pay_url(self, out_trade_no, total_amount, subject, body, notify_url):
"""
官方文档:https://docs.open.alipay.com/203
页面接口示例:alipay.trade.wap.pay
"""
# 对照接口文档,构造请求对象
model = AlipayTradeWapPayModel()
model.out_trade_no = out_trade_no
model.total_amount = total_amount
model.subject = subject
model.body = body
model.product_code = "QUICK_WAP_WAY"
alipay_request = AlipayTradeWapPayRequest(biz_model=model)
alipay_request.notify_url = notify_url
client = get_alipay_client()
response_url = client.page_execute(alipay_request, http_method="GET")
return response_url, AliPay_APP_ID
def get_alipay_client(self):
"""
设置配置,包括支付宝网关地址、app_id、应用私钥、支付宝公钥等,其他配置值可以查看AlipayClientConfig的定义。
"""
alipay_client_config = AlipayClientConfig()
alipay_client_config.app_id = AliPay_APP_ID
alipay_client_config.alipay_public_key = AliPayPublicKey
alipay_client_config.app_private_key = AliPayPrivateKey
# logging.basicConfig(
# level=logging.INFO,
# format='%(asctime)s %(levelname)s %(message)s',
# filemode='a',)
# logger = logging.getLogger('')
"""
得到客户端对象。
注意,一个alipay_client_config对象对应一个DefaultAlipayClient,定义DefaultAlipayClient对象后,alipay_client_config不得修改,如果想使用不同的配置,请定义不同的DefaultAlipayClient。
logger参数用于打印日志,不传则不打印,建议传递。
"""
client = DefaultAlipayClient(alipay_client_config=alipay_client_config)
return client
变量response_url就是返回的支付宝支付URL,直接在手机任意浏览器上打开就能看到,如果是支付宝app打开,则直接去支付,如果是其他的浏览器打开,就提示相应的跳转,如果是微信,则微信禁止了支付宝跳转
变量notify_url是支付成功后返回的URL地址,POST形式请求的
class AliPayNotifyUrlController(tornado.web.RequestHandler):
"""
支付宝手机网站支付结果异步通知
官方文档:https://docs.open.alipay.com/203/105286/
"""
def __init__(self, application, request, **kwargs):
super(AliPayNotifyUrlController, self).__init__(application, request, **kwargs)
self.title = "支付宝支付"
def post(self):
notify_time = self.get_argument("notify_time")
notify_type = self.get_argument("notify_type", "")
notify_id = self.get_argument("notify_id", "")
subject = self.get_argument("subject")
body = self.get_argument("body")
app_id = self.get_argument("app_id")
seller_id = self.get_argument("seller_id")
seller_email = self.get_argument("seller_email")
buyer_id = self.get_argument("buyer_id")
auth_app_id = self.get_argument("auth_app_id")
buyer_logon_id = self.get_argument("buyer_logon_id")
fund_bill_list = self.get_argument("fund_bill_list")
fund_bill_list = json.loads(fund_bill_list)
fund_bill_list_account = fund_bill_list[0]["amount"]
fund_bill_list_fundChannel = fund_bill_list[0]["fundChannel"]
trade_status = self.get_argument("trade_status")
total_amount = self.get_argument("total_amount")
invoice_amount = self.get_argument("invoice_amount")
receipt_amount = self.get_argument("receipt_amount")
buyer_pay_amount = self.get_argument("buyer_pay_amount")
point_amount = self.get_argument("point_amount")
version = self.get_argument("version")
out_trade_no = self.get_argument("out_trade_no")
trade_no = self.get_argument("trade_no")
charset = self.get_argument("charset")
sign = self.get_argument("sign")
sign_type = self.get_argument("sign_type")
gmt_create = self.get_argument("gmt_create")
gmt_payment = self.get_argument("gmt_payment", None)
gmt_close = self.get_argument("gmt_close", None)
model = MyAliPayOrderModel()
model.notify_time = notify_time
model.notify_type = notify_type
model.notify_id = notify_id
model.subject = subject
model.body = body
model.app_id = app_id
model.seller_id = seller_id
model.seller_email = seller_email
model.buyer_id = buyer_id
model.auth_app_id = auth_app_id
model.buyer_logon_id = buyer_logon_id
model.fund_bill_list = fund_bill_list
model.fund_bill_list_account = fund_bill_list_account
model.fund_bill_list_fundChannel = fund_bill_list_fundChannel
model.trade_status = trade_status
model.total_amount = total_amount
model.invoice_amount = invoice_amount
model.receipt_amount = receipt_amount
model.buyer_pay_amount = buyer_pay_amount
model.point_amount = point_amount
model.version = version
model.out_trade_no = out_trade_no
model.trade_no = trade_no
model.charset = charset
model.sign = sign
model.sign_type = sign_type
model.gmt_create = gmt_create
model.gmt_payment = gmt_payment
model.gmt_close = gmt_close
# 更新到数据库
# to do something
# 如果商户反馈给支付宝的字符不是success这7个字符,支付宝服务器会不断重发通知,直到超过24小时22分钟。
# 一般情况下,25小时以内完成8次通知(通知的间隔频率一般是:4m,10m,10m,1h,2h,6h,15h)。
self.write("success")
self.finish()