Skip to content

Commit 7edcab1

Browse files
committed
修复POST接口签名错误
1 parent 2c7bd73 commit 7edcab1

File tree

4 files changed

+34
-42
lines changed

4 files changed

+34
-42
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name="wechatpayv3",
9-
version="0.44",
9+
version="0.45",
1010
author="minibear",
1111
description="微信支付接口V3版python库",
1212
long_description=long_description,

wechatpayv3/api.py

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ def pay(self,
6565
if description:
6666
params.update({'description': description})
6767
else:
68-
raise WeChatPayException('description is not assigned.')
68+
raise Exception('description is not assigned.')
6969
if out_trade_no:
7070
params.update({'out_trade_no': out_trade_no})
7171
else:
72-
raise WeChatPayException('out_trade_no is not assigned.')
72+
raise Exception('out_trade_no is not assigned.')
7373
if amount:
7474
params.update({'amount': amount})
7575
else:
76-
raise WeChatPayException('amount is not assigned.')
76+
raise Exception('amount is not assigned.')
7777
if payer:
7878
params.update({'payer': payer})
7979
if scene_info:
@@ -90,37 +90,37 @@ def pay(self,
9090
params.update({'settle_info': settle_info})
9191
if self._type in [WeChatPayType.JSAPI, WeChatPayType.MINIPROG]:
9292
if not payer:
93-
raise WeChatPayException('payer is not assigned')
93+
raise Exception('payer is not assigned')
9494
path = '/v3/pay/transactions/jsapi'
9595
elif self._type == WeChatPayType.APP:
9696
path = '/v3/pay/transactions/app'
9797
elif self._type == WeChatPayType.H5:
9898
if not scene_info:
99-
raise WeChatPayException('scene_info is not assigned.')
99+
raise Exception('scene_info is not assigned.')
100100
path = '/v3/pay/transactions/h5'
101101
elif self._type == WeChatPayType.NATIVE:
102102
path = '/v3/pay/transactions/native'
103-
return self._core.post(path, json=json.dumps(params))
103+
return self._core.post(path, data=params)
104104

105105
def close(self, out_trade_no):
106106
"""关闭订单
107107
:param out_trade_no: 商户订单号,示例值:'1217752501201407033233368018'
108108
"""
109109
if not out_trade_no:
110-
raise WeChatPayException('out_trade_no is not assigned.')
110+
raise Exception('out_trade_no is not assigned.')
111111
path = '/v3/pay/transactions/out-trade-no/%s/close' % out_trade_no
112112
params = {}
113113
params['mchid'] = self._mchid
114114
params['out_trade_no'] = out_trade_no
115-
return self._core.post(path, json=json.dumps(params))
115+
return self._core.post(path, data=params)
116116

117117
def query(self, transaction_id=None, out_trade_no=None):
118118
"""查询订单
119119
:param transaction_id: 微信支付订单号,示例值:1217752501201407033233368018
120120
:param out_trade_no: 商户订单号,示例值:1217752501201407033233368018
121121
"""
122122
if not (transaction_id or out_trade_no):
123-
raise WeChatPayException('params is not assigned')
123+
raise Exception('params is not assigned')
124124
if transaction_id:
125125
path = '/v3/pay/transactions/id/%s' % transaction_id
126126
else:
@@ -152,11 +152,11 @@ def refund(self,
152152
if out_refund_no:
153153
params.update({'out_refund_no': out_refund_no})
154154
else:
155-
raise WeChatPayException('out_refund_no is not assigned.')
155+
raise Exception('out_refund_no is not assigned.')
156156
if amount:
157157
params.update({'amount': amount})
158158
else:
159-
raise WeChatPayException('amount is not assigned.')
159+
raise Exception('amount is not assigned.')
160160
if transaction_id:
161161
params.update({'transation_id': transaction_id})
162162
if out_trade_no:
@@ -168,7 +168,7 @@ def refund(self,
168168
if goods_detail:
169169
params.update({'goods_detail': goods_detail})
170170
path = '/v3/refund/domestic/refunds'
171-
return self._core.post(path, json=json.dumps(params))
171+
return self._core.post(path, data=params)
172172

173173
def query_refund(self, out_refund_no):
174174
"""查询单笔退款
@@ -239,11 +239,11 @@ def combine_pay(self,
239239
if combine_out_trade_no:
240240
params.update({'combine_out_trade_no': combine_out_trade_no})
241241
else:
242-
raise WeChatPayException('combine_out_trade_no is not assigned.')
242+
raise Exception('combine_out_trade_no is not assigned.')
243243
if sub_orders:
244244
params.update({'sub_orders': sub_orders})
245245
else:
246-
raise WeChatPayException('sub_orders is not assigned.')
246+
raise Exception('sub_orders is not assigned.')
247247
if scene_info:
248248
params.update({'scene_info': scene_info})
249249
if combine_payer_info:
@@ -254,25 +254,25 @@ def combine_pay(self,
254254
params.update({'time_expire': time_expire})
255255
if self._type in [WeChatPayType.JSAPI, WeChatPayType.MINIPROG]:
256256
if not combine_payer_info:
257-
raise WeChatPayException('combine_payer_info is not assigned')
257+
raise Exception('combine_payer_info is not assigned')
258258
path = '/v3/combine-transactions/jsapi'
259259
elif self._type == WeChatPayType.APP:
260260
path = '/v3/combine-transactions/app'
261261
elif self._type == WeChatPayType.H5:
262262
if not scene_info:
263-
raise WeChatPayException('scene_info is not assigned.')
263+
raise Exception('scene_info is not assigned.')
264264
path = '/v3/combine-transactions/h5'
265265
elif self._type == WeChatPayType.NATIVE:
266266
path = '/v3/combine-transactions/native'
267-
return self._core.post(path, json=json.dumps(params))
267+
return self._core.post(path, data=params)
268268

269269
def combine_query(self, combine_out_trade_no):
270270
"""合单查询订单
271271
:param combine_out_trade_no: 合单商户订单号,示例值:P20150806125346
272272
"""
273273
params = {}
274274
if not combine_out_trade_no:
275-
raise WeChatPayException('param combine_out_trade_no is not assigned')
275+
raise Exception('param combine_out_trade_no is not assigned')
276276
else:
277277
params.update({'combine_out_trade_no':combine_out_trade_no})
278278
path = '/v3/combine-transactions/out-trade-no/%s' % combine_out_trade_no
@@ -288,21 +288,13 @@ def combine_close(self, combine_out_trade_no, sub_orders, combine_appid=None):
288288
params['combine_appid'] = combine_appid or self._appid
289289

290290
if not combine_out_trade_no:
291-
raise WeChatPayException('combine_out_trade_no is not assigned.')
291+
raise Exception('combine_out_trade_no is not assigned.')
292292
if not sub_orders:
293-
raise WeChatPayException('sub_orders is not assigned.')
293+
raise Exception('sub_orders is not assigned.')
294294
else:
295295
params.update({'sub_orders': sub_orders})
296296
path = '/v3/combine-transactions/out-trade-no/%s/close' % combine_out_trade_no
297-
return self._core.post(path, json=json.dumps(params))
298-
299-
class WeChatPayException(Exception):
300-
def __init__(self, reason):
301-
self._reason = reason
302-
303-
def __str__(self):
304-
return self._reason
305-
297+
return self._core.post(path, data=params)
306298

307299
class WeChatPayType(Enum):
308300
JSAPI = 0

wechatpayv3/core.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from .utils import build_authorization
33
import requests
4-
5-
4+
import json
65
class Core():
76
def __init__(self, mchid, mch_key_serial_no, mch_private_key, wechat_public_key):
87
self._mchid = mchid
@@ -15,20 +14,20 @@ def get(self, path):
1514
headers = {}
1615
headers.update({'Content-Type': 'application/json'})
1716
headers.update({'Accept': 'application/json'})
18-
headers.update({'User-Agent': 'wechatpay v3 python sdk v0.1'})
17+
headers.update({'User-Agent': 'wechatpay v3 python sdk'})
1918
headers.update({'Authorization': build_authorization(
2019
path, 'GET', self._mchid, self._mch_key_serial_no, self._mch_private_key)})
2120
response = requests.get(url=self._gate_way + path, headers=headers)
2221
return response.status_code, response.text
2322

24-
def post(self, path, json=None):
23+
def post(self, path, data=None):
2524
headers = {}
2625
headers.update({'Content-Type': 'application/json'})
2726
headers.update({'Accept': 'application/json'})
28-
headers.update({'User-Agent': 'wechatpay v3 python sdk v0.1'})
29-
headers.update({'Authorization': build_authorization(
30-
path, 'POST', self._mchid, self._mch_key_serial_no, self._mch_private_key, json)})
27+
headers.update({'User-Agent': 'wechatpay v3 python sdk'})
28+
authorization = build_authorization(path, 'POST', self._mchid, self._mch_key_serial_no, self._mch_private_key, data=json.dumps(data))
29+
headers.update({'Authorization': authorization})
3130
response = requests.post(self._gate_way + path,
32-
json=json,
31+
json=data,
3332
headers=headers)
3433
return response.status_code, response.text

wechatpayv3/utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ def build_authorization(path,
1616
mchid,
1717
serial_no,
1818
mch_private_key,
19-
body=None,
19+
data=None,
2020
nonce_str=None):
2121
timeStamp = str(int(time.time()))
2222
nonce_str = nonce_str or ''.join(str(uuid.uuid4()).split('-')).upper()
23-
body = body if body else ''
23+
body = data if data else ''
2424
sign_str = method + '\n' + path + '\n' + \
2525
timeStamp + '\n' + nonce_str + '\n' + body + '\n'
26-
authorization = 'WECHATPAY2-SHA256-RSA2048 mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%s",signature="%s"' % (
27-
mchid, serial_no, nonce_str, timeStamp, sign(mch_private_key=mch_private_key, sign_str=sign_str))
26+
signature = sign(mch_private_key=mch_private_key, sign_str=sign_str)
27+
authorization = 'WECHATPAY2-SHA256-RSA2048 mchid="%s",nonce_str="%s",signature="%s",timestamp="%s",serial_no="%s"' % (
28+
mchid, nonce_str, signature, timeStamp, serial_no)
2829
return authorization
2930

3031

0 commit comments

Comments
 (0)