Passed
Pull Request — master (#1604)
by
unknown
03:16
created

Client::reduceStock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 1
    public function colors()
51
    {
52 1
        return $this->httpGet('card/getcolors');
53
    }
54
55
    /**
56
     * 卡券开放类目查询接口.
57
     *
58
     * @return mixed
59
     */
60 1
    public function categories()
61
    {
62 1
        return $this->httpGet('card/getapplyprotocol');
63
    }
64
65
    /**
66
     * 创建卡券.
67
     *
68
     * @param string $cardType
69
     * @param array  $attributes
70
     *
71
     * @return mixed
72
     */
73 1
    public function create($cardType = 'member_card', array $attributes)
74
    {
75
        $params = [
76
            'card' => [
77 1
                'card_type' => strtoupper($cardType),
78 1
                strtolower($cardType) => $attributes,
79
            ],
80
        ];
81
82 1
        return $this->httpPostJson('card/create', $params);
83
    }
84
85
    /**
86
     * 查看卡券详情.
87
     *
88
     * @param string $cardId
89
     *
90
     * @return mixed
91
     */
92 1
    public function get($cardId)
93
    {
94
        $params = [
95 1
            'card_id' => $cardId,
96
        ];
97
98 1
        return $this->httpPostJson('card/get', $params);
99
    }
100
101
    /**
102
     * 批量查询卡列表.
103
     *
104
     * @param int    $offset
105
     * @param int    $count
106
     * @param string $statusList
107
     *
108
     * @return mixed
109
     */
110 1
    public function list($offset = 0, $count = 10, $statusList = 'CARD_STATUS_VERIFY_OK')
111
    {
112
        $params = [
113 1
            'offset' => $offset,
114 1
            'count' => $count,
115 1
            'status_list' => $statusList,
116
        ];
117
118 1
        return $this->httpPostJson('card/batchget', $params);
119
    }
120
121
    /**
122
     * 更改卡券信息接口 and 设置跟随推荐接口.
123
     *
124
     * @param string $cardId
125
     * @param string $type
126
     * @param array  $attributes
127
     *
128
     * @return mixed
129
     */
130 1
    public function update($cardId, $type, array $attributes = [])
131
    {
132 1
        $card = [];
133 1
        $card['card_id'] = $cardId;
134 1
        $card[strtolower($type)] = $attributes;
135
136 1
        return $this->httpPostJson('card/update', $card);
137
    }
138
139
    /**
140
     * 删除卡券接口.
141
     *
142
     * @param string $cardId
143
     *
144
     * @return mixed
145
     */
146 1
    public function delete($cardId)
147
    {
148
        $params = [
149 1
            'card_id' => $cardId,
150
        ];
151
152 1
        return $this->httpPostJson('card/delete', $params);
153
    }
154
155
    /**
156
     * 创建二维码.
157
     *
158
     * @param array $cards
159
     *
160
     * @return mixed
161
     */
162 1
    public function createQrCode(array $cards)
163
    {
164 1
        return $this->httpPostJson('card/qrcode/create', $cards);
165
    }
166
167
    /**
168
     * ticket 换取二维码图片.
169
     *
170
     * @param string $ticket
171
     *
172
     * @return array
173
     */
174 1
    public function getQrCode($ticket)
175
    {
176 1
        $baseUri = 'https://mp.weixin.qq.com/cgi-bin/showqrcode';
177
        $params = [
178 1
            'ticket' => $ticket,
179
        ];
180
181 1
        $response = $this->requestRaw($baseUri, 'GET', $params);
182
183
        return [
184 1
            'status' => $response->getStatusCode(),
185 1
            'reason' => $response->getReasonPhrase(),
186 1
            'headers' => $response->getHeaders(),
187 1
            'body' => strval($response->getBody()),
188 1
            'url' => $baseUri.'?'.http_build_query($params),
189
        ];
190
    }
191
192
    /**
193
     * 通过ticket换取二维码 链接.
194
     *
195
     * @param string $ticket
196
     *
197
     * @return string
198
     */
199 1
    public function getQrCodeUrl($ticket)
200
    {
201 1
        return sprintf('https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s', $ticket);
202
    }
203
204
    /**
205
     * 创建货架接口.
206
     *
207
     * @param string $banner
208
     * @param string $pageTitle
209
     * @param bool   $canShare
210
     * @param string $scene     [SCENE_NEAR_BY 附近,SCENE_MENU 自定义菜单,SCENE_QRCODE 二维码,SCENE_ARTICLE 公众号文章,
211
     *                          SCENE_H5 h5页面,SCENE_IVR 自动回复,SCENE_CARD_CUSTOM_CELL 卡券自定义cell]
212
     * @param array  $cardList
213
     *
214
     * @return mixed
215
     */
216 1
    public function createLandingPage($banner, $pageTitle, $canShare, $scene, $cardList)
217
    {
218
        $params = [
219 1
            'banner' => $banner,
220 1
            'page_title' => $pageTitle,
221 1
            'can_share' => $canShare,
222 1
            'scene' => $scene,
223 1
            'card_list' => $cardList,
224
        ];
225
226 1
        return $this->httpPostJson('card/landingpage/create', $params);
227
    }
228
229
    /**
230
     * 图文消息群发卡券.
231
     *
232
     * @param string $cardId
233
     *
234
     * @return mixed
235
     */
236 1
    public function getHtml($cardId)
237
    {
238
        $params = [
239 1
            'card_id' => $cardId,
240
        ];
241
242 1
        return $this->httpPostJson('card/mpnews/gethtml', $params);
243
    }
244
245
    /**
246
     * 设置测试白名单.
247
     *
248
     * @param array $openids
249
     *
250
     * @return mixed
251
     */
252 1
    public function setTestWhitelist($openids)
253
    {
254
        $params = [
255 1
            'openid' => $openids,
256
        ];
257
258 1
        return $this->httpPostJson('card/testwhitelist/set', $params);
259
    }
260
261
    /**
262
     * 设置测试白名单(by username).
263
     *
264
     * @param array $usernames
265
     *
266
     * @return mixed
267
     */
268 1
    public function setTestWhitelistByName(array $usernames)
269
    {
270
        $params = [
271 1
            'username' => $usernames,
272
        ];
273
274 1
        return $this->httpPostJson('card/testwhitelist/set', $params);
275
    }
276
277
    /**
278
     * 获取用户已领取卡券接口.
279
     *
280
     * @param string $openid
281
     * @param string $cardId
282
     *
283
     * @return mixed
284
     */
285 1
    public function getUserCards($openid, $cardId = '')
286
    {
287
        $params = [
288 1
            'openid' => $openid,
289 1
            'card_id' => $cardId,
290
        ];
291
292 1
        return $this->httpPostJson('card/user/getcardlist', $params);
293
    }
294
295
    /**
296
     * 设置微信买单接口.
297
     * 设置买单的 card_id 必须已经配置了门店,否则会报错.
298
     *
299
     * @param string $cardId
300
     * @param bool   $isOpen
301
     *
302
     * @return mixed
303
     */
304 1
    public function setPayCell($cardId, $isOpen = true)
305
    {
306
        $params = [
307 1
            'card_id' => $cardId,
308 1
            'is_open' => $isOpen,
309
        ];
310
311 1
        return $this->httpPostJson('card/paycell/set', $params);
312
    }
313
314
    /**
315
     * 设置自助核销接口
316
     * 设置买单的 card_id 必须已经配置了门店,否则会报错.
317
     *
318
     * @param string $cardId
319
     * @param bool   $isOpen
320
     * @param bool   $verifyCod
321
     * @param bool   $remarkAmount
322
     *
323
     * @return mixed
324
     */
325 1
    public function setPayConsumeCell($cardId, $isOpen = true, $verifyCod = false, $remarkAmount = false)
326
    {
327
        $params = [
328 1
            'card_id'            => $cardId,
329 1
            'is_open'            => $isOpen,
330 1
            'need_verify_cod'    => $verifyCod,
331 1
            'need_remark_amount' => $remarkAmount,
332
        ];
333
334 1
        return $this->httpPostJson('card/selfconsumecell/set', $params);
335
    }
336
337
    /**
338
     * 增加库存.
339
     *
340
     * @param string $cardId
341
     * @param int    $amount
342
     *
343
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
344
     */
345 1
    public function increaseStock($cardId, $amount)
346
    {
347 1
        return $this->updateStock($cardId, $amount, 'increase');
348
    }
349
350
    /**
351
     * 减少库存.
352
     *
353
     * @param string $cardId
354
     * @param int    $amount
355
     *
356
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
357
     */
358 1
    public function reduceStock($cardId, $amount)
359
    {
360 1
        return $this->updateStock($cardId, $amount, 'reduce');
361
    }
362
363
    /**
364
     * 修改库存接口.
365
     *
366
     * @param string $cardId
367
     * @param int    $amount
368
     * @param string $action
369
     *
370
     * @return mixed
371
     */
372 1
    protected function updateStock($cardId, $amount, $action = 'increase')
373
    {
374 1
        $key = 'increase' === $action ? 'increase_stock_value' : 'reduce_stock_value';
375
        $params = [
376 1
            'card_id' => $cardId,
377 1
            $key => abs($amount),
378
        ];
379
380 1
        return $this->httpPostJson('card/modifystock', $params);
381
    }
382
}
383