WechatCard.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | wechat-php-sdk
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方文档: https://www.kancloud.cn/zoujingli/wechat-php-sdk
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/wechat-php-sdk
  12. // +----------------------------------------------------------------------
  13. namespace Wechat;
  14. use Wechat\Lib\Common;
  15. use Wechat\Lib\Tools;
  16. /**
  17. * 微信卡卷
  18. */
  19. class WechatCard extends Common
  20. {
  21. /** 卡券相关地址 */
  22. const CARD_CREATE = '/card/create?';
  23. // 删除卡卷
  24. const CARD_DELETE = '/card/delete?';
  25. // 更新卡卷信息
  26. const CARD_UPDATE = '/card/update?';
  27. // 获取卡卷详细信息
  28. const CARD_GET = '/card/get?';
  29. // 读取粉丝拥有的卡卷列表
  30. const CARD_USER_GET_LIST = '/card/user/getcardlist?';
  31. // 卡卷核查接口
  32. const CARD_CHECKCODE = '/card/code/checkcode?';
  33. // 卡卷图文群发获取HTML
  34. const CARD_SET_SELFCONSUMECELL = '/card/selfconsumecell/set?';
  35. const CARD_SEND_HTML = '/card/mpnews/gethtml?';
  36. const CARD_BATCHGET = '/card/batchget?';
  37. const CARD_MODIFY_STOCK = '/card/modifystock?';
  38. const CARD_GETCOLORS = '/card/getcolors?';
  39. const CARD_QRCODE_CREATE = '/card/qrcode/create?';
  40. const CARD_CODE_CONSUME = '/card/code/consume?';
  41. const CARD_CODE_DECRYPT = '/card/code/decrypt?';
  42. const CARD_CODE_GET = '/card/code/get?';
  43. const CARD_CODE_UPDATE = '/card/code/update?';
  44. const CARD_CODE_UNAVAILABLE = '/card/code/unavailable?';
  45. const CARD_TESTWHILELIST_SET = '/card/testwhitelist/set?';
  46. const CARD_MEETINGCARD_UPDATEUSER = '/card/meetingticket/updateuser?'; //更新会议门票
  47. const CARD_MEMBERCARD_ACTIVATE = '/card/membercard/activate?'; //激活会员卡
  48. const CARD_MEMBERCARD_UPDATEUSER = '/card/membercard/updateuser?'; //更新会员卡
  49. const CARD_MOVIETICKET_UPDATEUSER = '/card/movieticket/updateuser?'; //更新电影票(未加方法)
  50. const CARD_BOARDINGPASS_CHECKIN = '/card/boardingpass/checkin?'; //飞机票-在线选座(未加方法)
  51. /** 更新红包金额 */
  52. const CARD_LUCKYMONEY_UPDATE = '/card/luckymoney/updateuserbalance?';
  53. /*买单接口*/
  54. const CARD_PAYCELL_SET = '/card/paycell/set?';
  55. /*设置开卡字段接口*/
  56. const CARD_MEMBERCARD_ACTIVATEUSERFORM_SET = '/card/membercard/activateuserform/set?';
  57. /**
  58. * 获取微信卡券 api_ticket
  59. * @param string $appid
  60. * @param string $jsapi_ticket
  61. * @return bool|string
  62. */
  63. public function getJsCardTicket($appid = '', $jsapi_ticket = '')
  64. {
  65. if (!$this->access_token && !$this->getAccessToken()) {
  66. return false;
  67. }
  68. $appid = empty($appid) ? $this->appid : $appid;
  69. if ($jsapi_ticket) {
  70. return $jsapi_ticket;
  71. }
  72. $authname = 'wechat_jsapi_ticket_wxcard_' . $appid;
  73. if (($jsapi_ticket = Tools::getCache($authname))) {
  74. return $jsapi_ticket;
  75. }
  76. $result = Tools::httpGet(self::API_URL_PREFIX . self::GET_TICKET_URL . "access_token={$this->access_token}&type=wx_card");
  77. if ($result) {
  78. $json = json_decode($result, true);
  79. if (empty($json) || !empty($json['errcode'])) {
  80. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  81. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  82. return $this->checkRetry(__FUNCTION__, func_get_args());
  83. }
  84. $expire = $json['expires_in'] ? intval($json['expires_in']) - 100 : 3600;
  85. Tools::setCache($authname, $json['ticket'], $expire);
  86. return $json['ticket'];
  87. }
  88. return false;
  89. }
  90. /**
  91. * 生成选择卡卷JS签名包
  92. * @param string $cardid 卡券Id
  93. * @param string $cardtype 卡券类型
  94. * @param string $shopid 门店Id
  95. * @return array
  96. */
  97. public function createChooseCardJsPackage($cardid = null, $cardtype = null, $shopid = null)
  98. {
  99. $data = array();
  100. $data['api_ticket'] = $this->getJsCardTicket();
  101. $data['app_id'] = $this->appid;
  102. $data['timestamp'] = time();
  103. $data['nonceStr'] = Tools::createNoncestr();
  104. !empty($cardid) && $data['cardId'] = $cardid;
  105. !empty($cardtype) && $data['cardType'] = $cardtype;
  106. !empty($shopid) && $data['shopId'] = $shopid;
  107. $data['cardSign'] = $this->getTicketSignature($data);
  108. $data['signType'] = 'SHA1';
  109. unset($data['api_ticket'], $data['app_id']);
  110. return $data;
  111. }
  112. /**
  113. * 生成添加卡卷JS签名包
  114. * @param string|null $cardid 卡卷ID
  115. * @param array $data 其它限定参数
  116. * @return array
  117. */
  118. public function createAddCardJsPackage($cardid = null, $data = array())
  119. {
  120. $cardList = array();
  121. if (is_array($cardid)) {
  122. foreach ($cardid as $id) {
  123. $cardList[] = array('cardId' => $id, 'cardExt' => json_encode($this->_cardSign($id, $data)));
  124. }
  125. } else {
  126. $cardList[] = array('cardId' => $cardid, 'cardExt' => json_encode($this->_cardSign($cardid, $data)));
  127. }
  128. return array('cardList' => $cardList);
  129. }
  130. /**
  131. * 卡券数据签名
  132. * @param null|string $cardid
  133. * @param array $attr
  134. * @return array
  135. */
  136. private function _cardSign($cardid = null, $attr = array())
  137. {
  138. unset($attr['outer_id']);
  139. $attr['cardId'] = $cardid;
  140. $attr['timestamp'] = time();
  141. $attr['api_ticket'] = $this->getJsCardTicket();
  142. $attr['nonce_str'] = Tools::createNoncestr();
  143. $attr['signature'] = $this->getTicketSignature($attr);
  144. unset($attr['api_ticket']);
  145. return $attr;
  146. }
  147. /**
  148. * 获取微信卡券签名
  149. * @param array $arrdata 签名数组
  150. * @param string $method 签名方法
  151. * @return bool|string 签名值
  152. */
  153. public function getTicketSignature($arrdata, $method = "sha1")
  154. {
  155. if (!function_exists($method)) {
  156. return false;
  157. }
  158. $newArray = array();
  159. foreach ($arrdata as $value) {
  160. array_push($newArray, (string)$value);
  161. }
  162. sort($newArray, SORT_STRING);
  163. return $method(implode($newArray));
  164. }
  165. /**
  166. * 创建卡券
  167. * @param array $data 卡券数据
  168. * @return bool|array 返回数组中card_id为卡券ID
  169. */
  170. public function createCard($data)
  171. {
  172. if (!$this->access_token && !$this->getAccessToken()) {
  173. return false;
  174. }
  175. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CREATE . "access_token={$this->access_token}", Tools::json_encode($data));
  176. if ($result) {
  177. $json = json_decode($result, true);
  178. if (empty($json) || !empty($json['errcode'])) {
  179. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  180. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  181. return $this->checkRetry(__FUNCTION__, func_get_args());
  182. }
  183. return $json;
  184. }
  185. return false;
  186. }
  187. /**
  188. * 更改卡券信息
  189. * 调用该接口更新信息后会重新送审,卡券状态变更为待审核。已被用户领取的卡券会实时更新票面信息。
  190. * @param string $data
  191. * @return bool
  192. */
  193. public function updateCard($data)
  194. {
  195. if (!$this->access_token && !$this->getAccessToken()) {
  196. return false;
  197. }
  198. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_UPDATE . "access_token={$this->access_token}", Tools::json_encode($data));
  199. if ($result) {
  200. $json = json_decode($result, true);
  201. if (empty($json) || !empty($json['errcode'])) {
  202. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  203. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  204. return $this->checkRetry(__FUNCTION__, func_get_args());
  205. }
  206. return true;
  207. }
  208. return false;
  209. }
  210. /**
  211. * 删除卡券
  212. * 允许商户删除任意一类卡券。删除卡券后,该卡券对应已生成的领取用二维码、添加到卡包 JS API 均会失效。
  213. * 注意:删除卡券不能删除已被用户领取,保存在微信客户端中的卡券,已领取的卡券依旧有效。
  214. * @param string $card_id 卡券ID
  215. * @return bool
  216. */
  217. public function delCard($card_id)
  218. {
  219. if (!$this->access_token && !$this->getAccessToken()) {
  220. return false;
  221. }
  222. $data = array('card_id' => $card_id);
  223. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_DELETE . "access_token={$this->access_token}", Tools::json_encode($data));
  224. if ($result) {
  225. $json = json_decode($result, true);
  226. if (empty($json) || !empty($json['errcode'])) {
  227. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  228. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  229. return $this->checkRetry(__FUNCTION__, func_get_args());
  230. }
  231. return true;
  232. }
  233. return false;
  234. }
  235. /**
  236. * 获取粉丝下所有卡卷列表
  237. * @param $openid 粉丝openid
  238. * @param string $card_id 卡卷ID(可不给)
  239. * @return bool|array
  240. */
  241. public function getCardList($openid, $card_id = '')
  242. {
  243. if (!$this->access_token && !$this->getAccessToken()) {
  244. return false;
  245. }
  246. $data = array('openid' => $openid);
  247. !empty($card_id) && $data['card_id'] = $card_id;
  248. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_USER_GET_LIST . "access_token={$this->access_token}", Tools::json_encode($data));
  249. if ($result) {
  250. $json = json_decode($result, true);
  251. if (empty($json) || !empty($json['errcode'])) {
  252. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  253. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  254. return $this->checkRetry(__FUNCTION__, func_get_args());
  255. }
  256. return $json;
  257. }
  258. return false;
  259. }
  260. /**
  261. * 获取图文消息群发卡券HTML
  262. * @param string $card_id 卡卷ID
  263. * @return bool|array
  264. */
  265. public function getCardMpHtml($card_id)
  266. {
  267. if (!$this->access_token && !$this->getAccessToken()) {
  268. return false;
  269. }
  270. $data = array('card_id' => $card_id);
  271. !empty($card_id) && $data['card_id'] = $card_id;
  272. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_SEND_HTML . "access_token={$this->access_token}", Tools::json_encode($data));
  273. if ($result) {
  274. $json = json_decode($result, true);
  275. if (empty($json) || !empty($json['errcode'])) {
  276. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  277. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  278. return $this->checkRetry(__FUNCTION__, func_get_args());
  279. }
  280. return $json;
  281. }
  282. return false;
  283. }
  284. /**
  285. * 卡卷code核查
  286. * @param string $card_id 卡卷ID
  287. * @param array $code_list 卡卷code列表(一维数组)
  288. * @return bool|array
  289. */
  290. public function checkCardCodeList($card_id, $code_list)
  291. {
  292. if (!$this->access_token && !$this->getAccessToken()) {
  293. return false;
  294. }
  295. $data = array('card_id' => $card_id, 'code' => $code_list);
  296. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CHECKCODE . "access_token={$this->access_token}", Tools::json_encode($data));
  297. if ($result) {
  298. $json = json_decode($result, true);
  299. if (empty($json) || !empty($json['errcode'])) {
  300. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  301. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  302. return $this->checkRetry(__FUNCTION__, func_get_args());
  303. }
  304. return $json;
  305. }
  306. return false;
  307. }
  308. /**
  309. * 查询卡券详情
  310. * @param string $card_id 卡卷ID
  311. * @return bool|array
  312. */
  313. public function getCardInfo($card_id)
  314. {
  315. if (!$this->access_token && !$this->getAccessToken()) {
  316. return false;
  317. }
  318. $data = array('card_id' => $card_id);
  319. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_GET . "access_token={$this->access_token}", Tools::json_encode($data));
  320. if ($result) {
  321. $json = json_decode($result, true);
  322. if (empty($json) || !empty($json['errcode'])) {
  323. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  324. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  325. return $this->checkRetry(__FUNCTION__, func_get_args());
  326. }
  327. return $json;
  328. }
  329. return false;
  330. }
  331. /**
  332. * 获取颜色列表
  333. * 获得卡券的最新颜色列表,用于创建卡券
  334. * @return bool|array
  335. */
  336. public function getCardColors()
  337. {
  338. if (!$this->access_token && !$this->getAccessToken()) {
  339. return false;
  340. }
  341. $result = Tools::httpGet(self::API_BASE_URL_PREFIX . self::CARD_GETCOLORS . "access_token={$this->access_token}");
  342. if ($result) {
  343. $json = json_decode($result, true);
  344. if (empty($json) || !empty($json['errcode'])) {
  345. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  346. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  347. return $this->checkRetry(__FUNCTION__, func_get_args());
  348. }
  349. return $json;
  350. }
  351. return false;
  352. }
  353. /**
  354. * 生成卡券二维码
  355. * 成功则直接返回ticket值,可以用 getQRUrl($ticket) 换取二维码url
  356. * @param string $card_id 卡券ID 必须
  357. * @param string $code 指定卡券 code 码,只能被领一次。use_custom_code 字段为 true 的卡券必须填写,非自定义 code 不必填写。
  358. * @param string $openid 指定领取者的 openid,只有该用户能领取。bind_openid 字段为 true 的卡券必须填写,非自定义 openid 不必填写。
  359. * @param int $expire_seconds 指定二维码的有效时间,范围是 60 ~ 1800 秒。不填默认为永久有效。
  360. * @param bool $is_unique_code 指定下发二维码,生成的二维码随机分配一个 code,领取后不可再次扫描。填写 true 或 false。默认 false。
  361. * @param string $balance 红包余额,以分为单位。红包类型必填(LUCKY_MONEY),其他卡券类型不填。
  362. * @return bool|string
  363. */
  364. public function createCardQrcode($card_id, $code = '', $openid = '', $expire_seconds = 0, $is_unique_code = false, $balance = '')
  365. {
  366. if (!$this->access_token && !$this->getAccessToken()) {
  367. return false;
  368. }
  369. $card = array('card_id' => $card_id);
  370. !empty($code) && $card['code'] = $code;
  371. !empty($openid) && $card['openid'] = $openid;
  372. !empty($is_unique_code) && $card['is_unique_code'] = $is_unique_code;
  373. !empty($balance) && $card['balance'] = $balance;
  374. $data = array('action_name' => "QR_CARD");
  375. !empty($expire_seconds) && $data['expire_seconds'] = $expire_seconds;
  376. $data['action_info'] = array('card' => $card);
  377. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_QRCODE_CREATE . "access_token={$this->access_token}", Tools::json_encode($data));
  378. if ($result) {
  379. $json = json_decode($result, true);
  380. if (empty($json) || !empty($json['errcode'])) {
  381. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  382. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  383. return $this->checkRetry(__FUNCTION__, func_get_args());
  384. }
  385. return $json;
  386. }
  387. return false;
  388. }
  389. /**
  390. * 消耗 code
  391. * 自定义 code(use_custom_code 为 true)的优惠券,在 code 被核销时,必须调用此接口。
  392. * @param string $code 要消耗的序列号
  393. * @param string $card_id 要消耗序列号所述的 card_id,创建卡券时use_custom_code 填写 true 时必填。
  394. * @return bool|array
  395. * {
  396. * "errcode":0,
  397. * "errmsg":"ok",
  398. * "card":{"card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc"},
  399. * "openid":"oFS7Fjl0WsZ9AMZqrI80nbIq8xrA"
  400. * }
  401. */
  402. public function consumeCardCode($code, $card_id = '')
  403. {
  404. if (!$this->access_token && !$this->getAccessToken()) {
  405. return false;
  406. }
  407. $data = array('code' => $code);
  408. !empty($card_id) && $data['card_id'] = $card_id;
  409. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CODE_CONSUME . "access_token={$this->access_token}", Tools::json_encode($data));
  410. if ($result) {
  411. $json = json_decode($result, true);
  412. if (empty($json) || !empty($json['errcode'])) {
  413. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  414. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  415. return $this->checkRetry(__FUNCTION__, func_get_args());
  416. }
  417. return $json;
  418. }
  419. return false;
  420. }
  421. /**
  422. * code 解码
  423. * @param string $encrypt_code 通过 choose_card_info 获取的加密字符串
  424. * @return bool|array
  425. * {
  426. * "errcode":0,
  427. * "errmsg":"ok",
  428. * "code":"751234212312"
  429. * }
  430. */
  431. public function decryptCardCode($encrypt_code)
  432. {
  433. if (!$this->access_token && !$this->getAccessToken()) {
  434. return false;
  435. }
  436. $data = array('encrypt_code' => $encrypt_code,);
  437. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CODE_DECRYPT . "access_token={$this->access_token}", Tools::json_encode($data));
  438. if ($result) {
  439. $json = json_decode($result, true);
  440. if (empty($json) || !empty($json['errcode'])) {
  441. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  442. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  443. return $this->checkRetry(__FUNCTION__, func_get_args());
  444. }
  445. return $json;
  446. }
  447. return false;
  448. }
  449. /**
  450. * 查询 code 的有效性(非自定义 code)
  451. * @param string $code
  452. * @return bool|array
  453. * {
  454. * "errcode":0,
  455. * "errmsg":"ok",
  456. * "openid":"oFS7Fjl0WsZ9AMZqrI80nbIq8xrA", //用户 openid
  457. * "card":{
  458. * "card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc",
  459. * "begin_time": 1404205036, //起始使用时间
  460. * "end_time": 1404205036, //结束时间
  461. * }
  462. * }
  463. */
  464. public function checkCardCode($code)
  465. {
  466. if (!$this->access_token && !$this->getAccessToken()) {
  467. return false;
  468. }
  469. $data = array('code' => $code);
  470. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CODE_GET . "access_token={$this->access_token}", Tools::json_encode($data));
  471. if ($result) {
  472. $json = json_decode($result, true);
  473. if (empty($json) || !empty($json['errcode'])) {
  474. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  475. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  476. return $this->checkRetry(__FUNCTION__, func_get_args());
  477. }
  478. return $json;
  479. }
  480. return false;
  481. }
  482. /**
  483. * 批量查询卡列表
  484. * @param int $offset 开始拉取的偏移,默认为0从头开始
  485. * @param int $count 需要查询的卡片的数量(数量最大50,默认50)
  486. * @return bool|array
  487. * {
  488. * "errcode":0,
  489. * "errmsg":"ok",
  490. * "card_id_list":["ph_gmt7cUVrlRk8swPwx7aDyF-pg"], //卡 id 列表
  491. * "total_num":1 //该商户名下 card_id 总数
  492. * }
  493. */
  494. public function getCardIdList($offset = 0, $count = 50)
  495. {
  496. if (!$this->access_token && !$this->getAccessToken()) {
  497. return false;
  498. }
  499. $count > 50 && $count = 50;
  500. $data = array('offset' => $offset, 'count' => $count);
  501. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_BATCHGET . "access_token={$this->access_token}", Tools::json_encode($data));
  502. if ($result) {
  503. $json = json_decode($result, true);
  504. if (empty($json) || !empty($json['errcode'])) {
  505. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  506. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  507. return $this->checkRetry(__FUNCTION__, func_get_args());
  508. }
  509. return $json;
  510. }
  511. return false;
  512. }
  513. /**
  514. * 更改 code
  515. * 为确保转赠后的安全性,微信允许自定义code的商户对已下发的code进行更改。
  516. * 注:为避免用户疑惑,建议仅在发生转赠行为后(发生转赠后,微信会通过事件推送的方式告知商户被转赠的卡券code)对用户的code进行更改。
  517. * @param string $code 卡券的 code 编码
  518. * @param string $card_id 卡券 ID
  519. * @param string $new_code 新的卡券 code 编码
  520. * @return bool
  521. */
  522. public function updateCardCode($code, $card_id, $new_code)
  523. {
  524. if (!$this->access_token && !$this->getAccessToken()) {
  525. return false;
  526. }
  527. $data = array('code' => $code, 'card_id' => $card_id, 'new_code' => $new_code);
  528. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CODE_UPDATE . "access_token={$this->access_token}", Tools::json_encode($data));
  529. if ($result) {
  530. $json = json_decode($result, true);
  531. if (empty($json) || !empty($json['errcode'])) {
  532. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  533. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  534. return $this->checkRetry(__FUNCTION__, func_get_args());
  535. }
  536. return true;
  537. }
  538. return false;
  539. }
  540. /**
  541. * 设置卡券失效
  542. * 设置卡券失效的操作不可逆
  543. * @param string $code 需要设置为失效的 code
  544. * @param string $card_id 自定义 code 的卡券必填。非自定义 code 的卡券不填。
  545. * @return bool
  546. */
  547. public function unavailableCardCode($code, $card_id = '')
  548. {
  549. if (!$this->access_token && !$this->getAccessToken()) {
  550. return false;
  551. }
  552. $data = array('code' => $code);
  553. !empty($card_id) && $data['card_id'] = $card_id;
  554. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CODE_UNAVAILABLE . "access_token={$this->access_token}", Tools::json_encode($data));
  555. if ($result) {
  556. $json = json_decode($result, true);
  557. if (empty($json) || !empty($json['errcode'])) {
  558. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  559. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  560. return $this->checkRetry(__FUNCTION__, func_get_args());
  561. }
  562. return true;
  563. }
  564. return false;
  565. }
  566. /**
  567. * 库存修改
  568. * @param string $data
  569. * @return bool
  570. */
  571. public function modifyCardStock($data)
  572. {
  573. if (!$this->access_token && !$this->getAccessToken()) {
  574. return false;
  575. }
  576. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_MODIFY_STOCK . "access_token={$this->access_token}", Tools::json_encode($data));
  577. if ($result) {
  578. $json = json_decode($result, true);
  579. if (empty($json) || !empty($json['errcode'])) {
  580. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  581. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  582. return $this->checkRetry(__FUNCTION__, func_get_args());
  583. }
  584. return true;
  585. }
  586. return false;
  587. }
  588. /**
  589. * 更新门票
  590. * @param string $data
  591. * @return bool
  592. */
  593. public function updateMeetingCard($data)
  594. {
  595. if (!$this->access_token && !$this->getAccessToken()) {
  596. return false;
  597. }
  598. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_MEETINGCARD_UPDATEUSER . "access_token={$this->access_token}", Tools::json_encode($data));
  599. if ($result) {
  600. $json = json_decode($result, true);
  601. if (empty($json) || !empty($json['errcode'])) {
  602. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  603. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  604. return $this->checkRetry(__FUNCTION__, func_get_args());
  605. }
  606. return true;
  607. }
  608. return false;
  609. }
  610. /**
  611. * 激活/绑定会员卡
  612. * @param string $data 具体结构请参看卡券开发文档(6.1.1 激活/绑定会员卡)章节
  613. * @return bool
  614. */
  615. public function activateMemberCard($data)
  616. {
  617. if (!$this->access_token && !$this->getAccessToken()) {
  618. return false;
  619. }
  620. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_ACTIVATE . "access_token={$this->access_token}", Tools::json_encode($data));
  621. if ($result) {
  622. $json = json_decode($result, true);
  623. if (empty($json) || !empty($json['errcode'])) {
  624. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  625. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  626. return $this->checkRetry(__FUNCTION__, func_get_args());
  627. }
  628. return true;
  629. }
  630. return false;
  631. }
  632. /**
  633. * 会员卡交易
  634. * 会员卡交易后每次积分及余额变更需通过接口通知微信,便于后续消息通知及其他扩展功能。
  635. * @param string $data 具体结构请参看卡券开发文档(6.1.2 会员卡交易)章节
  636. * @return bool|array
  637. */
  638. public function updateMemberCard($data)
  639. {
  640. if (!$this->access_token && !$this->getAccessToken()) {
  641. return false;
  642. }
  643. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_UPDATEUSER . "access_token={$this->access_token}", Tools::json_encode($data));
  644. if ($result) {
  645. $json = json_decode($result, true);
  646. if (empty($json) || !empty($json['errcode'])) {
  647. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  648. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  649. return $this->checkRetry(__FUNCTION__, func_get_args());
  650. }
  651. return $json;
  652. }
  653. return false;
  654. }
  655. /**
  656. * 设置卡券测试白名单
  657. * @param array $openid 测试的 openid 列表
  658. * @param array $user 测试的微信号列表
  659. * @return bool
  660. */
  661. public function setCardTestWhiteList($openid = array(), $user = array())
  662. {
  663. if (!$this->access_token && !$this->getAccessToken()) {
  664. return false;
  665. }
  666. $data = array();
  667. count($openid) > 0 && $data['openid'] = $openid;
  668. count($user) > 0 && $data['username'] = $user;
  669. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_TESTWHILELIST_SET . "access_token={$this->access_token}", Tools::json_encode($data));
  670. if ($result) {
  671. $json = json_decode($result, true);
  672. if (empty($json) || !empty($json['errcode'])) {
  673. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  674. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  675. return $this->checkRetry(__FUNCTION__, func_get_args());
  676. }
  677. return true;
  678. }
  679. return false;
  680. }
  681. /**
  682. * 更新红包金额
  683. * @param string $code 红包的序列号
  684. * @param int $balance 红包余额
  685. * @param string $card_id 自定义 code 的卡券必填。非自定义 code 可不填。
  686. * @return bool|array
  687. */
  688. public function updateLuckyMoney($code, $balance, $card_id = '')
  689. {
  690. if (!$this->access_token && !$this->getAccessToken()) {
  691. return false;
  692. }
  693. $data = array('code' => $code, 'balance' => $balance);
  694. !empty($card_id) && $data['card_id'] = $card_id;
  695. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_LUCKYMONEY_UPDATE . "access_token={$this->access_token}", Tools::json_encode($data));
  696. if ($result) {
  697. $json = json_decode($result, true);
  698. if (empty($json) || !empty($json['errcode'])) {
  699. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  700. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  701. return $this->checkRetry(__FUNCTION__, func_get_args());
  702. }
  703. return true;
  704. }
  705. return false;
  706. }
  707. /**
  708. * 设置自助核销接口
  709. * @param string $card_id 卡券ID
  710. * @param bool $is_openid 是否开启自助核销功能,填true/false,默认为false
  711. * @param bool $need_verify_cod 用户核销时是否需要输入验证码,填true/false,默认为false
  712. * @param bool $need_remark_amount 用户核销时是否需要备注核销金额,填true/false,默认为false
  713. * @return bool|array
  714. */
  715. public function setSelfconsumecell($card_id, $is_openid = false, $need_verify_cod = false, $need_remark_amount = false)
  716. {
  717. if (!$this->access_token && !$this->getAccessToken()) {
  718. return false;
  719. }
  720. $data = array(
  721. 'card_id' => $card_id,
  722. 'is_open' => $is_openid,
  723. 'need_verify_cod' => $need_verify_cod,
  724. 'need_remark_amount' => $need_remark_amount,
  725. );
  726. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_SET_SELFCONSUMECELL . "access_token={$this->access_token}", Tools::json_encode($data));
  727. if ($result) {
  728. $json = json_decode($result, true);
  729. if (empty($json) || !empty($json['errcode'])) {
  730. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  731. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  732. return $this->checkRetry(__FUNCTION__, func_get_args());
  733. }
  734. return $json;
  735. }
  736. return false;
  737. }
  738. /**
  739. * 设置买单接口
  740. * @param string $card_id
  741. * @param bool $is_openid
  742. * @return bool|mixed
  743. */
  744. public function setPaycell($card_id, $is_openid = true)
  745. {
  746. if (!$this->access_token && !$this->getAccessToken()) {
  747. return false;
  748. }
  749. $data = array('card_id' => $card_id, 'is_open' => $is_openid,);
  750. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_PAYCELL_SET . "access_token={$this->access_token}", Tools::json_encode($data));
  751. if ($result) {
  752. $json = json_decode($result, true);
  753. if (empty($json) || !empty($json['errcode'])) {
  754. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  755. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  756. return $this->checkRetry(__FUNCTION__, func_get_args());
  757. }
  758. return $json;
  759. }
  760. return false;
  761. }
  762. /**
  763. * 设置开卡字段信息接口
  764. * @param array $data
  765. * @return bool|array
  766. */
  767. public function setMembercardActivateuserform($data)
  768. {
  769. if (!$this->access_token && !$this->getAccessToken()) {
  770. return false;
  771. }
  772. $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_ACTIVATEUSERFORM_SET . "access_token={$this->access_token}", Tools::json_encode($data));
  773. if ($result) {
  774. $json = json_decode($result, true);
  775. if (empty($json) || !empty($json['errcode'])) {
  776. $this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
  777. $this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
  778. return $this->checkRetry(__FUNCTION__, func_get_args());
  779. }
  780. return $json;
  781. }
  782. return false;
  783. }
  784. }