1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* LuckMoney.php |
4
|
|
|
* |
5
|
|
|
* Part of Overtrue\Wechat. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @author bontian <[email protected]> |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Overtrue\Wechat; |
15
|
|
|
|
16
|
|
|
use Overtrue\Wechat\Exception; |
17
|
|
|
use Overtrue\Wechat\Payment\Business; |
18
|
|
|
use Overtrue\Wechat\Utils\XML; |
19
|
|
|
use Overtrue\Wechat\Utils\SignGenerator; |
20
|
|
|
use Overtrue\Wechat\Http; |
21
|
|
|
|
22
|
|
|
class LuckMoney |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* 发送现金红包 |
26
|
|
|
*/ |
27
|
|
|
const API_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* 发送裂变红包 |
31
|
|
|
*/ |
32
|
|
|
const API_GROUP_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* 红包查询 |
36
|
|
|
*/ |
37
|
|
|
const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 红包预下单接口 |
41
|
|
|
*/ |
42
|
|
|
const API_PREORDER = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder'; |
43
|
|
|
|
44
|
|
|
const TYPE_CASH_LUCK_MONEY = 'NORMAL'; //红包类型,现金红包 |
45
|
|
|
const TYPE_GROUP_LUCK_MONEY = 'GROUP'; //红包类型,裂变红包 |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* 商户信息 |
51
|
|
|
* |
52
|
|
|
* @var Business |
53
|
|
|
*/ |
54
|
|
|
protected $business; |
55
|
|
|
|
56
|
|
|
public function __construct(Business $business) |
57
|
|
|
{ |
58
|
|
|
if (!is_null($business)) { |
59
|
|
|
$this->setBusiness($business); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* 设置商户 |
65
|
|
|
* |
66
|
|
|
* @param Business $business |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function setBusiness(Business $business) |
71
|
|
|
{ |
72
|
|
|
if (!is_null($business)) { |
73
|
|
|
$this->business = $business; |
74
|
|
|
} |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* 红包预下单,主要用于摇一摇红包活动 |
80
|
|
|
* |
81
|
|
|
* <pre> |
82
|
|
|
* $data: |
83
|
|
|
* { |
84
|
|
|
* "mch_billno": "198374613512", |
85
|
|
|
* "send_name":"某某公司", |
86
|
|
|
* "hb_type":"某某公司", |
87
|
|
|
* "total_amount": 1000, |
88
|
|
|
* "total_num": 1, |
89
|
|
|
* "wishing": "祝福语", |
90
|
|
|
* "act_name": "活动名称", |
91
|
|
|
* "remark": "红包备注", |
92
|
|
|
* "risk_cntl": "NORMAL", |
93
|
|
|
* } |
94
|
|
|
* </pre> |
95
|
|
|
* |
96
|
|
|
* @param array $data |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function preOrder(array $data) |
101
|
|
|
{ |
102
|
|
|
$defaultParam['nonce_str'] = uniqid('pre_'); |
|
|
|
|
103
|
|
|
$defaultParam['mch_id'] = $this->business->mch_id; |
|
|
|
|
104
|
|
|
$defaultParam['wxappid'] = $this->business->appid; |
|
|
|
|
105
|
|
|
|
106
|
|
|
//用于发红包时微信支付识别摇周边红包,所有开发者统一填写摇周边平台的商户号:1000052601 |
107
|
|
|
$defaultParam['auth_mchid'] = '1000052601'; |
108
|
|
|
//用于发红包时微信支付识别摇周边红包,所有开发者统一填写摇周边平台的appid:wxbf42bd79c4391863 |
109
|
|
|
$defaultParam['auth_appid'] = 'wxbf42bd79c4391863'; |
110
|
|
|
|
111
|
|
|
$defaultParam['amt_type'] = 'ALL_RAND'; |
112
|
|
|
|
113
|
|
|
$param = array_merge($data, $defaultParam); |
114
|
|
|
$signGenerator = new SignGenerator($param); |
115
|
|
|
$me = $this; |
116
|
|
|
$signGenerator->onSortAfter(function (SignGenerator $that) use ($me) { |
117
|
|
|
$that->key = $me->business->mch_key; |
|
|
|
|
118
|
|
|
}); |
119
|
|
|
|
120
|
|
|
$sign = $signGenerator->getResult(); |
121
|
|
|
$param['sign'] = $sign; |
122
|
|
|
|
123
|
|
|
$request = XML::build($param); |
124
|
|
|
|
125
|
|
|
//设置Http使用的证书 |
126
|
|
|
$options['sslcert_path'] = $this->business->getClientCert(); |
|
|
|
|
127
|
|
|
$options['sslkey_path'] = $this->business->getClientKey(); |
128
|
|
|
|
129
|
|
|
$http = new Http(); |
130
|
|
|
|
131
|
|
|
$response = $http->request(self::API_PREORDER, Http::POST, $request, $options); |
|
|
|
|
132
|
|
|
|
133
|
|
|
if (empty($response)) { |
134
|
|
|
throw new Exception('Create PreOrder failed.'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$result = XML::parse($response); |
|
|
|
|
138
|
|
|
|
139
|
|
|
return $result; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* 发送红包 |
144
|
|
|
* |
145
|
|
|
* <pre> |
146
|
|
|
* $data: |
147
|
|
|
* { |
148
|
|
|
* "mch_billno": "198374613512", |
149
|
|
|
* "send_name":"某某公司", |
150
|
|
|
* "re_openid": "oJCvDjjQKx5LMtM_1kjK0gGQLsew", |
151
|
|
|
* "total_amount": 1000, |
152
|
|
|
* "wishing": "祝福语", |
153
|
|
|
* "act_name": "活动名称", |
154
|
|
|
* "total_num": 1, |
155
|
|
|
* "remark": "红包备注" |
156
|
|
|
* } |
157
|
|
|
* </pre> |
158
|
|
|
* |
159
|
|
|
* @param array $data |
160
|
|
|
* @param int $type |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function send(array $data, $type = self::TYPE_CASH_LUCK_MONEY) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
$defaultParam['nonce_str'] = uniqid('pre_'); |
|
|
|
|
167
|
|
|
$defaultParam['mch_id'] = $this->business->mch_id; |
|
|
|
|
168
|
|
|
$defaultParam['wxappid'] = $this->business->appid; |
|
|
|
|
169
|
|
|
|
170
|
|
|
if ($type == self::TYPE_CASH_LUCK_MONEY) { |
171
|
|
|
$defaultParam['client_ip'] = $_SERVER['REMOTE_ADDR']; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($type == self::TYPE_GROUP_LUCK_MONEY) { |
175
|
|
|
$defaultParam['amt_type'] = 'ALL_RAND'; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$param = array_merge($data, $defaultParam); |
179
|
|
|
$signGenerator = new SignGenerator($param); |
180
|
|
|
$me = $this; |
181
|
|
|
$signGenerator->onSortAfter(function (SignGenerator $that) use ($me) { |
182
|
|
|
$that->key = $me->business->mch_key; |
|
|
|
|
183
|
|
|
}); |
184
|
|
|
|
185
|
|
|
$sign = $signGenerator->getResult(); |
186
|
|
|
$param['sign'] = $sign; |
187
|
|
|
|
188
|
|
|
$request = XML::build($param); |
189
|
|
|
|
190
|
|
|
//设置Http使用的证书 |
191
|
|
|
$options['sslcert_path'] = $this->business->getClientCert(); |
|
|
|
|
192
|
|
|
$options['sslkey_path'] = $this->business->getClientKey(); |
193
|
|
|
|
194
|
|
|
$http = new Http(); |
195
|
|
|
|
196
|
|
|
//根据红包类型决定调用的API |
197
|
|
|
if ($type == self::TYPE_CASH_LUCK_MONEY) { |
198
|
|
|
$url = self::API_SEND; |
199
|
|
|
} else { |
200
|
|
|
$url = self::API_GROUP_SEND; |
201
|
|
|
} |
202
|
|
|
$response = $http->request($url, Http::POST, $request, $options); |
|
|
|
|
203
|
|
|
|
204
|
|
|
if (empty($response)) { |
205
|
|
|
throw new Exception('Send LuckMoney failed.'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$result = XML::parse($response); |
|
|
|
|
209
|
|
|
|
210
|
|
|
return $result; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* 发送普通红包 |
215
|
|
|
* |
216
|
|
|
* <pre> |
217
|
|
|
* $data: |
218
|
|
|
* { |
219
|
|
|
* "mch_billno": "198374613512", |
220
|
|
|
* "send_name":"某某公司", |
221
|
|
|
* "re_openid": "oJCvDjjQKx5LMtM_1kjK0gGQLsew", |
222
|
|
|
* "total_amount": 1000, |
223
|
|
|
* "wishing": "祝福语", |
224
|
|
|
* "act_name": "活动名称", |
225
|
|
|
* "total_num": 1, |
226
|
|
|
* "remark": "红包备注" |
227
|
|
|
* } |
228
|
|
|
* </pre> |
229
|
|
|
* |
230
|
|
|
* @param array $data |
231
|
|
|
* |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
|
|
public function sendNormal(array $data) |
235
|
|
|
{ |
236
|
|
|
return $this->send($data, self::TYPE_CASH_LUCK_MONEY); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* 发送裂变红包 |
241
|
|
|
* |
242
|
|
|
* <pre> |
243
|
|
|
* $data: |
244
|
|
|
* { |
245
|
|
|
* "mch_billno": "198374613512", |
246
|
|
|
* "send_name":"某某公司", |
247
|
|
|
* "re_openid": "oJCvDjjQKx5LMtM_1kjK0gGQLsew", |
248
|
|
|
* "total_amount": 1000, |
249
|
|
|
* "wishing": "祝福语", |
250
|
|
|
* "act_name": "活动名称", |
251
|
|
|
* "total_num": 1, |
252
|
|
|
* "remark": "红包备注" |
253
|
|
|
* } |
254
|
|
|
* </pre> |
255
|
|
|
* |
256
|
|
|
* @param array $data |
257
|
|
|
* |
258
|
|
|
* @return array |
259
|
|
|
*/ |
260
|
|
|
public function sendGroup(array $data) |
261
|
|
|
{ |
262
|
|
|
return $this->send($data, self::TYPE_GROUP_LUCK_MONEY); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* 查询红包信息 |
267
|
|
|
* |
268
|
|
|
* @param string $mchBillNumber |
269
|
|
|
* |
270
|
|
|
* @return array |
271
|
|
|
*/ |
272
|
|
|
public function query($mchBillNumber) |
273
|
|
|
{ |
274
|
|
|
if (empty($mchBillNumber)) { |
275
|
|
|
throw new Exception('mch_id is required'); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$param['mch_billno'] = $mchBillNumber; |
|
|
|
|
279
|
|
|
$param['nonce_str'] = uniqid('pre_'); |
280
|
|
|
$param['mch_id'] = $this->business->mch_id; |
|
|
|
|
281
|
|
|
$param['appid'] = $this->business->appid; |
|
|
|
|
282
|
|
|
$param['bill_type'] = 'MCHT'; |
283
|
|
|
|
284
|
|
|
$signGenerator = new SignGenerator($param); |
285
|
|
|
$me = $this; |
286
|
|
|
$signGenerator->onSortAfter(function (SignGenerator $that) use ($me) { |
287
|
|
|
$that->key = $me->business->mch_key; |
|
|
|
|
288
|
|
|
}); |
289
|
|
|
|
290
|
|
|
$sign = $signGenerator->getResult(); |
291
|
|
|
$param['sign'] = $sign; |
292
|
|
|
|
293
|
|
|
$request = XML::build($param); |
294
|
|
|
|
295
|
|
|
//设置Http使用的证书 |
296
|
|
|
$options['sslcert_path'] = $this->business->getClientCert(); |
|
|
|
|
297
|
|
|
$options['sslkey_path'] = $this->business->getClientKey(); |
298
|
|
|
|
299
|
|
|
$http = new Http(); |
300
|
|
|
$response = $http->request(static::API_QUERY, Http::POST, $request, $options); |
|
|
|
|
301
|
|
|
|
302
|
|
|
if (empty($response)) { |
303
|
|
|
throw new Exception('Get LuckMoneyInfo failed.'); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
$result = XML::parse($response); |
|
|
|
|
307
|
|
|
|
308
|
|
|
return $result; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.