- 电脑网站支付
- 手机网站支付
- APP支付
- 小程序支付
- 通知验证
- 当面付
- 交易预创建
- 交易查询
- 交易取消
- 退款
- 统一退款查询
- 统一收单交易结算接口
- 统一收单交易关闭接口
- 单笔转账到支付宝账户接口
- DC/单笔转账接口
- 查询转账订单接口
- ISV集成/生成app_auth_code
- ISV集成/查询授权产品
电脑网站支付 alipay.trade.page.pay
# 如果你是 Python2 用户(考虑考虑升级到 Python3 吧),请确保非 ascii 的字符串为 utf8 编码:
subject = u"测试订单".encode("utf8")
# 如果你是 Python3 的用户,使用默认的字符串即可
subject = "测试订单"
# 电脑网站支付,需要跳转到:https://openapi.alipay.com/gateway.do? + order_string
order_string = alipay.api_alipay_trade_page_pay(
out_trade_no="20161112",
total_amount=0.01,
subject=subject,
return_url="https://example.com",
notify_url="https://example.com/notify" # 可选,不填则使用默认 notify url
)
手机网站支付 alipay.trade.wap.pay
# 手机网站支付,需要跳转到:https://openapi.alipay.com/gateway.do? + order_string
order_string = alipay.api_alipay_trade_wap_pay(
out_trade_no="20161112",
total_amount=0.01,
subject=subject,
return_url="https://example.com",
notify_url="https://example.com/notify" # 可选,不填则使用默认 notify url
)
App支付 alipay.trade.app.pay
# App 支付,将 order_string 返回给 app 即可
order_string = alipay.api_alipay_trade_app_pay(
out_trade_no="20161112",
total_amount=0.01,
subject=subject,
notify_url="https://example.com/notify" # 可选,不填则使用默认 notify url
)
小程序支付alipay.trade.create
# 小程序支付
alipay.api_alipay_trade_create(
out_trade_no="20161112",
total_amount=0.01,
subject=subject,
buyer_id="",
notify_url="https://example.com/notify" # 可选
)
这里有一个简单的基于 flask 的验证:
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/', methods=["GET", "POST"])
def hello_world():
data = request.form.to_dict()
# sign 不能参与签名验证
signature = data.pop("sign")
print(json.dumps(data))
print(signature)
# verify
success = alipay.verify(data, signature)
if success and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED" ):
print("trade succeed")
Django 版的
def hello_world(request):
# for django users
data = request.dict()
# for rest_framework users
data = request.data
signature = data.pop("sign")
# verification
success = alipay.verify(data, signature)
if success and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED" ):
print("trade succeed")
return 'Hello, World!'
一般而言,可以这样验证回调通知
# 验证 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")
success = alipay.verify(data, signature)
if success and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED" ):
print("trade succeed")
当面付(条码支付) alipay.trade.pay
以下是一个当面付的基本例子
alipay = AliPay(appid="", ...)
result = alipay.api_alipay_trade_pay(
out_trade_no="out_trade_no",
scene="bar_code/wave_code",
auth_code="auth_code",
subject="subject",
discountable_amount=10,
total_amount=20,
notify_url="https://example.com/notify" # 可选,不填则使用默认notify url
)
if result["code"] == "10000":
print("Order is paid")
交易预创建(扫码支付) alipay.trade.precreate
alipay = AliPay(appid="", ...)
# create an order
alipay.api_alipay_trade_precreate (
subject="test subject",
out_trade_no="out_trade_no",
total_amount=100
)
# check order status
paid = False
for i in range(10):
# check every 3s, and 10 times in all
print("now sleep 3s")
time.sleep(3)
result = alipay.api_alipay_trade_query(out_trade_no="out_trade_no")
if result.get("trade_status", "") == "TRADE_SUCCESS":
paid = True
break
print("not paid...")
# order is not paid in 30s , cancel this order
if paid is False:
alipay.api_alipay_trade_cancel(out_trade_no=out_trade_no)
refund 需要传入的参数参见官方文档
result = alipay.api_alipay_trade_refund(out_trade_no="xxx", refund_amount="xxx", ...)
if result["code"] == "10000":
print("success")
result = alipay.api_alipay_trade_fastpay_refund_query("20171120", out_trade_no="20171120")
result = {
'code': '10000',
'msg': 'Success',
'out_request_no': '20171120',
'out_trade_no': '20171120',
'refund_amount': '20.00',
'total_amount': '20.00',
'trade_no': '2017112021001004070200297107'
}
统一收单交易结算接口alipay.trade.order.settle
result = alipay.api_alipay_trade_order_settle(
out_request_no,
trade_no,
royalty_parameters
)
result = alipay.api_alipay_trade_close(
trace_no="xxx",
out_trade_no="xxx",
operator_id="this is optional"
)
result = {
"code": "10000",
"msg": "Success",
"trade_no": "2013112111001004500000675971",
"out_trade_no": "YX_001"
}
单笔转账到支付宝账户接口 alipay.fund.trans.toaccount.transfer
# transfer money to alipay account
result = alipay.api_alipay_fund_trans_toaccount_transfer(
datetime.now().strftime("%Y%m%d%H%M%S"),
payee_type="ALIPAY_LOGONID/ALIPAY_USERID",
payee_account="csqnji8117@sandbox.com",
amount=3.12
)
result = {'code': '10000', 'msg': 'Success', 'order_id': '', 'out_biz_no': '', 'pay_date': '2017-06-26 14:36:25'}
# Only support dc_client. Transfer money to alipay account
result = dc_alipay.api_alipay_fund_trans_uni_transfer(
out_biz_no=datetime.now().strftime("%Y%m%d%H%M%S"),
identity_type="ALIPAY_LOGON_ID/ALIPAY_LOGON_ID",
identity="egdjny5218@sandbox.com",
trans_amount=82.32,
name="沙箱环境"
)
result = {'code': '10000', 'msg': 'Success', 'order_id': '', 'out_biz_no': '', 'status': 'SUCCESS'}
查询转账订单接口 alipay.fund.trans.order.query
result = alipay.api_alipay_fund_trans_order_query(
out_biz_no="20170626152216"
)
print(result)
根据查询令牌获取发票要素列表 alipay.ebpp.invoice.token.batchquery
result = alipay.api_alipay_ebpp_invoice_token_batchquery(
invoice_token="123456"
)
print(result)
在开始前,请务必阅读官方文档
isv_alipay = ISVAliPay(
...
app_auth_code="app_auth_code"
)
response = isv_alipay.api_alipay_open_auth_token_app()
response = {
"code": "10000",
"msg": "Success",
"app_auth_token": "201708xxx",
"app_refresh_token": "201708xxx",
"auth_app_id": "appid",
"expires_in": 31536000,
"re_expires_in": 32140800,
"user_id": "2088xxxxx
}
response = isv_alipay.alipay_open_auth_token_app_query()