Skip to content

Commit a530b2e

Browse files
author
Jolly23
committed
fix: dev: 代码优化,注释调整,更新旧接口保证全部功能正常使用
1 parent 6f5bae8 commit a530b2e

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

wx_pay.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
import requests
88

99
try:
10-
from xml.etree import cElementTree as ETree
10+
from flask import request
1111
except ImportError:
12-
from xml.etree import ElementTree as ETree
12+
request = None
1313

1414
try:
15-
from flask import request
15+
from xml.etree import cElementTree as ETree
1616
except ImportError:
17-
request = None
17+
from xml.etree import ElementTree as ETree
1818

1919

2020
class WxPayError(Exception):
@@ -93,13 +93,20 @@ def fetch(self, url, data):
9393
except ETree.ParseError:
9494
return re_info
9595

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+
96101
def reply(self, msg, ok=True):
97102
code = "SUCCESS" if ok else "FAIL"
98103
return self.to_xml(dict(return_code=code, return_msg=msg))
99104

100105
def unified_order(self, **data):
101106
"""
102107
统一下单
108+
详细规则参考 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
109+
103110
:param data: out_trade_no, body, total_fee, trade_type
104111
out_trade_no: 商户订单号
105112
body: 商品描述
@@ -148,12 +155,13 @@ def js_pay_api(self, **kwargs):
148155
"""
149156
生成给JavaScript调用的数据
150157
详细规则参考 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
158+
151159
:param kwargs: openid, body, total_fee
152160
openid: 用户openid
153161
body: 商品名称
154162
total_fee: 标价金额, 整数, 单位 分
155163
out_trade_no: 商户订单号, 若未传入则自动生成
156-
:return:
164+
:return: 生成微信JS接口支付所需的信息
157165
"""
158166
kwargs.setdefault("trade_type", "JSAPI")
159167
if "out_trade_no" not in kwargs:
@@ -213,13 +221,13 @@ def close_order(self, out_trade_no):
213221
raise WxPayError(raw["return_msg"])
214222
return raw
215223

216-
def refund(self, api_client_cert_path, api_client_key_path, **data):
224+
def refund(self, api_cert_path, api_key_path, **data):
217225
"""
218226
申请退款
219227
详细规则参考 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
220228
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)需要先到微信支付商户平台获取,下载后保存至服务器
223231
:param data: out_trade_no、transaction_id至少填一个, out_refund_no, total_fee, refund_fee
224232
out_trade_no: 商户订单号
225233
transaction_id: 微信订单号
@@ -244,8 +252,7 @@ def refund(self, api_client_cert_path, api_client_key_path, **data):
244252
data.setdefault("nonce_str", self.nonce_str())
245253
data.setdefault("sign", self.sign(data))
246254

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)
249256
if raw["return_code"] == "FAIL":
250257
raise WxPayError(raw["return_msg"])
251258
return raw
@@ -302,13 +309,13 @@ def download_bill(self, bill_date, bill_type=None):
302309
raw = self.fetch(url, data)
303310
return raw
304311

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):
306313
"""
307314
发给用户微信红包
308315
详细规则参考 https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
309316
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)需要先到微信支付商户平台获取,下载后保存至服务器
312319
:param data: send_name, re_openid, total_amount, wishing, client_ip, act_name, remark
313320
send_name: 商户名称 例如: 天虹百货
314321
re_openid: 用户openid
@@ -345,25 +352,26 @@ def send_red_pack(self, api_client_cert_path, api_client_key_path, **data):
345352
data.setdefault("scene_id", 'PRODUCT_4')
346353
data.setdefault("sign", self.sign(data))
347354

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)
350356
if raw["return_code"] == "FAIL":
351357
raise WxPayError(raw["return_msg"])
352358
return raw
353359

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):
355361
"""
356362
使用企业对个人付款功能
357363
详细规则参考 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: 企业转账结果
367375
"""
368376
url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"
369377
if "openid" not in data:
@@ -372,7 +380,6 @@ def enterprise_payment(self, api_client_cert_path, api_client_key_path, **data):
372380
raise WxPayError(u"企业付款申请接口中,缺少必填参数check_name")
373381
if data['check_name'] and "re_user_name" not in data:
374382
raise WxPayError(u"企业付款申请接口中,缺少必填参数re_user_name")
375-
376383
if "amount" not in data:
377384
raise WxPayError(u"企业付款申请接口中,缺少必填参数amount")
378385
if "desc" not in data:
@@ -390,9 +397,7 @@ def enterprise_payment(self, api_client_cert_path, api_client_key_path, **data):
390397
'NO_CHECK')
391398
data.setdefault("sign", self.sign(data))
392399

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)
396401
if raw["return_code"] == "FAIL":
397402
raise WxPayError(raw["return_msg"])
398403
return raw

0 commit comments

Comments
 (0)