1 | # -*- coding: utf-8 -*- |
||
2 | 10 | from __future__ import absolute_import, unicode_literals |
|
3 | 10 | import random |
|
4 | 10 | from datetime import datetime |
|
5 | |||
6 | 10 | from wechatpy.pay.utils import get_external_ip |
|
7 | 10 | from wechatpy.pay.base import BaseWeChatPayAPI |
|
8 | |||
9 | |||
10 | 10 | class WeChatRedpack(BaseWeChatPayAPI): |
|
11 | |||
12 | 10 | View Code Duplication | def send(self, user_id, total_amount, send_name, act_name, |
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
13 | wishing, remark, total_num=1, client_ip=None, |
||
14 | out_trade_no=None, scene_id=None, consume_mch_id=None): |
||
15 | """ |
||
16 | 发送现金红包 |
||
17 | |||
18 | :param user_id: 接收红包的用户在公众号下的 openid |
||
19 | :param total_amount: 红包金额,单位分 |
||
20 | :param send_name: 商户名称 |
||
21 | :param act_name: 活动名称 |
||
22 | :param wishing: 红包祝福语 |
||
23 | :param remark: 备注 |
||
24 | :param client_ip: 可选,调用接口的机器 IP 地址 |
||
25 | :param total_num: 可选,红包发放总人数,默认为 1 |
||
26 | :param out_trade_no: 可选,商户订单号,默认会自动生成 |
||
27 | :param scene_id: 可选,发放红包使用场景,红包金额大于200时必传 |
||
28 | :param consume_mch_id: 可选,资金授权商户号。服务商替特约商户发放时使用 |
||
29 | :return: 返回的结果数据字典 |
||
30 | """ |
||
31 | if not out_trade_no: |
||
32 | now = datetime.now() |
||
33 | out_trade_no = '{0}{1}{2}'.format( |
||
34 | self.mch_id, |
||
35 | now.strftime('%Y%m%d%H%M%S'), |
||
36 | random.randint(1000, 10000) |
||
37 | ) |
||
38 | data = { |
||
39 | 'wxappid': self.appid, |
||
40 | 're_openid': user_id, |
||
41 | 'total_amount': total_amount, |
||
42 | 'send_name': send_name, |
||
43 | 'act_name': act_name, |
||
44 | 'wishing': wishing, |
||
45 | 'remark': remark, |
||
46 | 'client_ip': client_ip or get_external_ip(), |
||
47 | 'total_num': total_num, |
||
48 | 'mch_billno': out_trade_no, |
||
49 | 'scene_id': scene_id, |
||
50 | 'risk_info': None, |
||
51 | 'consume_mch_id': consume_mch_id |
||
52 | } |
||
53 | return self._post('mmpaymkttransfers/sendredpack', data=data) |
||
54 | |||
55 | 10 | View Code Duplication | def send_group(self, user_id, total_amount, send_name, act_name, wishing, |
0 ignored issues
–
show
|
|||
56 | remark, total_num, client_ip=None, amt_type="ALL_RAND", |
||
57 | out_trade_no=None, scene_id=None, consume_mch_id=None): |
||
58 | """ |
||
59 | 发送裂变红包 |
||
60 | |||
61 | :param user_id: 接收红包的用户在公众号下的 openid |
||
62 | :param total_amount: 红包金额,单位分 |
||
63 | :param send_name: 商户名称 |
||
64 | :param act_name: 活动名称 |
||
65 | :param wishing: 红包祝福语 |
||
66 | :param remark: 备注 |
||
67 | :param total_num: 红包发放总人数 |
||
68 | :param client_ip: 可选,调用接口的机器 IP 地址 |
||
69 | :param amt_type: 可选,红包金额设置方式 |
||
70 | ALL_RAND—全部随机,商户指定总金额和红包发放总人数,由微信支付随机计算出各红包金额 |
||
71 | :param out_trade_no: 可选,商户订单号,默认会自动生成 |
||
72 | :param scene_id: 可选,发放红包使用场景,红包金额大于200时必传 |
||
73 | :param consume_mch_id: 可选,资金授权商户号。服务商替特约商户发放时使用 |
||
74 | :return: 返回的结果数据字典 |
||
75 | """ |
||
76 | if not out_trade_no: |
||
77 | now = datetime.now() |
||
78 | out_trade_no = '{0}{1}{2}'.format( |
||
79 | self._client.mch_id, |
||
80 | now.strftime('%Y%m%d%H%M%S'), |
||
81 | random.randint(1000, 10000) |
||
82 | ) |
||
83 | data = { |
||
84 | 'wxappid': self.appid, |
||
85 | 're_openid': user_id, |
||
86 | 'total_amount': total_amount, |
||
87 | 'send_name': send_name, |
||
88 | 'act_name': act_name, |
||
89 | 'wishing': wishing, |
||
90 | 'remark': remark, |
||
91 | 'total_num': total_num, |
||
92 | 'client_ip': client_ip or get_external_ip(), |
||
93 | 'amt_type': amt_type, |
||
94 | 'mch_billno': out_trade_no, |
||
95 | 'scene_id': scene_id, |
||
96 | 'risk_info': None, |
||
97 | 'consume_mch_id': consume_mch_id |
||
98 | } |
||
99 | return self._post('mmpaymkttransfers/sendgroupredpack', data=data) |
||
100 | |||
101 | 10 | def query(self, out_trade_no, bill_type='MCHT'): |
|
102 | """ |
||
103 | 查询红包发放记录 |
||
104 | |||
105 | :param out_trade_no: 商户订单号 |
||
106 | :param bill_type: 可选,订单类型,目前固定为 MCHT |
||
107 | :return: 返回的红包发放记录信息 |
||
108 | """ |
||
109 | data = { |
||
110 | 'mch_billno': out_trade_no, |
||
111 | 'bill_type': bill_type, |
||
112 | 'appid': self.appid, |
||
113 | } |
||
114 | return self._post('mmpaymkttransfers/gethbinfo', data=data) |
||
115 |