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