7
7
import requests
8
8
9
9
try :
10
- from xml . etree import cElementTree as ETree
10
+ from flask import request
11
11
except ImportError :
12
- from xml . etree import ElementTree as ETree
12
+ request = None
13
13
14
14
try :
15
- from flask import request
15
+ from xml . etree import cElementTree as ETree
16
16
except ImportError :
17
- request = None
17
+ from xml . etree import ElementTree as ETree
18
18
19
19
20
20
class WxPayError (Exception ):
@@ -93,13 +93,20 @@ def fetch(self, url, data):
93
93
except ETree .ParseError :
94
94
return re_info
95
95
96
+ def fetch_with_ssl (self , url , data , api_client_cert_path , api_client_key_path ):
97
+ req = requests .post (url , data = self .to_xml (data ),
98
+ cert = (api_client_cert_path , api_client_key_path ))
99
+ return self .to_dict (req .content )
100
+
96
101
def reply (self , msg , ok = True ):
97
102
code = "SUCCESS" if ok else "FAIL"
98
103
return self .to_xml (dict (return_code = code , return_msg = msg ))
99
104
100
105
def unified_order (self , ** data ):
101
106
"""
102
107
统一下单
108
+ 详细规则参考 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
109
+
103
110
:param data: out_trade_no, body, total_fee, trade_type
104
111
out_trade_no: 商户订单号
105
112
body: 商品描述
@@ -148,12 +155,13 @@ def js_pay_api(self, **kwargs):
148
155
"""
149
156
生成给JavaScript调用的数据
150
157
详细规则参考 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
158
+
151
159
:param kwargs: openid, body, total_fee
152
160
openid: 用户openid
153
161
body: 商品名称
154
162
total_fee: 标价金额, 整数, 单位 分
155
163
out_trade_no: 商户订单号, 若未传入则自动生成
156
- :return:
164
+ :return: 生成微信JS接口支付所需的信息
157
165
"""
158
166
kwargs .setdefault ("trade_type" , "JSAPI" )
159
167
if "out_trade_no" not in kwargs :
@@ -213,13 +221,13 @@ def close_order(self, out_trade_no):
213
221
raise WxPayError (raw ["return_msg" ])
214
222
return raw
215
223
216
- def refund (self , api_client_cert_path , api_client_key_path , ** data ):
224
+ def refund (self , api_cert_path , api_key_path , ** data ):
217
225
"""
218
226
申请退款
219
227
详细规则参考 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
220
228
221
- :param api_client_cert_path : 微信支付商户证书路径,此证书(apiclient_cert.pem)需要先到微信支付商户平台获取,下载后保存至服务器
222
- :param api_client_key_path : 微信支付商户证书路径,此证书(apiclient_key.pem)需要先到微信支付商户平台获取,下载后保存至服务器
229
+ :param api_cert_path : 微信支付商户证书路径,此证书(apiclient_cert.pem)需要先到微信支付商户平台获取,下载后保存至服务器
230
+ :param api_key_path : 微信支付商户证书路径,此证书(apiclient_key.pem)需要先到微信支付商户平台获取,下载后保存至服务器
223
231
:param data: out_trade_no、transaction_id至少填一个, out_refund_no, total_fee, refund_fee
224
232
out_trade_no: 商户订单号
225
233
transaction_id: 微信订单号
@@ -244,8 +252,7 @@ def refund(self, api_client_cert_path, api_client_key_path, **data):
244
252
data .setdefault ("nonce_str" , self .nonce_str ())
245
253
data .setdefault ("sign" , self .sign (data ))
246
254
247
- req = requests .post (url , data = self .to_xml (data ), cert = (api_client_cert_path , api_client_key_path ))
248
- raw = self .to_dict (req .content )
255
+ raw = self .fetch_with_ssl (url , data , api_cert_path , api_key_path )
249
256
if raw ["return_code" ] == "FAIL" :
250
257
raise WxPayError (raw ["return_msg" ])
251
258
return raw
@@ -302,13 +309,13 @@ def download_bill(self, bill_date, bill_type=None):
302
309
raw = self .fetch (url , data )
303
310
return raw
304
311
305
- def send_red_pack (self , api_client_cert_path , api_client_key_path , ** data ):
312
+ def send_red_pack (self , api_cert_path , api_key_path , ** data ):
306
313
"""
307
314
发给用户微信红包
308
315
详细规则参考 https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
309
316
310
- :param api_client_cert_path : 微信支付商户证书路径,此证书(apiclient_cert.pem)需要先到微信支付商户平台获取,下载后保存至服务器
311
- :param api_client_key_path : 微信支付商户证书路径,此证书(apiclient_key.pem)需要先到微信支付商户平台获取,下载后保存至服务器
317
+ :param api_cert_path : 微信支付商户证书路径,此证书(apiclient_cert.pem)需要先到微信支付商户平台获取,下载后保存至服务器
318
+ :param api_key_path : 微信支付商户证书路径,此证书(apiclient_key.pem)需要先到微信支付商户平台获取,下载后保存至服务器
312
319
:param data: send_name, re_openid, total_amount, wishing, client_ip, act_name, remark
313
320
send_name: 商户名称 例如: 天虹百货
314
321
re_openid: 用户openid
@@ -345,25 +352,26 @@ def send_red_pack(self, api_client_cert_path, api_client_key_path, **data):
345
352
data .setdefault ("scene_id" , 'PRODUCT_4' )
346
353
data .setdefault ("sign" , self .sign (data ))
347
354
348
- req = requests .post (url , data = self .to_xml (data ), cert = (api_client_cert_path , api_client_key_path ))
349
- raw = self .to_dict (req .content )
355
+ raw = self .fetch_with_ssl (url , data , api_cert_path , api_key_path )
350
356
if raw ["return_code" ] == "FAIL" :
351
357
raise WxPayError (raw ["return_msg" ])
352
358
return raw
353
359
354
- def enterprise_payment (self , api_client_cert_path , api_client_key_path , ** data ):
360
+ def enterprise_payment (self , api_cert_path , api_key_path , ** data ):
355
361
"""
356
362
使用企业对个人付款功能
357
363
详细规则参考 https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2
358
- 参数:
359
- api_client_cert_path: 微信支付商户证书路径,此证书(apiclient_cert.pem)需要先到微信支付商户平台获取,下载后保存至服务器。
360
- api_client_key_path: 微信支付商户证书路径,此证书(apiclient_key.pem)需要先到微信支付商户平台获取,下载后保存至服务器。
361
- openid: 用户openid
362
- check_name: 是否校验用户姓名
363
- re_user_name: 如果 check_name 为True,则填写,否则不带此参数
364
- amount: 金额: 企业付款金额,单位为分
365
- desc: 企业付款描述信息
366
- spbill_create_ip: 调用接口的机器Ip地址, 注:此地址为服务器地址
364
+
365
+ :param api_cert_path: 微信支付商户证书路径,此证书(apiclient_cert.pem)需要先到微信支付商户平台获取,下载后保存至服务器
366
+ :param api_key_path: 微信支付商户证书路径,此证书(apiclient_key.pem)需要先到微信支付商户平台获取,下载后保存至服务器
367
+ :param data: openid, check_name, re_user_name, amount, desc, spbill_create_ip
368
+ openid: 用户openid
369
+ check_name: 是否校验用户姓名
370
+ re_user_name: 如果 check_name 为True,则填写,否则不带此参数
371
+ amount: 金额: 企业付款金额,单位为分
372
+ desc: 企业付款描述信息
373
+ spbill_create_ip: 调用接口的机器Ip地址, 注:此地址为服务器地址
374
+ :return: 企业转账结果
367
375
"""
368
376
url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"
369
377
if "openid" not in data :
@@ -372,7 +380,6 @@ def enterprise_payment(self, api_client_cert_path, api_client_key_path, **data):
372
380
raise WxPayError (u"企业付款申请接口中,缺少必填参数check_name" )
373
381
if data ['check_name' ] and "re_user_name" not in data :
374
382
raise WxPayError (u"企业付款申请接口中,缺少必填参数re_user_name" )
375
-
376
383
if "amount" not in data :
377
384
raise WxPayError (u"企业付款申请接口中,缺少必填参数amount" )
378
385
if "desc" not in data :
@@ -390,9 +397,7 @@ def enterprise_payment(self, api_client_cert_path, api_client_key_path, **data):
390
397
'NO_CHECK' )
391
398
data .setdefault ("sign" , self .sign (data ))
392
399
393
- req = requests .post (url , data = self .to_xml (data ), cert = (api_client_cert_path , api_client_key_path ))
394
-
395
- raw = self .to_dict (req .content )
400
+ raw = self .fetch_with_ssl (url , data , api_cert_path , api_key_path )
396
401
if raw ["return_code" ] == "FAIL" :
397
402
raise WxPayError (raw ["return_msg" ])
398
403
return raw
0 commit comments