-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathflask_weixin_pay.py
317 lines (257 loc) · 10.8 KB
/
flask_weixin_pay.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# -*- coding: utf-8 -*-
import time
import string
import random
import hashlib
import urllib2
from collections import namedtuple
try:
from flask import current_app, request
except ImportError:
current_app = None
request = None
try:
from lxml import etree
except ImportError:
from xml.etree import cElementTree as etree
except ImportError:
from xml.etree import ElementTree as etree
__all__ = ("WeixinPay", "WeixinPayError")
__version__ = "0.3.4"
__author__ = "Weicheng Zou <zwczou@gmail.com>"
StandaloneApplication = namedtuple("StandaloneApplication", ["config"])
class WeixinPayError(Exception):
def __init__(self, msg):
super(WeixinPayError, self).__init__(msg)
class WeixinPay(object):
def __init__(self, app=None):
self.opener = urllib2.build_opener(urllib2.HTTPSHandler())
if isinstance(app, dict):
app = StandaloneApplication(config=app)
if app is None:
self.app = current_app
else:
self.init_app(app)
self.app = app
def init_app(self, app):
app.config.setdefault("WEIXIN_APP_ID", "")
app.config.setdefault("WEIXIN_MCH_ID", "")
app.config.setdefault("WEIXIN_MCH_KEY", "")
app.config.setdefault("WEIXIN_NOTIFY_URL", "")
def _get_app_id(self):
return self.app.config["WEIXIN_APP_ID"]
def _set_app_id(self, app_id):
self.app.config["WEIXIN_APP_ID"] = app_id
app_id = property(_get_app_id, _set_app_id)
del _set_app_id, _get_app_id
def _get_mch_id(self):
return self.app.config["WEIXIN_MCH_ID"]
def _set_mch_id(self, mch_id):
self.app.config["WEIXIN_MCH_ID"] = mch_id
mch_id = property(_get_mch_id, _set_mch_id)
del _get_mch_id, _set_mch_id
def _get_mch_key(self):
return self.app.config["WEIXIN_MCH_KEY"]
def _set_mch_key(self, mch_key):
self.app.config["WEIXIN_MCH_KEY"] = mch_key
mch_key = property(_get_mch_key, _set_mch_key)
del _get_mch_key, _set_mch_key
def _get_notify_url(self):
return self.app.config["WEIXIN_NOTIFY_URL"]
def _set_notify_url(self, notify_url):
self.app.config["WEIXIN_NOTIFY_URL"] = notify_url
notify_url = property(_get_notify_url, _set_notify_url)
del _get_notify_url, _set_notify_url
@property
def remote_addr(self):
return request.remote_addr if request else ""
@property
def nonce_str(self):
char = string.ascii_letters + string.digits
return "".join(random.choice(char) for _ in range(32))
to_utf8 = lambda self, x: x.encode("utf-8") if isinstance(x, unicode) else x
def sign(self, raw):
"""
生成签名
参考微信签名生成算法
https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3
"""
raw = [(k, str(raw[k]) if isinstance(raw[k], (int, float)) else raw[k]) \
for k in sorted(raw.keys())]
s = "&".join("=".join(kv) for kv in raw if kv[1])
s += "&key={0}".format(self.mch_key)
return hashlib.md5(self.to_utf8(s)).hexdigest().upper()
def check(self, raw):
"""
验证签名是否正确
"""
sign = raw.pop("sign")
return sign == self.sign(raw)
def to_xml(self, raw):
s = ""
for k, v in raw.iteritems():
s += "<{0}>{1}</{0}>".format(k, self.to_utf8(v), k)
return "<xml>{0}</xml>".format(s)
def to_dict(self, content):
raw = {}
root = etree.fromstring(content)
for child in root:
raw[child.tag] = child.text
return raw
def fetch(self, url, data):
req = urllib2.Request(url, data=self.to_xml(data))
try:
resp = self.opener.open(req, timeout=20)
except urllib2.HTTPError, e:
resp = e
return self.to_dict(resp.read())
def reply(self, msg, ok=True):
code = "SUCCESS" if ok else "FAIL"
return self.to_xml(dict(return_code=code, return_msg=msg))
def unified_order(self, **data):
"""
统一下单
out_trade_no、body、total_fee、trade_type必填
app_id, mchid, nonce_str自动填写
user_ip 在flask框架下可以自动填写
"""
url = "https://api.mch.weixin.qq.com/pay/unifiedorder"
# 必填参数
if "out_trade_no" not in data:
raise WeixinPayError("缺少统一支付接口必填参数out_trade_no")
if "body" not in data:
raise WeixinPayError("缺少统一支付接口必填参数body")
if "total_fee" not in data:
raise WeixinPayError("缺少统一支付接口必填参数total_fee")
if "trade_type" not in data:
raise WeixinPayError("缺少统一支付接口必填参数trade_type")
# 关联参数
if data["trade_type"] == "JSAPI" and "openid" not in data:
raise WeixinPayError("trade_type为JSAPI时,openid为必填参数")
if data["trade_type"] == "NATIVE" and "product_id" not in data:
raise WeixinPayError("trade_type为NATIVE时,product_id为必填参数")
data.setdefault("appid", self.app_id)
data.setdefault("mch_id", self.mch_id)
data.setdefault("notify_url", self.notify_url)
data.setdefault("nonce_str", self.nonce_str)
data.setdefault("spbill_create_ip", self.remote_addr)
data.setdefault("sign", self.sign(data))
raw = self.fetch(url, data)
if raw["return_code"] == "FAIL":
raise WeixinPayError(raw["return_msg"])
err_msg = raw.get("err_code_des")
if err_msg:
raise WeixinPayError(err_msg)
return raw
def jsapi(self, **kwargs):
"""
生成给JavaScript调用的数据
详细规则参考 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
"""
kwargs.setdefault("trade_type", "JSAPI")
raw = self.unified_order(**kwargs)
package = "prepay_id={0}".format(raw["prepay_id"])
timestamp = int(time.time())
nonce_str = self.nonce_str
raw = dict(appId=self.app_id, timeStamp=timestamp,
nonceStr=nonce_str, package=package, signType="MD5")
sign = self.sign(raw)
return dict(package=package, appId=self.app_id,
timeStamp=timestamp, nonceStr=nonce_str, sign=sign)
def order_query(self, **data):
"""
订单查询
out_trade_no, transaction_id至少填一个
appid, mchid, nonce_str不需要填入
"""
url = "https://api.mch.weixin.qq.com/pay/orderquery"
if "out_trade_no" not in data and "transaction_id" not in data:
raise WeixinPayError("订单查询接口中,out_trade_no、transaction_id至少填一个")
data.setdefault("appid", self.app_id)
data.setdefault("mch_id", self.mch_id)
data.setdefault("nonce_str", self.nonce_str)
data.setdefault("sign", self.sign(data))
raw = self.fetch(url, data)
if raw["return_code"] == "FAIL":
raise WeixinPayError(raw["return_msg"])
return raw
def close_order(self, out_trade_no, **data):
"""
关闭订单
transaction_id必填
appid, mchid, nonce_str不需要填入
"""
url = "https://api.mch.weixin.qq.com/pay/closeorder"
data.setdefault("out_trace_no", out_trade_no)
data.setdefault("appid", self.app_id)
data.setdefault("mch_id", self.mch_id)
data.setdefault("nonce_str", self.nonce_str)
data.setdefault("sign", self.sign(data))
raw = self.fetch(url, data)
if raw["return_code"] == "FAIL":
raise WeixinPayError(raw["return_msg"])
return raw
def refund(self, **data):
"""
申请退款
out_trade_no、transaction_id至少填一个且
out_refund_no、total_fee、refund_fee、op_user_id为必填参数
appid、mchid、nonce_str不需要填入
"""
url = "https://api.mch.weixin.qq.com/secapi/pay/refund"
if "out_trade_no" not in data and "transaction_id" not in data:
raise WeixinPayError("订单查询接口中,out_trade_no、transaction_id至少填一个")
if "out_refund_no" not in data:
raise WeixinPayError("退款申请接口中,缺少必填参数out_refund_no");
if "total_fee" not in data:
raise WeixinPayError("退款申请接口中,缺少必填参数total_fee");
if "refund_fee" not in data:
raise WeixinPayError("退款申请接口中,缺少必填参数refund_fee");
if "op_user_id" not in data:
raise WeixinPayError("退款申请接口中,缺少必填参数op_user_id");
data.setdefault("appid", self.app_id)
data.setdefault("mch_id", self.mch_id)
data.setdefault("nonce_str", self.nonce_str)
data.setdefault("sign", self.sign(data))
raw = self.fetch(url, data)
if raw["return_code"] == "FAIL":
raise WeixinPayError(raw["return_msg"])
return raw
def refund_query(self, **data):
"""
查询退款
提交退款申请后,通过调用该接口查询退款状态。退款有一定延时,
用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。
out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个
appid、mchid、nonce_str不需要填入
"""
url = "https://api.mch.weixin.qq.com/pay/refundquery"
if "out_refund_no" not in data and "out_trade_no" not in data \
and "transaction_id" not in data and "refund_id" not in data:
raise WeixinPayError("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个")
data.setdefault("appid", self.app_id)
data.setdefault("mch_id", self.mch_id)
data.setdefault("nonce_str", self.nonce_str)
data.setdefault("sign", self.sign(data))
raw = self.fetch(url, data)
if raw["return_code"] == "FAIL":
raise WeixinPayError(raw["return_msg"])
return raw
def download_bill(self, bill_date, **data):
"""
下载对账单
bill_date为必填参数
appid、mchid、nonce_str不需要填入
"""
url = "https://api.mch.weixin.qq.com/pay/downloadbill"
if "bill_date" not in data:
raise WeixinPayError("对账单接口中,缺少必填参数bill_date")
data.setdefault("bill_date", bill_date)
data.setdefault("appid", self.app_id)
data.setdefault("mch_id", self.mch_id)
data.setdefault("nonce_str", self.nonce_str)
data.setdefault("sign", self.sign(data))
raw = self.fetch(url, data)
if raw["return_code"] == "FAIL":
raise WeixinPayError(raw["return_msg"])
return raw