1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/wechat. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace EasyWeChat\OfficialAccount\Card; |
13
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\BaseClient; |
15
|
|
|
use EasyWeChat\Kernel\Traits\InteractsWithCache; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Client. |
19
|
|
|
* |
20
|
|
|
* @author overtrue <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Client extends BaseClient |
23
|
|
|
{ |
24
|
|
|
use InteractsWithCache; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $url; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Ticket cache key. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $ticketCacheKey; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Ticket cache prefix. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $ticketCachePrefix = 'easywechat.official_account.card.api_ticket.'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* 获取卡券颜色. |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
* |
50
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
51
|
|
|
*/ |
52
|
1 |
|
public function colors() |
53
|
|
|
{ |
54
|
1 |
|
return $this->httpGet('card/getcolors'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* 卡券开放类目查询接口. |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
* |
62
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
63
|
|
|
*/ |
64
|
1 |
|
public function categories() |
65
|
|
|
{ |
66
|
1 |
|
return $this->httpGet('card/getapplyprotocol'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* 创建卡券. |
71
|
|
|
* |
72
|
|
|
* @param string $cardType |
73
|
|
|
* @param array $attributes |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
* |
77
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
78
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
79
|
|
|
*/ |
80
|
1 |
|
public function create($cardType = 'member_card', array $attributes) |
81
|
|
|
{ |
82
|
|
|
$params = [ |
83
|
|
|
'card' => [ |
84
|
1 |
|
'card_type' => strtoupper($cardType), |
85
|
1 |
|
strtolower($cardType) => $attributes, |
86
|
|
|
], |
87
|
|
|
]; |
88
|
|
|
|
89
|
1 |
|
return $this->httpPostJson('card/create', $params); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* 查看卡券详情. |
94
|
|
|
* |
95
|
|
|
* @param string $cardId |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
* |
99
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
100
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
101
|
|
|
*/ |
102
|
1 |
|
public function get($cardId) |
103
|
|
|
{ |
104
|
|
|
$params = [ |
105
|
1 |
|
'card_id' => $cardId, |
106
|
|
|
]; |
107
|
|
|
|
108
|
1 |
|
return $this->httpPostJson('card/get', $params); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* 批量查询卡列表. |
113
|
|
|
* |
114
|
|
|
* @param int $offset |
115
|
|
|
* @param int $count |
116
|
|
|
* @param string $statusList |
117
|
|
|
* |
118
|
|
|
* @return mixed |
119
|
|
|
* |
120
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
121
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
122
|
|
|
*/ |
123
|
1 |
|
public function list($offset = 0, $count = 10, $statusList = 'CARD_STATUS_VERIFY_OK') |
124
|
|
|
{ |
125
|
|
|
$params = [ |
126
|
1 |
|
'offset' => $offset, |
127
|
1 |
|
'count' => $count, |
128
|
1 |
|
'status_list' => $statusList, |
129
|
|
|
]; |
130
|
|
|
|
131
|
1 |
|
return $this->httpPostJson('card/batchget', $params); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* 更改卡券信息接口 and 设置跟随推荐接口. |
136
|
|
|
* |
137
|
|
|
* @param string $cardId |
138
|
|
|
* @param string $type |
139
|
|
|
* @param array $attributes |
140
|
|
|
* |
141
|
|
|
* @return mixed |
142
|
|
|
* |
143
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
144
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
145
|
|
|
*/ |
146
|
1 |
|
public function update($cardId, $type, array $attributes = []) |
147
|
|
|
{ |
148
|
1 |
|
$card = []; |
149
|
1 |
|
$card['card_id'] = $cardId; |
150
|
1 |
|
$card[strtolower($type)] = $attributes; |
151
|
|
|
|
152
|
1 |
|
return $this->httpPostJson('card/update', $card); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* 删除卡券接口. |
157
|
|
|
* |
158
|
|
|
* @param string $cardId |
159
|
|
|
* |
160
|
|
|
* @return mixed |
161
|
|
|
* |
162
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
163
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
164
|
|
|
*/ |
165
|
1 |
|
public function delete($cardId) |
166
|
|
|
{ |
167
|
|
|
$params = [ |
168
|
1 |
|
'card_id' => $cardId, |
169
|
|
|
]; |
170
|
|
|
|
171
|
1 |
|
return $this->httpPostJson('card/delete', $params); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* 创建二维码. |
176
|
|
|
* |
177
|
|
|
* @param array $cards |
178
|
|
|
* |
179
|
|
|
* @return mixed |
180
|
|
|
* |
181
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
182
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
183
|
|
|
*/ |
184
|
1 |
|
public function createQrCode(array $cards) |
185
|
|
|
{ |
186
|
1 |
|
return $this->httpPostJson('card/qrcode/create', $cards); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* ticket 换取二维码图片. |
191
|
|
|
* |
192
|
|
|
* @param string $ticket |
193
|
|
|
* |
194
|
|
|
* @return array |
195
|
|
|
* |
196
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
197
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
198
|
|
|
*/ |
199
|
1 |
|
public function getQrCode($ticket) |
200
|
|
|
{ |
201
|
1 |
|
$baseUri = 'https://mp.weixin.qq.com/cgi-bin/showqrcode'; |
202
|
|
|
$params = [ |
203
|
1 |
|
'ticket' => $ticket, |
204
|
|
|
]; |
205
|
|
|
|
206
|
1 |
|
$response = $this->requestRaw($baseUri, 'GET', $params); |
207
|
|
|
|
208
|
|
|
return [ |
209
|
1 |
|
'status' => $response->getStatusCode(), |
210
|
1 |
|
'reason' => $response->getReasonPhrase(), |
211
|
1 |
|
'headers' => $response->getHeaders(), |
212
|
1 |
|
'body' => strval($response->getBody()), |
213
|
1 |
|
'url' => $baseUri.'?'.http_build_query($params), |
214
|
|
|
]; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* 通过ticket换取二维码 链接. |
219
|
|
|
* |
220
|
|
|
* @param string $ticket |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
1 |
|
public function getQrCodeUrl($ticket) |
225
|
|
|
{ |
226
|
1 |
|
return sprintf('https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s', $ticket); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* 创建货架接口. |
231
|
|
|
* |
232
|
|
|
* @param string $banner |
233
|
|
|
* @param string $pageTitle |
234
|
|
|
* @param bool $canShare |
235
|
|
|
* @param string $scene [SCENE_NEAR_BY 附近,SCENE_MENU 自定义菜单,SCENE_QRCODE 二维码,SCENE_ARTICLE 公众号文章, |
236
|
|
|
* SCENE_H5 h5页面,SCENE_IVR 自动回复,SCENE_CARD_CUSTOM_CELL 卡券自定义cell] |
237
|
|
|
* @param array $cardList |
238
|
|
|
* |
239
|
|
|
* @return mixed |
240
|
|
|
* |
241
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
242
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
243
|
|
|
*/ |
244
|
1 |
|
public function createLandingPage($banner, $pageTitle, $canShare, $scene, $cardList) |
245
|
|
|
{ |
246
|
|
|
$params = [ |
247
|
1 |
|
'banner' => $banner, |
248
|
1 |
|
'page_title' => $pageTitle, |
249
|
1 |
|
'can_share' => $canShare, |
250
|
1 |
|
'scene' => $scene, |
251
|
1 |
|
'card_list' => $cardList, |
252
|
|
|
]; |
253
|
|
|
|
254
|
1 |
|
return $this->httpPostJson('card/landingpage/create', $params); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* 图文消息群发卡券. |
259
|
|
|
* |
260
|
|
|
* @param string $cardId |
261
|
|
|
* |
262
|
|
|
* @return mixed |
263
|
|
|
* |
264
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
265
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
266
|
|
|
*/ |
267
|
1 |
|
public function getHtml($cardId) |
268
|
|
|
{ |
269
|
|
|
$params = [ |
270
|
1 |
|
'card_id' => $cardId, |
271
|
|
|
]; |
272
|
|
|
|
273
|
1 |
|
return $this->httpPostJson('card/mpnews/gethtml', $params); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* 设置测试白名单. |
278
|
|
|
* |
279
|
|
|
* @param array $openids |
280
|
|
|
* |
281
|
|
|
* @return mixed |
282
|
|
|
* |
283
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
284
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
285
|
|
|
*/ |
286
|
1 |
|
public function setTestWhitelist($openids) |
287
|
|
|
{ |
288
|
|
|
$params = [ |
289
|
1 |
|
'openid' => $openids, |
290
|
|
|
]; |
291
|
|
|
|
292
|
1 |
|
return $this->httpPostJson('card/testwhitelist/set', $params); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* 设置测试白名单(by username). |
297
|
|
|
* |
298
|
|
|
* @param array $usernames |
299
|
|
|
* |
300
|
|
|
* @return mixed |
301
|
|
|
* |
302
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
303
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
304
|
|
|
*/ |
305
|
1 |
|
public function setTestWhitelistByName(array $usernames) |
306
|
|
|
{ |
307
|
|
|
$params = [ |
308
|
1 |
|
'username' => $usernames, |
309
|
|
|
]; |
310
|
|
|
|
311
|
1 |
|
return $this->httpPostJson('card/testwhitelist/set', $params); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* 获取用户已领取卡券接口. |
316
|
|
|
* |
317
|
|
|
* @param string $openid |
318
|
|
|
* @param string $cardId |
319
|
|
|
* |
320
|
|
|
* @return mixed |
321
|
|
|
* |
322
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
323
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
324
|
|
|
*/ |
325
|
1 |
|
public function getUserCards($openid, $cardId = '') |
326
|
|
|
{ |
327
|
|
|
$params = [ |
328
|
1 |
|
'openid' => $openid, |
329
|
1 |
|
'card_id' => $cardId, |
330
|
|
|
]; |
331
|
|
|
|
332
|
1 |
|
return $this->httpPostJson('card/user/getcardlist', $params); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* 设置微信买单接口. |
337
|
|
|
* 设置买单的 card_id 必须已经配置了门店,否则会报错. |
338
|
|
|
* |
339
|
|
|
* @param string $cardId |
340
|
|
|
* @param bool $isOpen |
341
|
|
|
* |
342
|
|
|
* @return mixed |
343
|
|
|
* |
344
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
345
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
346
|
|
|
*/ |
347
|
1 |
|
public function setPayCell($cardId, $isOpen = true) |
348
|
|
|
{ |
349
|
|
|
$params = [ |
350
|
1 |
|
'card_id' => $cardId, |
351
|
1 |
|
'is_open' => $isOpen, |
352
|
|
|
]; |
353
|
|
|
|
354
|
1 |
|
return $this->httpPostJson('card/paycell/set', $params); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* 设置自助核销接口 |
359
|
|
|
* 设置买单的 card_id 必须已经配置了门店,否则会报错. |
360
|
|
|
* |
361
|
|
|
* @param string $cardId |
362
|
|
|
* @param bool $isOpen |
363
|
|
|
* @param bool $verifyCod |
364
|
|
|
* @param bool $remarkAmount |
365
|
|
|
* |
366
|
|
|
* @return mixed |
367
|
|
|
*/ |
368
|
1 |
|
public function setPayConsumeCell($cardId, $isOpen = true, $verifyCod = false, $remarkAmount = false) |
369
|
|
|
{ |
370
|
|
|
$params = [ |
371
|
1 |
|
'card_id' => $cardId, |
372
|
1 |
|
'is_open' => $isOpen, |
373
|
1 |
|
'need_verify_cod' => $verifyCod, |
374
|
1 |
|
'need_remark_amount' => $remarkAmount, |
375
|
|
|
]; |
376
|
|
|
|
377
|
1 |
|
return $this->httpPostJson('card/selfconsumecell/set', $params); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* 增加库存. |
382
|
|
|
* |
383
|
|
|
* @param string $cardId |
384
|
|
|
* @param int $amount |
385
|
|
|
* |
386
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
387
|
|
|
*/ |
388
|
1 |
|
public function increaseStock($cardId, $amount) |
389
|
|
|
{ |
390
|
1 |
|
return $this->updateStock($cardId, $amount, 'increase'); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* 减少库存. |
395
|
|
|
* |
396
|
|
|
* @param string $cardId |
397
|
|
|
* @param int $amount |
398
|
|
|
* |
399
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
400
|
|
|
*/ |
401
|
1 |
|
public function reduceStock($cardId, $amount) |
402
|
|
|
{ |
403
|
1 |
|
return $this->updateStock($cardId, $amount, 'reduce'); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* 修改库存接口. |
408
|
|
|
* |
409
|
|
|
* @param string $cardId |
410
|
|
|
* @param int $amount |
411
|
|
|
* @param string $action |
412
|
|
|
* |
413
|
|
|
* @return mixed |
414
|
|
|
* |
415
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
416
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
417
|
|
|
*/ |
418
|
1 |
|
protected function updateStock($cardId, $amount, $action = 'increase') |
419
|
|
|
{ |
420
|
1 |
|
$key = 'increase' === $action ? 'increase_stock_value' : 'reduce_stock_value'; |
421
|
|
|
$params = [ |
422
|
1 |
|
'card_id' => $cardId, |
423
|
1 |
|
$key => abs($amount), |
424
|
|
|
]; |
425
|
|
|
|
426
|
1 |
|
return $this->httpPostJson('card/modifystock', $params); |
427
|
|
|
} |
428
|
|
|
} |
429
|
|
|
|