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
|
|
|
/** |
13
|
|
|
* Card.php. |
14
|
|
|
* |
15
|
|
|
* @author overtrue <[email protected]> |
16
|
|
|
* @copyright 2016 overtrue <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @see https://github.com/overtrue |
19
|
|
|
* @see http://overtrue.me |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace EasyWeChat\Card; |
23
|
|
|
|
24
|
|
|
use Doctrine\Common\Cache\Cache; |
25
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
26
|
|
|
use EasyWeChat\Core\AbstractAPI; |
27
|
|
|
use EasyWeChat\Support\Arr; |
28
|
|
|
use Psr\Http\Message\ResponseInterface; |
29
|
|
|
|
30
|
|
|
class Card extends AbstractAPI |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Cache. |
34
|
|
|
* |
35
|
|
|
* @var Cache |
36
|
|
|
*/ |
37
|
|
|
protected $cache; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Ticket cache key. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $ticketCacheKey; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Ticket cache prefix. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $ticketCachePrefix = 'overtrue.wechat.card_api_ticket.'; |
52
|
|
|
|
53
|
|
|
const API_GET_COLORS = 'https://api.weixin.qq.com/card/getcolors'; |
54
|
|
|
const API_CREATE_CARD = 'https://api.weixin.qq.com/card/create'; |
55
|
|
|
const API_CREATE_QRCODE = 'https://api.weixin.qq.com/card/qrcode/create'; |
56
|
|
|
const API_SHOW_QRCODE = 'https://mp.weixin.qq.com/cgi-bin/showqrcode'; |
57
|
|
|
const API_GET_CARD_TICKET = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; |
58
|
|
|
const API_CREATE_LANDING_PAGE = 'https://api.weixin.qq.com/card/landingpage/create'; |
59
|
|
|
const API_DEPOSIT_CODE = 'https://api.weixin.qq.com/card/code/deposit'; |
60
|
|
|
const API_GET_DEPOSIT_COUNT = 'https://api.weixin.qq.com/card/code/getdepositcount'; |
61
|
|
|
const API_CHECK_CODE = 'https://api.weixin.qq.com/card/code/checkcode'; |
62
|
|
|
const API_GET_HTML = 'https://api.weixin.qq.com/card/mpnews/gethtml'; |
63
|
|
|
const API_SET_TEST_WHITE_LIST = 'https://api.weixin.qq.com/card/testwhitelist/set'; |
64
|
|
|
const API_GET_CODE = 'https://api.weixin.qq.com/card/code/get'; |
65
|
|
|
const API_CONSUME_CARD = 'https://api.weixin.qq.com/card/code/consume'; |
66
|
|
|
const API_DECRYPT_CODE = 'https://api.weixin.qq.com/card/code/decrypt'; |
67
|
|
|
const API_GET_CARD_LIST = 'https://api.weixin.qq.com/card/user/getcardlist'; |
68
|
|
|
const API_GET_CARD = 'https://api.weixin.qq.com/card/get'; |
69
|
|
|
const API_LIST_CARD = 'https://api.weixin.qq.com/card/batchget'; |
70
|
|
|
const API_UPDATE_CARD = 'https://api.weixin.qq.com/card/update'; |
71
|
|
|
const API_SET_PAY_CELL = 'https://api.weixin.qq.com/card/paycell/set'; |
72
|
|
|
const API_MODIFY_STOCK = 'https://api.weixin.qq.com/card/modifystock'; |
73
|
|
|
const API_UPDATE_CODE = 'https://api.weixin.qq.com/card/code/update'; |
74
|
|
|
const API_DELETE_CARD = 'https://api.weixin.qq.com/card/delete'; |
75
|
|
|
const API_DISABLE_CARD = 'https://api.weixin.qq.com/card/code/unavailable'; |
76
|
|
|
const API_ACTIVATE_MEMBER_CARD = 'https://api.weixin.qq.com/card/membercard/activate'; |
77
|
|
|
const API_ACTIVATE_MEMBER_USER_FORM = 'https://api.weixin.qq.com/card/membercard/activateuserform/set'; |
78
|
|
|
const API_GET_MEMBER_USER_INFO = 'https://api.weixin.qq.com/card/membercard/userinfo/get'; |
79
|
|
|
const API_UPDATE_MEMBER_CARD_USER = 'https://api.weixin.qq.com/card/membercard/updateuser'; |
80
|
|
|
const API_CREATE_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/submit'; |
81
|
|
|
const API_UPDATE_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/update'; |
82
|
1 |
|
const API_GET_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/get'; |
83
|
|
|
const API_LIST_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/batchget'; |
84
|
1 |
|
const API_GET_CATEGORIES = 'https://api.weixin.qq.com/card/getapplyprotocol'; |
85
|
|
|
const API_ACTIVATE_GENERAL_CARD = 'https://api.weixin.qq.com/card/generalcard/activate'; |
86
|
|
|
const API_UPDATE_GENERAL_CARD_USER = 'https://api.weixin.qq.com/card/generalcard/updateuser'; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* 获取卡券颜色. |
90
|
|
|
* |
91
|
|
|
* @return \EasyWeChat\Support\Collection |
92
|
|
|
*/ |
93
|
|
|
public function getColors() |
94
|
|
|
{ |
95
|
|
|
return $this->parseJSON('get', [self::API_GET_COLORS]); |
96
|
|
|
} |
97
|
1 |
|
|
98
|
|
|
/** |
99
|
|
|
* 创建卡券. |
100
|
|
|
* |
101
|
1 |
|
* @param string $cardType |
102
|
1 |
|
* @param array $baseInfo |
103
|
1 |
|
* @param array $especial |
104
|
1 |
|
* @param array $advancedInfo |
105
|
|
|
* |
106
|
1 |
|
* @return \EasyWeChat\Support\Collection |
107
|
|
|
*/ |
108
|
|
|
public function create($cardType = 'member_card', array $baseInfo = [], array $especial = [], array $advancedInfo = []) |
109
|
|
|
{ |
110
|
|
|
$params = [ |
111
|
|
|
'card' => [ |
112
|
|
|
'card_type' => strtoupper($cardType), |
113
|
|
|
strtolower($cardType) => array_merge(['base_info' => $baseInfo], $especial, $advancedInfo), |
114
|
|
|
], |
115
|
|
|
]; |
116
|
1 |
|
|
117
|
|
|
return $this->parseJSON('json', [self::API_CREATE_CARD, $params]); |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* 创建二维码. |
122
|
|
|
* |
123
|
|
|
* @param array $cards |
124
|
|
|
* |
125
|
|
|
* @return \EasyWeChat\Support\Collection |
126
|
|
|
*/ |
127
|
|
|
public function QRCode(array $cards = []) |
128
|
|
|
{ |
129
|
|
|
return $this->parseJSON('json', [self::API_CREATE_QRCODE, $cards]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* ticket 换取二维码图片. |
134
|
|
|
* |
135
|
|
|
* @param string $ticket |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public function showQRCode($ticket = null) |
140
|
|
|
{ |
141
|
|
|
$params = [ |
142
|
|
|
'ticket' => $ticket, |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
$http = $this->getHttp(); |
146
|
|
|
|
147
|
|
|
/** @var ResponseInterface $response */ |
148
|
|
|
$response = $http->get(self::API_SHOW_QRCODE, $params); |
149
|
|
|
|
150
|
|
|
return [ |
151
|
|
|
'status' => $response->getStatusCode(), |
152
|
|
|
'reason' => $response->getReasonPhrase(), |
153
|
|
|
'headers' => $response->getHeaders(), |
154
|
|
|
'body' => strval($response->getBody()), |
155
|
1 |
|
'url' => self::API_SHOW_QRCODE.'?'.http_build_query($params), |
156
|
|
|
]; |
157
|
1 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* 通过ticket换取二维码 链接. |
161
|
|
|
* |
162
|
|
|
* @param string $ticket |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public function getQRCodeUrl($ticket) |
167
|
2 |
|
{ |
168
|
|
|
return self::API_SHOW_QRCODE.'?ticket='.$ticket; |
169
|
2 |
|
} |
170
|
|
|
|
171
|
2 |
|
/** |
172
|
|
|
* 获取 卡券 Api_ticket. |
173
|
2 |
|
* |
174
|
1 |
|
* @param bool $refresh 是否强制刷新 |
175
|
|
|
* |
176
|
1 |
|
* @return string $apiTicket |
177
|
|
|
*/ |
178
|
1 |
View Code Duplication |
public function getAPITicket($refresh = false) |
179
|
|
|
{ |
180
|
|
|
$key = $this->getTicketCacheKey(); |
181
|
1 |
|
|
182
|
|
|
$ticket = $this->getCache()->fetch($key); |
183
|
|
|
|
184
|
|
|
if (!$ticket || $refresh) { |
185
|
|
|
$result = $this->parseJSON('get', [self::API_GET_CARD_TICKET, ['type' => 'wx_card']]); |
186
|
|
|
|
187
|
|
|
$this->getCache()->save($key, $result['ticket'], $result['expires_in'] - 500); |
188
|
|
|
|
189
|
|
|
return $result['ticket']; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $ticket; |
193
|
1 |
|
} |
194
|
1 |
|
|
195
|
1 |
|
/** |
196
|
|
|
* 微信卡券:JSAPI 卡券发放. |
197
|
|
|
* |
198
|
|
|
* @param array $cards |
199
|
|
|
* |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
|
|
public function jsConfigForAssign(array $cards) |
203
|
|
|
{ |
204
|
|
|
return json_encode(array_map(function ($card) { |
205
|
|
|
return $this->attachExtension($card['card_id'], $card); |
206
|
1 |
|
}, $cards)); |
207
|
|
|
} |
208
|
1 |
|
|
209
|
|
|
/** |
210
|
1 |
|
* 生成 js添加到卡包 需要的 card_list 项. |
211
|
1 |
|
* |
212
|
1 |
|
* @param string $cardId |
213
|
1 |
|
* @param array $extension |
214
|
1 |
|
* |
215
|
1 |
|
* @return string |
216
|
1 |
|
*/ |
217
|
1 |
|
public function attachExtension($cardId, array $extension = []) |
218
|
1 |
|
{ |
219
|
1 |
|
$timestamp = time(); |
220
|
1 |
|
$ext = [ |
221
|
1 |
|
'code' => Arr::get($extension, 'code'), |
222
|
1 |
|
'openid' => Arr::get($extension, 'openid', Arr::get($extension, 'open_id')), |
223
|
1 |
|
'timestamp' => $timestamp, |
224
|
|
|
'outer_id' => Arr::get($extension, 'outer_id'), |
225
|
|
|
'balance' => Arr::get($extension, 'balance'), |
226
|
1 |
|
]; |
227
|
1 |
|
$ext['signature'] = $this->getSignature( |
228
|
1 |
|
$this->getAPITicket(), |
229
|
|
|
$timestamp, |
230
|
|
|
$cardId, |
231
|
|
|
$ext['code'], |
232
|
|
|
$ext['openid'], |
233
|
|
|
$ext['balance'] |
234
|
|
|
); |
235
|
|
|
|
236
|
1 |
|
return [ |
237
|
|
|
'cardId' => $cardId, |
238
|
1 |
|
'cardExt' => json_encode($ext), |
239
|
1 |
|
]; |
240
|
|
|
} |
241
|
1 |
|
|
242
|
|
|
/** |
243
|
|
|
* 生成签名. |
244
|
|
|
* |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
|
|
public function getSignature() |
248
|
|
|
{ |
249
|
|
|
$params = func_get_args(); |
250
|
|
|
sort($params, SORT_STRING); |
251
|
|
|
|
252
|
|
|
return sha1(implode($params)); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
1 |
|
* 创建货架接口. |
257
|
|
|
* |
258
|
|
|
* @param string $banner |
259
|
1 |
|
* @param string $pageTitle |
260
|
1 |
|
* @param bool $canShare |
261
|
1 |
|
* @param string $scene [SCENE_NEAR_BY 附近,SCENE_MENU 自定义菜单,SCENE_QRCODE 二维码,SCENE_ARTICLE 公众号文章, |
262
|
1 |
|
* SCENE_H5 h5页面,SCENE_IVR 自动回复,SCENE_CARD_CUSTOM_CELL 卡券自定义cell] |
263
|
1 |
|
* @param array $cardList |
264
|
1 |
|
* |
265
|
|
|
* @return \EasyWeChat\Support\Collection |
266
|
1 |
|
*/ |
267
|
|
|
public function createLandingPage($banner, $pageTitle, $canShare, $scene, $cardList) |
268
|
|
|
{ |
269
|
|
|
$params = [ |
270
|
|
|
'banner' => $banner, |
271
|
|
|
'page_title' => $pageTitle, |
272
|
|
|
'can_share' => $canShare, |
273
|
|
|
'scene' => $scene, |
274
|
|
|
'card_list' => $cardList, |
275
|
|
|
]; |
276
|
|
|
|
277
|
1 |
|
return $this->parseJSON('json', [self::API_CREATE_LANDING_PAGE, $params]); |
278
|
|
|
} |
279
|
|
|
|
280
|
1 |
|
/** |
281
|
1 |
|
* 导入code接口. |
282
|
1 |
|
* |
283
|
|
|
* @param string $cardId |
284
|
1 |
|
* @param array $code |
285
|
|
|
* |
286
|
|
|
* @return \EasyWeChat\Support\Collection |
287
|
|
|
*/ |
288
|
|
View Code Duplication |
public function deposit($cardId, $code) |
289
|
|
|
{ |
290
|
|
|
$params = [ |
291
|
|
|
'card_id' => $cardId, |
292
|
|
|
'code' => $code, |
293
|
|
|
]; |
294
|
1 |
|
|
295
|
|
|
return $this->parseJSON('json', [self::API_DEPOSIT_CODE, $params]); |
296
|
|
|
} |
297
|
1 |
|
|
298
|
1 |
|
/** |
299
|
|
|
* 查询导入code数目. |
300
|
1 |
|
* |
301
|
|
|
* @param string $cardId |
302
|
|
|
* |
303
|
|
|
* @return \EasyWeChat\Support\Collection |
304
|
|
|
*/ |
305
|
|
View Code Duplication |
public function getDepositedCount($cardId) |
306
|
|
|
{ |
307
|
|
|
$params = [ |
308
|
|
|
'card_id' => $cardId, |
309
|
|
|
]; |
310
|
|
|
|
311
|
1 |
|
return $this->parseJSON('json', [self::API_GET_DEPOSIT_COUNT, $params]); |
312
|
|
|
} |
313
|
|
|
|
314
|
1 |
|
/** |
315
|
1 |
|
* 核查code接口. |
316
|
1 |
|
* |
317
|
|
|
* @param string $cardId |
318
|
1 |
|
* @param array $code |
319
|
|
|
* |
320
|
|
|
* @return \EasyWeChat\Support\Collection |
321
|
|
|
*/ |
322
|
|
View Code Duplication |
public function checkCode($cardId, $code) |
323
|
|
|
{ |
324
|
|
|
$params = [ |
325
|
|
|
'card_id' => $cardId, |
326
|
|
|
'code' => $code, |
327
|
|
|
]; |
328
|
|
|
|
329
|
|
|
return $this->parseJSON('json', [self::API_CHECK_CODE, $params]); |
330
|
1 |
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
1 |
|
* 查询Code接口. |
334
|
1 |
|
* |
335
|
1 |
|
* @param string $code |
336
|
1 |
|
* @param bool $checkConsume |
337
|
|
|
* @param string $cardId |
338
|
1 |
|
* |
339
|
|
|
* @return \EasyWeChat\Support\Collection |
340
|
|
|
*/ |
341
|
|
View Code Duplication |
public function getCode($code, $checkConsume, $cardId) |
342
|
|
|
{ |
343
|
|
|
$params = [ |
344
|
|
|
'code' => $code, |
345
|
|
|
'check_consume' => $checkConsume, |
346
|
|
|
'card_id' => $cardId, |
347
|
|
|
]; |
348
|
|
|
|
349
|
1 |
|
return $this->parseJSON('json', [self::API_GET_CODE, $params]); |
350
|
|
|
} |
351
|
1 |
|
|
352
|
1 |
|
/** |
353
|
1 |
|
* 核销Code接口. |
354
|
|
|
* |
355
|
|
|
* @param string $code |
356
|
1 |
|
* @param string $cardId |
357
|
1 |
|
* |
358
|
|
|
* @return \EasyWeChat\Support\Collection |
359
|
1 |
|
*/ |
360
|
1 |
|
public function consume($code, $cardId = null) |
361
|
1 |
|
{ |
362
|
|
|
if (strlen($code) === 28 && $cardId && strlen($cardId) !== 28) { |
|
|
|
|
363
|
1 |
|
list($code, $cardId) = [$cardId, $code]; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
$params = [ |
367
|
|
|
'code' => $code, |
368
|
|
|
]; |
369
|
|
|
|
370
|
|
|
if ($cardId) { |
|
|
|
|
371
|
|
|
$params['card_id'] = $cardId; |
372
|
|
|
} |
373
|
1 |
|
|
374
|
|
|
return $this->parseJSON('json', [self::API_CONSUME_CARD, $params]); |
375
|
|
|
} |
376
|
1 |
|
|
377
|
1 |
|
/** |
378
|
|
|
* Code解码接口. |
379
|
1 |
|
* |
380
|
|
|
* @param string $encryptedCode |
381
|
|
|
* |
382
|
|
|
* @return \EasyWeChat\Support\Collection |
383
|
|
|
*/ |
384
|
|
|
public function decryptCode($encryptedCode) |
385
|
|
|
{ |
386
|
|
|
$params = [ |
387
|
|
|
'encrypt_code' => $encryptedCode, |
388
|
|
|
]; |
389
|
1 |
|
|
390
|
|
|
return $this->parseJSON('json', [self::API_DECRYPT_CODE, $params]); |
391
|
|
|
} |
392
|
1 |
|
|
393
|
1 |
|
/** |
394
|
|
|
* 图文消息群发卡券. |
395
|
1 |
|
* |
396
|
|
|
* @param string $cardId |
397
|
|
|
* |
398
|
|
|
* @return \EasyWeChat\Support\Collection |
399
|
|
|
*/ |
400
|
|
View Code Duplication |
public function getHtml($cardId) |
401
|
|
|
{ |
402
|
|
|
$params = [ |
403
|
|
|
'card_id' => $cardId, |
404
|
|
|
]; |
405
|
1 |
|
|
406
|
|
|
return $this->parseJSON('json', [self::API_GET_HTML, $params]); |
407
|
|
|
} |
408
|
1 |
|
|
409
|
1 |
|
/** |
410
|
|
|
* 设置测试白名单. |
411
|
1 |
|
* |
412
|
|
|
* @param array $openids |
413
|
|
|
* |
414
|
|
|
* @return \EasyWeChat\Support\Collection |
415
|
|
|
*/ |
416
|
|
|
public function setTestWhitelist($openids) |
417
|
|
|
{ |
418
|
|
|
$params = [ |
419
|
|
|
'openid' => $openids, |
420
|
|
|
]; |
421
|
1 |
|
|
422
|
|
|
return $this->parseJSON('json', [self::API_SET_TEST_WHITE_LIST, $params]); |
423
|
|
|
} |
424
|
1 |
|
|
425
|
1 |
|
/** |
426
|
|
|
* 设置测试白名单(by username). |
427
|
1 |
|
* |
428
|
|
|
* @param array $usernames |
429
|
|
|
* |
430
|
|
|
* @return \EasyWeChat\Support\Collection |
431
|
|
|
*/ |
432
|
|
|
public function setTestWhitelistByUsername($usernames) |
433
|
|
|
{ |
434
|
|
|
$params = [ |
435
|
|
|
'username' => $usernames, |
436
|
|
|
]; |
437
|
|
|
|
438
|
1 |
|
return $this->parseJSON('json', [self::API_SET_TEST_WHITE_LIST, $params]); |
439
|
|
|
} |
440
|
|
|
|
441
|
1 |
|
/** |
442
|
1 |
|
* 获取用户已领取卡券接口. |
443
|
1 |
|
* |
444
|
|
|
* @param string $openid |
445
|
1 |
|
* @param string $cardId |
446
|
|
|
* |
447
|
|
|
* @return \EasyWeChat\Support\Collection |
448
|
|
|
*/ |
449
|
|
View Code Duplication |
public function getUserCards($openid, $cardId = '') |
450
|
|
|
{ |
451
|
|
|
$params = [ |
452
|
|
|
'openid' => $openid, |
453
|
|
|
'card_id' => $cardId, |
454
|
|
|
]; |
455
|
1 |
|
|
456
|
|
|
return $this->parseJSON('json', [self::API_GET_CARD_LIST, $params]); |
457
|
|
|
} |
458
|
1 |
|
|
459
|
1 |
|
/** |
460
|
|
|
* 查看卡券详情. |
461
|
1 |
|
* |
462
|
|
|
* @param string $cardId |
463
|
|
|
* |
464
|
|
|
* @return \EasyWeChat\Support\Collection |
465
|
|
|
*/ |
466
|
|
View Code Duplication |
public function getCard($cardId) |
467
|
|
|
{ |
468
|
|
|
$params = [ |
469
|
|
|
'card_id' => $cardId, |
470
|
|
|
]; |
471
|
|
|
|
472
|
|
|
return $this->parseJSON('json', [self::API_GET_CARD, $params]); |
473
|
1 |
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
1 |
|
* 批量查询卡列表. |
477
|
1 |
|
* |
478
|
1 |
|
* @param int $offset |
479
|
1 |
|
* @param int $count |
480
|
|
|
* @param string $statusList |
481
|
1 |
|
* |
482
|
|
|
* @return \EasyWeChat\Support\Collection |
483
|
|
|
*/ |
484
|
|
View Code Duplication |
public function lists($offset = 0, $count = 10, $statusList = 'CARD_STATUS_VERIFY_OK') |
485
|
|
|
{ |
486
|
|
|
$params = [ |
487
|
|
|
'offset' => $offset, |
488
|
|
|
'count' => $count, |
489
|
|
|
'status_list' => $statusList, |
490
|
|
|
]; |
491
|
|
|
|
492
|
|
|
return $this->parseJSON('json', [self::API_LIST_CARD, $params]); |
493
|
|
|
} |
494
|
1 |
|
|
495
|
|
|
/** |
496
|
1 |
|
* 更改卡券信息接口 and 设置跟随推荐接口. |
497
|
1 |
|
* |
498
|
1 |
|
* @param string $cardId |
499
|
|
|
* @param string $type |
500
|
1 |
|
* @param array $baseInfo |
501
|
1 |
|
* @param array $especial |
502
|
1 |
|
* |
503
|
1 |
|
* @return \EasyWeChat\Support\Collection |
504
|
|
|
*/ |
505
|
1 |
|
public function update($cardId, $type, $baseInfo = [], $especial = []) |
506
|
|
|
{ |
507
|
1 |
|
$card = []; |
508
|
|
|
$card['card_id'] = $cardId; |
509
|
|
|
$card[$type] = []; |
510
|
|
|
|
511
|
|
|
$cardInfo = []; |
512
|
|
|
if ($baseInfo) { |
|
|
|
|
513
|
|
|
$cardInfo['base_info'] = $baseInfo; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
$card[$type] = array_merge($cardInfo, $especial); |
517
|
|
|
|
518
|
|
|
return $this->parseJSON('json', [self::API_UPDATE_CARD, $card]); |
519
|
1 |
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
1 |
|
* 设置微信买单接口. |
523
|
1 |
|
* 设置买单的 card_id 必须已经配置了门店,否则会报错. |
524
|
1 |
|
* |
525
|
|
|
* @param string $cardId |
526
|
1 |
|
* @param bool $isOpen |
527
|
|
|
* |
528
|
|
|
* @return \EasyWeChat\Support\Collection |
529
|
|
|
*/ |
530
|
|
View Code Duplication |
public function setPayCell($cardId, $isOpen = true) |
531
|
|
|
{ |
532
|
|
|
$params = [ |
533
|
|
|
'card_id' => $cardId, |
534
|
|
|
'is_open' => $isOpen, |
535
|
|
|
]; |
536
|
|
|
|
537
|
1 |
|
return $this->parseJSON('json', [self::API_SET_PAY_CELL, $params]); |
538
|
|
|
} |
539
|
1 |
|
|
540
|
|
|
/** |
541
|
|
|
* 增加库存. |
542
|
|
|
* |
543
|
|
|
* @param string $cardId |
544
|
|
|
* @param int $amount |
545
|
|
|
* |
546
|
|
|
* @return \EasyWeChat\Support\Collection |
547
|
|
|
*/ |
548
|
|
|
public function increaseStock($cardId, $amount) |
549
|
|
|
{ |
550
|
1 |
|
return $this->updateStock($cardId, $amount, 'increase'); |
551
|
|
|
} |
552
|
1 |
|
|
553
|
|
|
/** |
554
|
|
|
* 减少库存. |
555
|
|
|
* |
556
|
|
|
* @param string $cardId |
557
|
|
|
* @param int $amount |
558
|
|
|
* |
559
|
|
|
* @return \EasyWeChat\Support\Collection |
560
|
|
|
*/ |
561
|
|
|
public function reduceStock($cardId, $amount) |
562
|
|
|
{ |
563
|
|
|
return $this->updateStock($cardId, $amount, 'reduce'); |
564
|
1 |
|
} |
565
|
|
|
|
566
|
1 |
|
/** |
567
|
|
|
* 修改库存接口. |
568
|
1 |
|
* |
569
|
1 |
|
* @param string $cardId |
570
|
1 |
|
* @param int $amount |
571
|
|
|
* @param string $action |
572
|
1 |
|
* |
573
|
|
|
* @return \EasyWeChat\Support\Collection |
574
|
|
|
*/ |
575
|
|
|
protected function updateStock($cardId, $amount, $action = 'increase') |
576
|
|
|
{ |
577
|
|
|
$key = $action === 'increase' ? 'increase_stock_value' : 'reduce_stock_value'; |
578
|
|
|
$params = [ |
579
|
|
|
'card_id' => $cardId, |
580
|
|
|
$key => abs($amount), |
581
|
|
|
]; |
582
|
|
|
|
583
|
|
|
return $this->parseJSON('json', [self::API_MODIFY_STOCK, $params]); |
584
|
1 |
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
1 |
|
* 更改Code接口. |
588
|
1 |
|
* |
589
|
1 |
|
* @param string $code |
590
|
1 |
|
* @param string $newCode |
591
|
|
|
* @param array $cardId |
592
|
1 |
|
* |
593
|
|
|
* @return \EasyWeChat\Support\Collection |
594
|
|
|
*/ |
595
|
|
View Code Duplication |
public function updateCode($code, $newCode, $cardId = []) |
596
|
|
|
{ |
597
|
|
|
$params = [ |
598
|
|
|
'code' => $code, |
599
|
|
|
'new_code' => $newCode, |
600
|
|
|
'card_id' => $cardId, |
601
|
|
|
]; |
602
|
1 |
|
|
603
|
|
|
return $this->parseJSON('json', [self::API_UPDATE_CODE, $params]); |
604
|
|
|
} |
605
|
1 |
|
|
606
|
1 |
|
/** |
607
|
|
|
* 删除卡券接口. |
608
|
1 |
|
* |
609
|
|
|
* @param string $cardId |
610
|
|
|
* |
611
|
|
|
* @return \EasyWeChat\Support\Collection |
612
|
|
|
*/ |
613
|
|
View Code Duplication |
public function delete($cardId) |
614
|
|
|
{ |
615
|
|
|
$params = [ |
616
|
|
|
'card_id' => $cardId, |
617
|
|
|
]; |
618
|
|
|
|
619
|
1 |
|
return $this->parseJSON('json', [self::API_DELETE_CARD, $params]); |
620
|
|
|
} |
621
|
|
|
|
622
|
1 |
|
/** |
623
|
1 |
|
* 设置卡券失效. |
624
|
1 |
|
* |
625
|
|
|
* @param string $code |
626
|
1 |
|
* @param string $cardId |
627
|
|
|
* |
628
|
|
|
* @return \EasyWeChat\Support\Collection |
629
|
|
|
*/ |
630
|
|
View Code Duplication |
public function disable($code, $cardId = '') |
631
|
|
|
{ |
632
|
|
|
$params = [ |
633
|
|
|
'code' => $code, |
634
|
|
|
'card_id' => $cardId, |
635
|
|
|
]; |
636
|
1 |
|
|
637
|
|
|
return $this->parseJSON('json', [self::API_DISABLE_CARD, $params]); |
638
|
1 |
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* 会员卡接口激活. |
642
|
|
|
* |
643
|
|
|
* @param array $info |
644
|
|
|
* |
645
|
|
|
* @return \EasyWeChat\Support\Collection |
646
|
|
|
*/ |
647
|
|
|
public function activate($info = [], $cardType = 'member_card') |
648
|
|
|
{ |
649
|
|
|
if ($cardType === 'general_card') { |
650
|
1 |
|
return $this->parseJSON('json', [self::API_ACTIVATE_GENERAL_CARD, $info]); |
651
|
|
|
} |
652
|
1 |
|
|
653
|
|
|
return $this->parseJSON('json', [self::API_ACTIVATE_MEMBER_CARD, $info]); |
654
|
1 |
|
} |
655
|
|
|
|
656
|
|
|
/** |
657
|
|
|
* 设置开卡字段接口. |
658
|
|
|
* |
659
|
|
|
* @param string $cardId |
660
|
|
|
* @param array $requiredForm |
661
|
|
|
* @param array $optionalForm |
662
|
|
|
* |
663
|
|
|
* @return \EasyWeChat\Support\Collection |
664
|
|
|
*/ |
665
|
1 |
|
public function activateUserForm($cardId, array $requiredForm = [], array $optionalForm = []) |
666
|
|
|
{ |
667
|
|
|
$params = array_merge(['card_id' => $cardId], $requiredForm, $optionalForm); |
668
|
1 |
|
|
669
|
1 |
|
return $this->parseJSON('json', [self::API_ACTIVATE_MEMBER_USER_FORM, $params]); |
670
|
1 |
|
} |
671
|
|
|
|
672
|
1 |
|
/** |
673
|
|
|
* 拉取会员信息接口. |
674
|
|
|
* |
675
|
|
|
* @param string $cardId |
676
|
|
|
* @param string $code |
677
|
|
|
* |
678
|
|
|
* @return \EasyWeChat\Support\Collection |
679
|
|
|
*/ |
680
|
|
View Code Duplication |
public function getMemberCardUser($cardId, $code) |
681
|
|
|
{ |
682
|
1 |
|
$params = [ |
683
|
|
|
'card_id' => $cardId, |
684
|
1 |
|
'code' => $code, |
685
|
|
|
]; |
686
|
|
|
|
687
|
|
|
return $this->parseJSON('json', [self::API_GET_MEMBER_USER_INFO, $params]); |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
/** |
691
|
|
|
* 更新会员信息. |
692
|
|
|
* |
693
|
|
|
* @param array $params |
694
|
1 |
|
* |
695
|
|
|
* @return \EasyWeChat\Support\Collection |
696
|
|
|
*/ |
697
|
1 |
|
public function updateMemberCardUser(array $params = []) |
698
|
1 |
|
{ |
699
|
1 |
|
return $this->parseJSON('json', [self::API_UPDATE_MEMBER_CARD_USER, $params]); |
700
|
1 |
|
} |
701
|
1 |
|
|
702
|
1 |
|
/** |
703
|
1 |
|
* 更新通用员信息. |
704
|
1 |
|
* |
705
|
1 |
|
* @param array $params |
706
|
1 |
|
* |
707
|
1 |
|
* @return \EasyWeChat\Support\Collection |
708
|
1 |
|
*/ |
709
|
|
|
public function updateGeneralCardUser(array $params = []) |
710
|
1 |
|
{ |
711
|
|
|
return $this->parseJSON('json', [self::API_UPDATE_GENERAL_CARD_USER, $params]); |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
/** |
715
|
|
|
* 添加子商户. |
716
|
|
|
* |
717
|
|
|
* @param array $info |
718
|
|
|
* |
719
|
|
|
* @return \EasyWeChat\Support\Collection |
720
|
|
|
*/ |
721
|
1 |
View Code Duplication |
public function createSubMerchant(array $info = []) |
722
|
|
|
{ |
723
|
|
|
$params = [ |
724
|
1 |
|
'info' => Arr::only($info, [ |
725
|
1 |
|
'brand_name', |
726
|
1 |
|
'logo_url', |
727
|
1 |
|
'protocol', |
728
|
1 |
|
'end_time', |
729
|
1 |
|
'primary_category_id', |
730
|
1 |
|
'secondary_category_id', |
731
|
1 |
|
'agreement_media_id', |
732
|
1 |
|
'operator_media_id', |
733
|
1 |
|
'app_id', |
734
|
1 |
|
]), |
735
|
1 |
|
]; |
736
|
1 |
|
|
737
|
|
|
return $this->parseJSON('json', [self::API_CREATE_SUB_MERCHANT, $params]); |
738
|
1 |
|
} |
739
|
|
|
|
740
|
|
|
/** |
741
|
|
|
* 更新子商户. |
742
|
|
|
* |
743
|
|
|
* @param int $merchantId |
744
|
|
|
* @param array $info |
745
|
|
|
* |
746
|
|
|
* @return \EasyWeChat\Support\Collection |
747
|
|
|
*/ |
748
|
|
View Code Duplication |
public function updateSubMerchant($merchantId, array $info = []) |
749
|
|
|
{ |
750
|
|
|
$params = [ |
751
|
|
|
'info' => array_merge(['merchant_id' => $merchantId], |
752
|
|
|
Arr::only($info, [ |
753
|
|
|
'brand_name', |
754
|
|
|
'logo_url', |
755
|
|
|
'protocol', |
756
|
|
|
'end_time', |
757
|
|
|
'primary_category_id', |
758
|
|
|
'secondary_category_id', |
759
|
|
|
'agreement_media_id', |
760
|
|
|
'operator_media_id', |
761
|
|
|
'app_id', |
762
|
|
|
])), |
763
|
|
|
]; |
764
|
|
|
|
765
|
|
|
return $this->parseJSON('json', [self::API_UPDATE_SUB_MERCHANT, $params]); |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
/** |
769
|
|
|
* 获取子商户信息. |
770
|
|
|
* |
771
|
|
|
* @param int $merchantId |
772
|
|
|
* |
773
|
|
|
* @return \EasyWeChat\Support\Collection |
774
|
|
|
*/ |
775
|
|
|
public function getSubMerchant($merchantId) |
776
|
|
|
{ |
777
|
|
|
return $this->parseJSON('json', [self::API_GET_SUB_MERCHANT, ['merchant_id' => $merchantId]]); |
778
|
1 |
|
} |
779
|
|
|
|
780
|
1 |
|
/** |
781
|
|
|
* 批量获取子商户信息. |
782
|
|
|
* |
783
|
|
|
* @param int $beginId |
784
|
|
|
* @param int $limit |
785
|
|
|
* @param string $status |
786
|
|
|
* |
787
|
|
|
* @return \EasyWeChat\Support\Collection |
788
|
|
|
*/ |
789
|
|
|
public function listSubMerchants($beginId = 0, $limit = 50, $status = 'CHECKING') |
790
|
2 |
|
{ |
791
|
|
|
$params = [ |
792
|
2 |
|
'begin_id' => $beginId, |
793
|
|
|
'limit' => $limit, |
794
|
2 |
|
'status' => $status, |
795
|
|
|
]; |
796
|
|
|
|
797
|
|
|
return $this->parseJSON('json', [self::API_LIST_SUB_MERCHANT, $params]); |
798
|
|
|
} |
799
|
|
|
|
800
|
|
|
/** |
801
|
|
|
* 卡券开放类目查询接口. |
802
|
2 |
|
* |
803
|
|
|
* @return \EasyWeChat\Support\Collection |
804
|
2 |
|
*/ |
805
|
|
|
public function getCategories() |
806
|
|
|
{ |
807
|
|
|
return $this->parseJSON('get', [self::API_GET_CATEGORIES]); |
808
|
|
|
} |
809
|
|
|
|
810
|
|
|
/** |
811
|
|
|
* Set cache manager. |
812
|
|
|
* |
813
|
|
|
* @param \Doctrine\Common\Cache\Cache $cache |
814
|
1 |
|
* |
815
|
|
|
* @return $this |
816
|
1 |
|
*/ |
817
|
|
|
public function setCache(Cache $cache) |
818
|
1 |
|
{ |
819
|
|
|
$this->cache = $cache; |
820
|
|
|
|
821
|
|
|
return $this; |
822
|
|
|
} |
823
|
|
|
|
824
|
|
|
/** |
825
|
|
|
* Return cache manager. |
826
|
|
|
* |
827
|
|
|
* @return \Doctrine\Common\Cache\Cache |
828
|
|
|
*/ |
829
|
|
|
public function getCache() |
830
|
|
|
{ |
831
|
|
|
return $this->cache ?: $this->cache = new FilesystemCache(sys_get_temp_dir()); |
832
|
|
|
} |
833
|
|
|
|
834
|
|
|
/** |
835
|
|
|
* Set Api_ticket cache prifix. |
836
|
|
|
* |
837
|
|
|
* @param string $prefix |
838
|
|
|
* |
839
|
|
|
* @return $this |
840
|
|
|
*/ |
841
|
|
|
public function setTicketCachePrefix($prefix) |
842
|
|
|
{ |
843
|
|
|
$this->ticketCachePrefix = $prefix; |
844
|
|
|
|
845
|
|
|
return $this; |
846
|
|
|
} |
847
|
|
|
|
848
|
|
|
/** |
849
|
|
|
* Set Api_ticket cache key. |
850
|
|
|
* |
851
|
|
|
* @param string $cacheKey |
852
|
|
|
* |
853
|
|
|
* @return $this |
854
|
|
|
*/ |
855
|
|
|
public function setTicketCacheKey($cacheKey) |
856
|
|
|
{ |
857
|
|
|
$this->ticketCacheKey = $cacheKey; |
858
|
|
|
|
859
|
|
|
return $this; |
860
|
|
|
} |
861
|
|
|
|
862
|
|
|
/** |
863
|
|
|
* Get Api_ticket token cache key. |
864
|
|
|
* |
865
|
|
|
* @return string $this->ticketCacheKey |
866
|
|
|
*/ |
867
|
|
|
public function getTicketCacheKey() |
868
|
|
|
{ |
869
|
|
|
if (is_null($this->ticketCacheKey)) { |
870
|
|
|
return $this->ticketCachePrefix.$this->appId; |
|
|
|
|
871
|
|
|
} |
872
|
|
|
|
873
|
|
|
return $this->ticketCacheKey; |
874
|
|
|
} |
875
|
|
|
|
876
|
|
|
/** |
877
|
|
|
* Set current url. |
878
|
|
|
* |
879
|
|
|
* @param string $url |
880
|
|
|
* |
881
|
|
|
* @return Card |
882
|
|
|
*/ |
883
|
|
|
public function setUrl($url) |
884
|
|
|
{ |
885
|
|
|
$this->url = $url; |
|
|
|
|
886
|
|
|
|
887
|
|
|
return $this; |
888
|
|
|
} |
889
|
|
|
} |
890
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: