Skip to content

Commit d0f6dd0

Browse files
committed
commit alipay python sdk and Django Demo
1 parent 6306ef7 commit d0f6dd0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+13807
-0
lines changed

.gitignore

Whitespace-only changes.

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 wangdiwen
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

100644100755
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,101 @@ Alipay_Python_API
22
=================
33

44
This repo is Python API interface of Alipay, it gives direct and dual trade 2 functions.
5+
6+
Note:
7+
=================
8+
这个仓库是支付宝接口的 Python 版本,提供了即时到帐和双功能(担保交易)2种功能。
9+
10+
其中 Web 项目基于 Django,
11+
12+
支付宝接口功能的 API 和 Lib 在项目结构目录的 third_part 目录中,
13+
14+
1、alipay_direct ==> 即时到帐功能接口
15+
16+
2、alipay_dual ==> 双功能(担保交易)功能接口
17+
18+
3、alipay_send ==> 商家物流发货确认功能接口(配合双功能使用)
19+
20+
21+
关于接口类:
22+
==================
23+
每个功能都是独立的给出简洁的 API 接口,在每个功能目录的下面,有个叫 alipay_api.py 的 Python 文件,
24+
25+
里面定义了 Alipay_API 这个接口类,接口类提供统一的调用接口 alipay_submit,
26+
27+
alipay_submit 接口便是买家订单的提交入口,在项目的 application/zhi/controllers/ 下面的 zhi/dual_zhi/send_zhi/.py 脚本中可以作为参考。
28+
29+
30+
关于如何集成使用 Alipay_Python_API:
31+
==================
32+
1、商家支付宝账户配置:
33+
34+
在 third_part/ 下面的每个独立功能中,找到 alipay_config.py ,在 Alipay_Config 类中,定义自己的商家支付宝账户信息:包括 seller_email、partner、key
35+
36+
2、配置你自己的处理支付宝返回 URL:
37+
38+
在 third_part/ 下面的每个独立功能中,找到 alipay_api.py,在 Alipay_API 类中,找到 return_url、notify_url,改为你自己的 Web 服务器处理 URL.
39+
40+
3、接口获取订单提交:
41+
在 application/zhi/controllers/ 下面 zhi/dual_zhi/send_zhi/.py 脚本,可以作为参考。
42+
接口使用只需要 2 行代码:
43+
44+
1-1、New 一个订单提交对象
45+
46+
submit = Alipay_API()
47+
48+
1-2、提交订单,获取支付宝官方的 HTML 表单内容
49+
50+
html = submit.alipay_submit()
51+
52+
53+
54+
运行项目:
55+
==================
56+
# python2.7 manager.py runserver 0.0.0.0:8000
57+
58+
或者使用项目中给出的 uwsgi 方式开启 Web 服务器,但是要求你的服务器上面已经提供了 uwsgi 程式;
59+
60+
# uwsgi --http 0.0.0.0:8000 --chdir /opt/www/mysite/mysite/ --module django_uwsgi
61+
62+
63+
浏览器验证:
64+
==================
65+
1、即时到帐功能
66+
67+
浏览器输入 url http://192.168.1.199:8000
68+
69+
2、双功能(担保交易)
70+
71+
浏览器输入 url http://192.168.1.199:8000/dual
72+
73+
3、商家物流发货确认
74+
75+
浏览器输入 url http://192.168.1.199:8000/send
76+
77+
78+
79+
关于本 SDK 项目:
80+
===================
81+
这份支付宝商家集成 Python API,以及 Django 简单的集成 Demo 项目,是在帮助一位朋友做支付宝集成的过程中开发而来,
82+
83+
当初在集成的过程中,发现支付宝官方并没有提供商家支付宝交易的 Python SDK,而在网上,我也没有找到合适的 Alipay Python API,而 Github 上面已经有不少国内的开发者提供了一些开源版本,但是大都单独提供 Lib 或者 Python 代码,很少有提供完整的基于 Django 或者其他 Web Python 框架的实际生产 Demo,
84+
85+
所以我自己根据支付宝官方的文档 《即时到帐交易接口(create_direct_pay_by_user)接入与使用规则》、《标准双接口(trade_create_by_buyer)接入与使用规则)、《确认发货接口(send_goods_confirm_by_platform)接入与使用规则》,开发了这个 Alipay Python SDK,
86+
87+
在 Django 项目的 third_part 第三方目录里面,你可以找到对应的支付宝功能目录 SDK,
88+
89+
在开发的过程中,我也借鉴了支付宝官方提供的 demo 中的 PHP 接口实现,其中大部分的类、方法,在 Python 接口中都与 PHP 的方法一致,如果你也是一位同时熟悉 PHP 和 Python 开发者的话,相信自己在研究 third_part 下面代码的时候,会发现这一点,
90+
91+
此份 SDK 和配套的 Django 项目实际生产 Demo,如果说它做的比较好一点的地方,就是它不仅给出了易于使用的 Class API,而且同时给出了一个在 Web 生产环境下面使用的 Django Demo。
92+
93+
94+
关于作者:
95+
=====================
96+
Steven Wang,
97+
98+
如果你碰巧使用了这份 SDK、发现了 Bug、或者想学习和交流的话,
99+
100+
欢迎与我联系: [email protected]
101+
102+
Good Luck!

application/__init__.py

Whitespace-only changes.

application/__init__.pyc

146 Bytes
Binary file not shown.

application/zhi/__init__.py

Whitespace-only changes.

application/zhi/__init__.pyc

150 Bytes
Binary file not shown.

application/zhi/controllers/__init__.py

Whitespace-only changes.
162 Bytes
Binary file not shown.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env python
2+
#coding=utf-8
3+
4+
# import python env
5+
import os
6+
import sys
7+
8+
reload(sys)
9+
sys.setdefaultencoding('utf8')
10+
11+
# import django env
12+
from django.http import HttpResponse, HttpResponseRedirect
13+
from django.shortcuts import render_to_response
14+
from django.views.decorators.csrf import csrf_exempt
15+
from third_part.alipay_dual.alipay_api import *
16+
17+
def index(request):
18+
template_name = 'templates/zhifu_dual.html'
19+
return render_to_response(template_name)
20+
21+
@csrf_exempt
22+
def submit(request):
23+
if request.method == 'POST':
24+
out_trade_no = request.POST.get('out_trade_no', '')
25+
subject = request.POST.get('subject', '')
26+
price = request.POST.get('price', '')
27+
28+
quantity = request.POST.get('quantity', '')
29+
logistics_fee = request.POST.get('logistics_fee', '')
30+
logistics_type = request.POST.get('logistics_type', '')
31+
logistics_payment = request.POST.get('logistics_payment', '')
32+
33+
body = request.POST.get('body', '')
34+
show_url = request.POST.get('show_url', '')
35+
36+
receive_name = request.POST.get('receive_name', '')
37+
receive_address = request.POST.get('receive_address', '')
38+
receive_zip = request.POST.get('receive_zip', '')
39+
receive_phone = request.POST.get('receive_phone', '')
40+
receive_mobile = request.POST.get('receive_mobile', '')
41+
42+
html = '<p>订单已经提交,准备进入支付宝官方收银台 ...</p>'
43+
submit = Alipay_API()
44+
html = submit.alipay_submit(out_trade_no, subject, price, \
45+
quantity, logistics_fee, logistics_type, logistics_payment, \
46+
body, show_url, \
47+
receive_name, receive_address, receive_zip, receive_phone, receive_mobile)
48+
49+
return HttpResponse(html)
50+
51+
def return_url(request): # 支付宝同步通知跳转url,get 方式
52+
out_trade_no = request.GET.get('out_trade_no', '')
53+
trade_no = request.GET.get('trade_no', '') # 支付宝交易号
54+
trade_status = request.GET.get('trade_status', '')
55+
price = request.GET.get('price', '')
56+
57+
if trade_status == 'WAIT_SELLER_SEND_GOODS' or trade_status == 'TRADE_FINISHED':
58+
# // 支付宝返回的支付成功状态
59+
# // 可以再次加入平台的订单处理逻辑代码,如记录数据库,提供用户一下必要的服务信息等
60+
# // 注意:如果是异步通知的url,还可以根据订单号对数据库中的订单进行查询,判断是否已经处理了?
61+
62+
# todo ...
63+
logResult('============ 双功能 同步通知 ===============')
64+
logResult('订单号:' + out_trade_no)
65+
logResult('支付宝交易号:' + trade_no)
66+
logResult('订单费用:' + price)
67+
logResult('==========================================')
68+
69+
70+
return HttpResponse('订单购买成功!')
71+
72+
def notify_url(request): # 支付宝异步通知url,post 方式
73+
out_trade_no = request.POST.get('out_trade_no', '')
74+
trade_no = request.POST.get('trade_no', '')
75+
trade_status = request.POST.get("trade_status", '')
76+
77+
if trade_status == 'WAIT_BUYER_PAY': # 表示买家已在支付宝交易管理中产生了交易记录,但没有付款
78+
# todo ...
79+
pass
80+
elif trade_status == 'WAIT_SELLER_SEND_GOODS': # 表示买家已在支付宝交易管理中产生了交易记录且付款成功,但卖家没有发货
81+
# todo ...
82+
pass
83+
elif trade_status == 'WAIT_BUYER_CONFIRM_GOODS': # 表示卖家已经发了货,但买家还没有做确认收货的操作
84+
# todo ...
85+
pass
86+
elif trade_status == 'TRADE_FINISHED': # 表示买家已经确认收货,这笔交易完成
87+
# todo ...
88+
pass
89+
90+
# // Todo...
91+
logResult('============ 异步通知 ===============')
92+
logResult('订单号:' + out_trade_no)
93+
logResult('支付宝交易号:' + trade_no)
94+
logResult('====================================')
95+
96+
return HttpResponse('success') # donnot modify this
97+
98+
def logResult(msg): # 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
99+
log = 'third_part/alipay_direct/log_dual.txt'
100+
fi = open(log, 'a')
101+
fi.write(msg + "\n")
102+
fi.close()

0 commit comments

Comments
 (0)