I can't find any official python alipay sdks so far, and it really pains a lot dealing with those sign methods. Hoping this libarary could do some help :).
So far, the following functions are supported:
- Pay via Web
- Pay via WAP
- Pay via App
- Verification
- Face to face trade
- Precreate trade
- Query trade
- Cancel trade
- Refund
- Transfer money to alipay account
- Query money transfer result
Taking a look at this guide if you are interested at the details on signing your order requests. Or you may just follow this manual if not.
pip install python-alipay-sdk --upgrade
openssl
OpenSSL> genrsa -out app_private_key.pem 2048 # the private key file
OpenSSL> rsa -in app_private_key.pem -pubout -out app_public_key.pem # export public key
OpenSSL> exit
from alipay import AliPay
alipay = AliPay(
appid="",
app_notify_url="",
app_private_key_path="",
alipay_public_key_path="" # alipay public key file path, do not put your public key file here
sign_type="RSA" # RSA or RSA2
debug=False # False by default
)
Given a alipay function, say alipay.trade.page.pay
, we will defind a corresponding function alipay.api_alipay_trade_page_pay()
Generally we will do such a translation:
function_name = "alipay_" + alipay_function_name.replace(".", "_")
according to alipay document, some paremeters in biz_content
are optional and some are not.
we defind functions in this way so that you can put those optional parameters in kwargs
:
def api_alipay_xxx(self, out_trade, total_amount, **kwargs):
...
biz_content.update(kwargs)
# if you are using Python 2(you should really think about Python 3), making sure non-ascii strings are utf-8 encoded
subject = u"测试订单".encode("utf8")
# if you are Python3 user, just use the default string
subject = "测试订单"
# paid via Web,open this url in your browser: 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"
)
# paid via WAP, open this url in your browser: 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="http://example.com"
)
# paid via App,just order_string to your Android or iOS client
order_string = alipay.api_alipay_trade_app_pay(
out_trade_no="20161112",
total_amount="0.01",
subject=subject
)
Once an order is paid, you will get a POST request from alipay servers which informs you that the order is paid
Here is a simple example for flask web server:
import json
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/', methods=["GET", "POST"])
def hello_world():
data = request.form.to_dict()
signature = data.pop("sign")
print(json.dumps(data))
print(signature)
# verify
alipay.verify(data, signature)
if success and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED" ):
print("trade succeed")
return 'Hello, World!'
Here is a more general example for verification
# gathering all parameters sent from alipay server, and put them in a dictionary called data
data = {
"subject": "testing order",
"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 = 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
)
if result["code"] == "10000":
print("Order is paid")
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)
If you want to know what parameters are accepted, take a look into the official document
result = alipay.api_alipay_trade_refund(out_trade_no="xxx", refund_amount="xxx", ...)
if result["code"] == "10000":
print("success")
# 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="[email protected]",
amount=3.12
)
result = {'code': '10000', 'msg': 'Success', 'order_id': '', 'out_biz_no': '', 'pay_date': '2017-06-26 14:36:25'}
result = alipay.api_alipay_fund_trans_order_query(
out_biz_no="20170626152216"
)
print(result)
python -m unittest discover
Or you may do test manually in this way, debug=True
will direct your request to sandbox environment:
alipay = AliPay(..., debug=True)
- John60676
- varwey
alipay.fund.trans.toaccount.transfer
andalipay.fund.trans.order.query
- return url missing for
alipay.trade.page.pay
- code refactoring, all functions are renamed
alipay.trade.page.pay
is used instead ofcreate_direct_pay_by_user
- load key into memory, local key file access is needed for the 1st time