892 line
22 KiB

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