Skip to content

Commit 2c7bd73

Browse files
committed
增加合单下单、查询、关闭
1 parent 4629fd1 commit 2c7bd73

File tree

6 files changed

+189
-16
lines changed

6 files changed

+189
-16
lines changed

README.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,74 @@ wxpay = WeChatPay(wechatpay_type=WeChatPayType.MINIPROG,
3838
```
3939

4040
### 接口
41-
参考examples.py
41+
42+
``` python
43+
# 下载证书
44+
def certificate():
45+
code, message = wxpay.certificate()
46+
print('code: %s, message: %s' % (code, message))
47+
48+
# 统一下单
49+
def pay():
50+
code, message = wxpay.pay(description='demo-description',
51+
out_trade_no='demo-trade-no',
52+
amount={'total': 100},
53+
payer={'openid': 'demo-openid'})
54+
print('code: %s, message: %s' % (code, message))
55+
56+
# 订单查询
57+
def query():
58+
code, message = wxpay.query(transaction_id='demo-transation-id')
59+
print('code: %s, message: %s' % (code, message))
60+
61+
# 关闭订单
62+
def close():
63+
code, message = wxpay.close(out_trade_no='demo-out-trade-no')
64+
print('code: %s, message: %s' % (code, message))
65+
66+
# 申请退款
67+
def refund():
68+
code, message = wxpay.refund(transaction_id='demo-transation-id',
69+
out_refund_no='demo-out-refund-no',
70+
amount={'refund': 100, 'total': 100, 'currency': 'CNY'})
71+
print('code: %s, message: %s' % (code, message))
72+
73+
# 退款查询
74+
def query_refund():
75+
code, message = wxpay.query_refund(out_refund_no='demo-out-refund-no')
76+
print('code: %s, message: %s' % (code, message))
77+
78+
# 申请交易账单
79+
def trade_bill():
80+
code, message = wxpay.trade_bill(bill_date='2021-04-01')
81+
print('code: %s, message: %s' % (code, message))
82+
83+
# 申请资金流水账单
84+
def fundflow_bill():
85+
code, message = wxpay.fundflow_bill(bill_date='2021-04-01')
86+
print('code: %s, message: %s' % (code, message))
87+
88+
# 下载账单
89+
def download_bill():
90+
code, message = wxpay.download_bill(
91+
url='https://api.mch.weixin.qq.com/v3/billdownload/file?token=demo-token')
92+
print('code: %s, message: %s' % (code, message))
93+
94+
# 合单支付下单
95+
def combine_pay():
96+
code, message = wxpay.combine_pay(combine_out_trade_no='demo_out_trade_no', sub_orders=[{'mchid':'1900000109', 'attach':'深圳分店', 'amount':{'total_amount':100,'currency':'CNY'}, 'out_trade_no':'20150806125346', 'description':'腾讯充值中心-QQ会员充值', 'settle_info':{'profit_sharing':False, 'subsidy_amount':10}}])
97+
print('code: %s, message: %s' % (code, message))
98+
99+
# 合单订单查询
100+
def combine_query():
101+
code, message = wxpay.combine_query(combine_out_trade_no='demo_out_trade_no')
102+
print('code: %s, message: %s' % (code, message))
103+
104+
# 合单订单关闭
105+
def combine_close():
106+
code, message = wxpay.combine_close(combine_out_trade_no='demo_out_trade_no', sub_orders=[
107+
{'mchid': '1900000109', 'out_trade_no': '20150806125346'}])
108+
print('code: %s, message: %s' % (code, message))
109+
```
42110

43111

examples.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ def download_bill():
6363
print('code: %s, message: %s' % (code, message))
6464

6565

66+
def combine_pay():
67+
code, message = wxpay.combine_pay(combine_out_trade_no='demo_out_trade_no', sub_orders=[{'mchid': '1900000109', 'attach': '深圳分店', 'amount': {
68+
'total_amount': 100, 'currency': 'CNY'}, 'out_trade_no': '20150806125346', 'description': '腾讯充值中心-QQ会员充值', 'settle_info': {'profit_sharing': False, 'subsidy_amount': 10}}])
69+
print('code: %s, message: %s' % (code, message))
70+
71+
72+
def combine_query():
73+
code, message = wxpay.combine_query(
74+
combine_out_trade_no='demo_out_trade_no')
75+
print('code: %s, message: %s' % (code, message))
76+
77+
78+
def combine_close():
79+
code, message = wxpay.combine_close(combine_out_trade_no='demo_out_trade_no', sub_orders=[
80+
{'mchid': '1900000109', 'out_trade_no': '20150806125346'}])
81+
print('code: %s, message: %s' % (code, message))
82+
83+
6684
if __name__ == '__main__':
6785
# certificate()
6886
# pay()
@@ -72,5 +90,8 @@ def download_bill():
7290
# query_refund()
7391
# trade_bill()
7492
# fundflow_bill()
75-
download_bill()
93+
# download_bill()
94+
# combine_pay()
95+
# combine_query()
96+
# combine_close()
7697
pass

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.43",
9+
version="0.44",
1010
author="minibear",
1111
description="微信支付接口V3版python库",
1212
long_description=long_description,

wechatpayv3/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from .api import (WeChatPay, WeChatPayException, WeChatPayType)
1+
# -*- coding: utf-8 -*-
2+
from .api import WeChatPay, WeChatPayException, WeChatPayType

wechatpayv3/api.py

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def pay(self,
5555
:param goods_tag: 订单优惠标记,示例值:'WXG'
5656
:param detail: 优惠功能,示例值:{'cost_price':608800, 'invoice_id':'微信123', 'goods_detail':[{'merchant_goods_id':'商品编码', 'wechatpay_goods_id':'1001', 'goods_name':'iPhoneX 256G', 'quantity':1, 'unit_price':828800}]}
5757
:param scene_info: 场景信息,示例值:{'payer_client_ip':'14.23.150.211', 'device_id':'013467007045764', 'store_info':{'id':'0001', 'name':'腾讯大厦分店', 'area_code':'440305', 'address':'广东省深圳市南山区科技中一道10000号'}}
58-
:param settle_info: 结算信息,示例值:{'profit_sharing':false}
58+
:param settle_info: 结算信息,示例值:{'profit_sharing':False}
5959
:param notify_url: 通知地址,示例值:'https://www.weixin.qq.com/wxpay/pay.php'
6060
"""
6161
params = {}
@@ -74,6 +74,8 @@ def pay(self,
7474
params.update({'amount': amount})
7575
else:
7676
raise WeChatPayException('amount is not assigned.')
77+
if payer:
78+
params.update({'payer': payer})
7779
if scene_info:
7880
params.update({'scene_info': scene_info})
7981
if time_expire:
@@ -87,9 +89,7 @@ def pay(self,
8789
if settle_info:
8890
params.update({'settle_info': settle_info})
8991
if self._type in [WeChatPayType.JSAPI, WeChatPayType.MINIPROG]:
90-
if payer:
91-
params.update({'payer': payer})
92-
else:
92+
if not payer:
9393
raise WeChatPayException('payer is not assigned')
9494
path = '/v3/pay/transactions/jsapi'
9595
elif self._type == WeChatPayType.APP:
@@ -100,7 +100,7 @@ def pay(self,
100100
path = '/v3/pay/transactions/h5'
101101
elif self._type == WeChatPayType.NATIVE:
102102
path = '/v3/pay/transactions/native'
103-
self._core.post(path, json=json.dumps(params))
103+
return self._core.post(path, json=json.dumps(params))
104104

105105
def close(self, out_trade_no):
106106
"""关闭订单
@@ -121,15 +121,12 @@ def query(self, transaction_id=None, out_trade_no=None):
121121
"""
122122
if not (transaction_id or out_trade_no):
123123
raise WeChatPayException('params is not assigned')
124-
params = {}
125-
params['mchid'] = self._mchid
126124
if transaction_id:
127125
path = '/v3/pay/transactions/id/%s' % transaction_id
128-
params['transaction_id'] = transaction_id
129126
else:
130127
path = '/v3/pay/transactions/out-trade-no/%s' % out_trade_no
131-
params['out_trade_no'] = out_trade_no
132-
return self._core.get(path, json=json.dumps(params))
128+
path = '%s?mchid=%s' %(path, self._mchid)
129+
return self._core.get(path)
133130

134131
def refund(self,
135132
out_refund_no,
@@ -209,9 +206,95 @@ def download_bill(self, url):
209206
return self._core.get(path)
210207

211208
def certificate(self):
209+
"""下载微信支付平台证书
210+
"""
212211
path = '/v3/certificates'
213212
return self._core.get(path)
214213

214+
def combine_pay(self,
215+
combine_out_trade_no,
216+
sub_orders,
217+
scene_info=None,
218+
combine_payer_info=None,
219+
time_start=None,
220+
time_expire=None,
221+
combine_appid=None,
222+
combine_mchid=None,
223+
notify_url=None):
224+
"""合单支付下单
225+
:param combine_out_trade_no: 合单商户订单号, 示例值:'P20150806125346'
226+
:param sub_orders: 子单信息,示例值:[{'mchid':'1900000109', 'attach':'深圳分店', 'amount':{'total_amount':100,'currency':'CNY'}, 'out_trade_no':'20150806125346', 'description':'腾讯充值中心-QQ会员充值', 'settle_info':{'profit_sharing':False, 'subsidy_amount':10}}]
227+
:param scene_info: 场景信息, 示例值:{'device_id':'POS1:123', 'payer_client_ip':'14.17.22.32'}
228+
:param combine_payer_info: 支付者, 示例值:{'openid':'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o'}
229+
:param time_start: 交易起始时间,示例值:'2019-12-31T15:59:59+08:00'
230+
:param time_expire: 交易结束时间, 示例值:'2019-12-31T15:59:59+08:00'
231+
:param combine_appid: 合单商户appid, 示例值:'wxd678efh567hg6787'
232+
:param combine_mchid: 合单发起方商户号,示例值:'1900000109'
233+
:param notify_url: 通知地址, 示例值:'https://yourapp.com/notify'
234+
"""
235+
params={}
236+
params['combine_appid'] = combine_appid or self._appid
237+
params['combine_mchid'] = combine_mchid or self._mchid
238+
params['notify_url'] = notify_url or self._notify_url
239+
if combine_out_trade_no:
240+
params.update({'combine_out_trade_no': combine_out_trade_no})
241+
else:
242+
raise WeChatPayException('combine_out_trade_no is not assigned.')
243+
if sub_orders:
244+
params.update({'sub_orders': sub_orders})
245+
else:
246+
raise WeChatPayException('sub_orders is not assigned.')
247+
if scene_info:
248+
params.update({'scene_info': scene_info})
249+
if combine_payer_info:
250+
params.update({'combine_payer_info': combine_payer_info})
251+
if time_start:
252+
params.update({'time_start': time_start})
253+
if time_expire:
254+
params.update({'time_expire': time_expire})
255+
if self._type in [WeChatPayType.JSAPI, WeChatPayType.MINIPROG]:
256+
if not combine_payer_info:
257+
raise WeChatPayException('combine_payer_info is not assigned')
258+
path = '/v3/combine-transactions/jsapi'
259+
elif self._type == WeChatPayType.APP:
260+
path = '/v3/combine-transactions/app'
261+
elif self._type == WeChatPayType.H5:
262+
if not scene_info:
263+
raise WeChatPayException('scene_info is not assigned.')
264+
path = '/v3/combine-transactions/h5'
265+
elif self._type == WeChatPayType.NATIVE:
266+
path = '/v3/combine-transactions/native'
267+
return self._core.post(path, json=json.dumps(params))
268+
269+
def combine_query(self, combine_out_trade_no):
270+
"""合单查询订单
271+
:param combine_out_trade_no: 合单商户订单号,示例值:P20150806125346
272+
"""
273+
params = {}
274+
if not combine_out_trade_no:
275+
raise WeChatPayException('param combine_out_trade_no is not assigned')
276+
else:
277+
params.update({'combine_out_trade_no':combine_out_trade_no})
278+
path = '/v3/combine-transactions/out-trade-no/%s' % combine_out_trade_no
279+
return self._core.get(path)
280+
281+
def combine_close(self, combine_out_trade_no, sub_orders, combine_appid=None):
282+
"""合单关闭订单
283+
:param combine_out_trade_no: 合单商户订单号,示例值:'P20150806125346'
284+
:param sub_orders: 子单信息, 示例值:[{'mchid': '1900000109', 'out_trade_no': '20150806125346'}]
285+
:param combine_appid: 合单商户appid, 示例值:'wxd678efh567hg6787'
286+
"""
287+
params = {}
288+
params['combine_appid'] = combine_appid or self._appid
289+
290+
if not combine_out_trade_no:
291+
raise WeChatPayException('combine_out_trade_no is not assigned.')
292+
if not sub_orders:
293+
raise WeChatPayException('sub_orders is not assigned.')
294+
else:
295+
params.update({'sub_orders': sub_orders})
296+
path = '/v3/combine-transactions/out-trade-no/%s/close' % combine_out_trade_no
297+
return self._core.post(path, json=json.dumps(params))
215298

216299
class WeChatPayException(Exception):
217300
def __init__(self, reason):

wechatpayv3/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, mchid, mch_key_serial_no, mch_private_key, wechat_public_key)
1111
self._wechat_public_key = wechat_public_key
1212
self._gate_way = 'https://api.mch.weixin.qq.com'
1313

14-
def get(self, path, **kwargs):
14+
def get(self, path):
1515
headers = {}
1616
headers.update({'Content-Type': 'application/json'})
1717
headers.update({'Accept': 'application/json'})
@@ -21,7 +21,7 @@ def get(self, path, **kwargs):
2121
response = requests.get(url=self._gate_way + path, headers=headers)
2222
return response.status_code, response.text
2323

24-
def post(self, path, json=None, **kwargs):
24+
def post(self, path, json=None):
2525
headers = {}
2626
headers.update({'Content-Type': 'application/json'})
2727
headers.update({'Accept': 'application/json'})

0 commit comments

Comments
 (0)