-
Notifications
You must be signed in to change notification settings - Fork 0
/
alipay.py
121 lines (102 loc) · 4.33 KB
/
alipay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
# encoding: utf-8
'''
Created on 2011-1-5
@author: codeback@gmail.com
'''
import hashlib
import urllib2
verfyURL="https://mapi.alipay.com/gateway.do?service=notify_verify"
gateway="https://mapi.alipay.com/gateway.do"
class alipay:
def __init__(self,
partner="您的淘宝身份",
key="您的淘宝Key",
sellermail="卖家邮箱",
notifyurl="异步通知回调URL",
returnurl="跳转回调URL",
showurl="产品页面"):
self.key=key;
self.conf={
'partner' : partner,
'service' : "create_partner_trade_by_buyer",
'payment_type' : "1",
'seller_email' : sellermail,
'notify_url' : notifyurl,
'return_url' : returnurl,
'show_url' : showurl,
'_input_charset' : "UTF-8",
'sign_type' : "MD5",
#其他参数,如果有默认值定义在下面:
'paymethod' : "",
'mainname' : "",
}
def populateURLStr(self,params):
ks=params.keys()
ks.sort()
rlt=''
for k in ks:
if params[k]==None or len(params[k])==0 \
or k=="sign" or k=="sign_type" or k=="key":
continue
rlt=rlt+"&%s=%s"%(k,params[k])
print "URL:"+rlt[1:]
return rlt[1:]
def buildSign(self,params):
sign=hashlib.md5(self.populateURLStr(params)+self.key).hexdigest()
print "md5 sign is %s" % sign;
return sign
'''
校验支付宝返回的参数,交易成功的通知回调.
校验分为两个步骤:检查签名是否正确、访问支付宝确认当前数据是由支付宝返回。
params为支付宝传回的数据。
'''
def notifiyCall(self,params,verify=True,transport="http"):
sign=None
if params.has_key('sign'):
sign=params['sign']
locSign=self.buildSign(params)
if sign==None or locSign!=sign:
print "sign error."
return "fail"
#if params['trade_status']!='TRADE_FINISHED' and params['trade_status']!='TRADE_SUCCESS':
#return "fail"
if not verify:
return "success"
else:
print "Verify the request is call by alipay.com...."
url = verfyURL + "&partner=%s¬ify_id=%s"%(self.conf['partner'],params['notify_id'])
response=urllib2.urlopen(url)
html=response.read()
print "aliypay.com return: %s" % html
if html=='true':
return "success"
else:
return "fail"
'''
生成提交到支付宝的表单,用户通过此表单将订单信息提交到支付宝。
由params参数提供订单信息,必须包含以下几项内容:
out_trade_no:订单号
subject :订单名称、或商品名称
body :订单备注、描述
total_fee :总额
'''
def createPayForm(self,params,method="POST",title="确认,支付宝付款"):
params.update(self.conf)
sign=self.buildSign(params)
params['sign']=sign
ele=""
for nm in params:
print "key in params : %s"%nm
if params[nm]==None or len(params[nm])==0 or nm=='_input_charset':
continue
ele = ele + " <input type='hidden' name='%s' value='%s' />" % (nm,params[nm])
html='''
<form name='alipaysubmit' action='%s?_input_charset=%s' method='%s' >
%s
<li class="sub_li"><button class="test" id="btn_go" type="submit" value="%s" onclick="javascript:this.form.action=%s">确认无误,付款咯~</button></li>
<li class="sub_li"><button class="test" id="btn_later" type=submit onclick="javascript:this.form.action='/nopay'" >以后再付</button></li>
<li class="sub_li"><button class="test" id="btn_rtn" onclick="javascript:this.form.action='/'" >返回</button></li>
</form>
''' % (gateway,params['_input_charset'],method,ele,title,gateway)
return html