1 package com.foxinmy.weixin4j.mp.api;
2
3 import java.io.IOException;
4 import java.util.List;
5
6 import com.alibaba.fastjson.JSON;
7 import com.alibaba.fastjson.JSONArray;
8 import com.alibaba.fastjson.JSONObject;
9 import com.alibaba.fastjson.TypeReference;
10 import com.foxinmy.weixin4j.exception.WeixinException;
11 import com.foxinmy.weixin4j.http.weixin.ApiResult;
12 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
13 import com.foxinmy.weixin4j.model.Token;
14 import com.foxinmy.weixin4j.model.card.*;
15 import com.foxinmy.weixin4j.model.qr.QRParameter;
16 import com.foxinmy.weixin4j.model.qr.QRResult;
17 import com.foxinmy.weixin4j.token.TokenManager;
18 import com.foxinmy.weixin4j.type.card.CardStatus;
19 import com.foxinmy.weixin4j.type.card.CardType;
20 import com.foxinmy.weixin4j.util.IOUtil;
21
22
23
24
25
26
27
28
29
30
31
32 public class CardApi extends MpApi {
33 protected final TokenManager tokenManager;
34
35 public CardApi(TokenManager tokenManager) {
36 this.tokenManager = tokenManager;
37 }
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public String createCardCoupon(CardCoupon cardCoupon)
59 throws WeixinException {
60 JSONObject content = new JSONObject();
61 String cardType = cardCoupon.getCardType().name();
62 content.put("card_type", cardType);
63 content.put(cardType.toLowerCase(), cardCoupon);
64 JSONObject card = new JSONObject();
65 card.put("card", content);
66 Token token = tokenManager.getCache();
67 String card_create_uri = getRequestUri("card_create_uri");
68 WeixinResponse response = weixinExecutor.post(
69 String.format(card_create_uri, token.getAccessToken()),
70 card.toJSONString());
71 return response.getAsJson().getString("card_id");
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public ApiResult setCardPayCell(String cardId, boolean isOpen)
87 throws WeixinException {
88 JSONObject params = new JSONObject();
89 params.put("card_id", cardId);
90 params.put("is_open", isOpen);
91 Token token = tokenManager.getCache();
92 String card_paycell_uri = getRequestUri("card_paycell_uri");
93 WeixinResponse response = weixinExecutor.post(
94 String.format(card_paycell_uri, token.getAccessToken()),
95 params.toJSONString());
96 return response.getAsResult();
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111 public ApiResult setCardSelfConsumeCell(String cardId, boolean isOpen)
112 throws WeixinException {
113 JSONObject params = new JSONObject();
114 params.put("card_id", cardId);
115 params.put("is_open", isOpen);
116 Token token = tokenManager.getCache();
117 String card_selfconsumecell_uri = getRequestUri("card_selfconsumecell_uri");
118 WeixinResponse response = weixinExecutor
119 .post(String.format(card_selfconsumecell_uri,
120 token.getAccessToken()), params.toJSONString());
121 return response.getAsResult();
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 public QRResult createCardQR(Integer expireSeconds, CardQR... cardQRs)
139 throws WeixinException {
140 QRParameter parameter = QRParameter.createCardCouponQR(expireSeconds,
141 cardQRs);
142 Token token = tokenManager.getCache();
143 String qr_uri = getRequestUri("card_qr_ticket_uri");
144 WeixinResponse response = weixinExecutor.post(
145 String.format(qr_uri, token.getAccessToken()),
146 JSON.toJSONString(parameter));
147 QRResult result = response.getAsObject(new TypeReference<QRResult>() {
148 });
149 qr_uri = String.format(getRequestUri("qr_image_uri"),
150 result.getTicket());
151 response = weixinExecutor.get(qr_uri);
152 result.setShowUrl(qr_uri);
153 try {
154 result.setContent(IOUtil.toByteArray(response.getBody()));
155 } catch (IOException e) {
156 throw new WeixinException(e);
157 }
158 return result;
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177 public ApiResult setTestWhiteList(List<String> openIds,
178 List<String> userNames) throws WeixinException {
179 JSONObject requestObj = new JSONObject();
180 if (openIds != null && openIds.size() > 0) {
181 requestObj.put("openid", openIds);
182 }
183 if (userNames != null && userNames.size() > 0) {
184 requestObj.put("username", userNames);
185 }
186 String card_set_test_whitelist_uri = getRequestUri("card_set_test_whitelist_uri");
187 Token token = tokenManager.getCache();
188 WeixinResponse response = weixinExecutor.post(
189 String.format(card_set_test_whitelist_uri,
190 token.getAccessToken()), requestObj.toJSONString());
191 return response.getAsResult();
192 }
193
194
195
196
197
198
199
200
201
202
203
204 public CardStatus queryCardStatus(String cardId) throws WeixinException {
205 JSONObject requestObj = new JSONObject();
206 requestObj.put("card_id", cardId);
207 String card_get_uri = getRequestUri("card_get_uri");
208 Token token = tokenManager.getCache();
209 WeixinResponse response = weixinExecutor.post(
210 String.format(card_get_uri, token.getAccessToken()),
211 requestObj.toJSONString());
212 JSONObject responseAsJson = response.getAsJson();
213 JSONObject card = responseAsJson.getJSONObject("card");
214 String cardType = card.getString("card_type");
215 JSONObject baseInfo = card.getJSONObject(cardType.toLowerCase())
216 .getJSONObject("base_info");
217 String status = baseInfo.getString("status");
218 return CardStatus.valueOf(status);
219 }
220
221
222
223
224
225
226
227
228 public JSONObject getCardInfo(String cardId) throws WeixinException {
229 JSONObject requestObj = new JSONObject();
230 requestObj.put("card_id", cardId);
231 String card_get_uri = getRequestUri("card_get_uri");
232 Token token = tokenManager.getCache();
233 WeixinResponse response = weixinExecutor.post(
234 String.format(card_get_uri, token.getAccessToken()),
235 requestObj.toJSONString());
236 JSONObject responseJson = response.getAsJson();
237 return responseJson.getJSONObject("card");
238 }
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 public Boolean updateCardCoupon(String cardId, CardCoupon card)
255 throws WeixinException {
256 JSONObject request = new JSONObject();
257 request.put("card_id", cardId);
258 CardType cardType = card.getCardType();
259 card.cleanCantUpdateField();
260 request.put(cardType.name().toLowerCase(), card);
261 String card_update_uri = getRequestUri("card_update_uri");
262 Token token = tokenManager.getCache();
263 WeixinResponse response = weixinExecutor.post(
264 String.format(card_update_uri, token.getAccessToken()),
265 JSON.toJSONString(request));
266 JSONObject jsonObject = response.getAsJson();
267 return jsonObject.getBoolean("send_check");
268 }
269
270
271
272
273
274
275
276
277
278
279 public ApiResult activateMemberCard(MemberInitInfo memberInitInfo)
280 throws WeixinException {
281 String card_member_card_activate_uri = getRequestUri("card_member_card_activate_uri");
282 Token token = tokenManager.getCache();
283 WeixinResponse response = weixinExecutor.post(
284 String.format(card_member_card_activate_uri,
285 token.getAccessToken()),
286 JSON.toJSONString(memberInitInfo));
287 return response.getAsResult();
288 }
289
290
291
292
293
294
295
296
297 public ApiResult setActivateUserForm(MemberUserForm memberUserForm)
298 throws WeixinException {
299 String user_form_uri = getRequestUri("card_member_card_activate_user_form_uri");
300 Token token = tokenManager.getCache();
301 WeixinResponse response = weixinExecutor.post(
302 String.format(user_form_uri, token.getAccessToken()),
303 JSON.toJSONString(memberUserForm));
304 return response.getAsResult();
305 }
306
307
308
309
310
311
312
313
314
315
316
317 public MemberUserInfo getMemberUserInfo(String cardId, String code)
318 throws WeixinException {
319 String user_info_uri = getRequestUri("card_member_card_user_info_uri");
320 Token token = tokenManager.getCache();
321 JSONObject jsonObject = new JSONObject();
322 jsonObject.put("card_id", cardId);
323 jsonObject.put("code", code);
324 WeixinResponse response = weixinExecutor.post(
325 String.format(user_info_uri, token.getAccessToken()),
326 JSON.toJSONString(jsonObject));
327 return response.getAsObject(new TypeReference<MemberUserInfo>() {
328 });
329 }
330
331
332
333
334
335
336
337
338 public JSONObject updateMemberUserInfo(MemberUpdateInfo updateInfo)
339 throws WeixinException {
340 String card_member_card_update_user_uri = getRequestUri("card_member_card_update_user_uri");
341 Token token = tokenManager.getCache();
342 WeixinResponse response = weixinExecutor.post(
343 String.format(card_member_card_update_user_uri,
344 token.getAccessToken()), JSON.toJSONString(updateInfo));
345 return response.getAsJson();
346 }
347
348
349
350
351
352
353
354
355 public String addGiftCardPage(GiftCardPage page) throws WeixinException {
356 String card_gift_card_page_add = getRequestUri("card_gift_card_page_add_uri");
357 JSONObject pageJson = new JSONObject();
358 pageJson.put("page", page);
359 Token token = tokenManager.getCache();
360 WeixinResponse response = weixinExecutor.post(
361 String.format(card_gift_card_page_add,
362 token.getAccessToken()), JSON.toJSONString(pageJson));
363 JSONObject jsonObject = response.getAsJson();
364 return jsonObject.getString("page_id");
365 }
366
367
368
369
370
371
372
373
374
375 public JSONObject getGiftCardPage(String pageId) throws WeixinException {
376 String card_gift_card_page_get = getRequestUri("card_gift_card_page_get_uri");
377 JSONObject param = new JSONObject();
378 param.put("page_id", pageId);
379 Token token = tokenManager.getCache();
380 WeixinResponse response = weixinExecutor.post(
381 String.format(card_gift_card_page_get,
382 token.getAccessToken()), JSON.toJSONString(param));
383 JSONObject jsonObject = response.getAsJson();
384
385 return jsonObject.getJSONObject("page");
386 }
387
388
389
390
391
392
393
394 public String[] getGiftCardPageIdList() throws WeixinException {
395 String card_gift_card_page_batchget = getRequestUri("card_gift_card_page_batchget_uri");
396 JSONObject param = new JSONObject();
397 Token token = tokenManager.getCache();
398 WeixinResponse response = weixinExecutor.post(
399 String.format(card_gift_card_page_batchget,
400 token.getAccessToken()), JSON.toJSONString(param));
401 JSONObject jsonObject = response.getAsJson();
402 JSONArray idList = jsonObject.getJSONArray("page_id_list");
403 if(idList==null || idList.size()==0){
404 return new String[0];
405 }
406
407 return idList.toArray(new String[idList.size()]);
408 }
409
410
411
412
413
414
415
416
417
418 public ApiResult maintainGiftCardPage(String pageId) throws WeixinException {
419 String card_gift_card_maintain_set = getRequestUri("card_gift_card_maintain_set_uri");
420 JSONObject param = new JSONObject();
421 param.put("page_id", pageId);
422 param.put("maintain", true);
423 Token token = tokenManager.getCache();
424 WeixinResponse response = weixinExecutor.post(
425 String.format(card_gift_card_maintain_set,
426 token.getAccessToken()), JSON.toJSONString(param));
427 return response.getAsResult();
428 }
429
430
431
432
433
434
435
436 public ApiResult maintainAllGiftCardPage() throws WeixinException {
437 String card_gift_card_maintain_set = getRequestUri("card_gift_card_maintain_set_uri");
438 JSONObject param = new JSONObject();
439 param.put("all", true);
440 param.put("maintain", true);
441 Token token = tokenManager.getCache();
442 WeixinResponse response = weixinExecutor.post(
443 String.format(card_gift_card_maintain_set,
444 token.getAccessToken()), JSON.toJSONString(param));
445 return response.getAsResult();
446 }
447
448
449
450
451
452
453
454
455
456 public JSONObject getOrderInfo(String orderId) throws WeixinException {
457 String card_gift_card_order_get = getRequestUri("card_gift_card_order_get_uri");
458 JSONObject param = new JSONObject();
459 param.put("order_id", orderId);
460 Token token = tokenManager.getCache();
461 WeixinResponse response = weixinExecutor.post(
462 String.format(card_gift_card_order_get,
463 token.getAccessToken()), JSON.toJSONString(param));
464
465 return response.getAsJson();
466 }
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484 public JSONObject getOrders(long beginTime, long endTime, String sortType, int offset, int limit) throws WeixinException {
485 String card_gift_card_order_batchget_uri = getRequestUri("card_gift_card_order_batchget_uri");
486 JSONObject param = new JSONObject();
487 param.put("begin_time", beginTime);
488 param.put("end_time", endTime);
489 param.put("sort_type", sortType);
490 param.put("offset", offset);
491 param.put("count", limit);
492 Token token = tokenManager.getCache();
493 WeixinResponse response = weixinExecutor.post(
494 String.format(card_gift_card_order_batchget_uri,
495 token.getAccessToken()), JSON.toJSONString(param));
496
497 return response.getAsJson();
498 }
499
500
501
502
503
504
505
506
507 public ApiResult updateGiftCardPage(GiftCardPage page) throws WeixinException {
508 String card_gift_card_page_update_uri = getRequestUri("card_gift_card_page_update_uri");
509 JSONObject pageJson = new JSONObject();
510 pageJson.put("page", page);
511 Token token = tokenManager.getCache();
512 WeixinResponse response = weixinExecutor.post(
513 String.format(card_gift_card_page_update_uri,
514 token.getAccessToken()), JSON.toJSONString(pageJson));
515 return response.getAsResult();
516 }
517
518
519
520
521
522
523
524
525
526
527 public String addGiftCardPayWhitelist(String subMchId) throws WeixinException{
528 String card_gift_card_pay_whitelist_add = getRequestUri("card_gift_card_pay_whitelist_add_uri");
529 JSONObject param = new JSONObject();
530 param.put("sub_mch_id", subMchId);
531 Token token = tokenManager.getCache();
532 WeixinResponse response = weixinExecutor.post(
533 String.format(card_gift_card_pay_whitelist_add,
534 token.getAccessToken()), JSON.toJSONString(param));
535 JSONObject jsonObject = response.getAsJson();
536 return jsonObject.getString("url");
537 }
538
539
540
541
542
543
544
545
546
547
548
549 public ApiResult bindGiftCardPaySubMch(String wxaAppid, String subMchId) throws WeixinException {
550 String card_gift_card_pay_submch_bind = getRequestUri("card_gift_card_pay_submch_bind_uri");
551 JSONObject param = new JSONObject();
552 param.put("sub_mch_id", subMchId);
553 param.put("wxa_appid", wxaAppid);
554
555 Token token = tokenManager.getCache();
556 WeixinResponse response = weixinExecutor.post(
557 String.format(card_gift_card_pay_submch_bind,
558 token.getAccessToken()), JSON.toJSONString(param));
559
560 return response.getAsResult();
561 }
562
563
564
565
566
567
568
569
570
571
572
573
574 public ApiResult setGiftCardWxaCode(String wxaAppid, String pageId) throws WeixinException {
575 String card_gift_card_wxa_set = getRequestUri("card_gift_card_wxa_set_uri");
576 JSONObject param = new JSONObject();
577 param.put("wxa_appid", wxaAppid);
578 param.put("page_id", pageId);
579
580 Token token = tokenManager.getCache();
581 WeixinResponse response = weixinExecutor.post(
582 String.format(card_gift_card_wxa_set,
583 token.getAccessToken()), JSON.toJSONString(param));
584
585 return response.getAsResult();
586 }
587
588
589
590
591
592
593
594
595
596 public JSONObject updateGiftCardUserBalance(CardInfo cardInfo) throws WeixinException {
597 String card_gift_card_wxa_set = getRequestUri("card_general_card_update_user_uri");
598
599 Token token = tokenManager.getCache();
600 WeixinResponse response = weixinExecutor.post(
601 String.format(card_gift_card_wxa_set,
602 token.getAccessToken()), JSON.toJSONString(cardInfo));
603 return response.getAsJson();
604 }
605
606
607
608
609
610
611
612
613
614
615
616 public ApiResult consumeGiftCard(String code, String cardId) throws WeixinException {
617 String card_code_consume = getRequestUri("card_code_consume_uri");
618 JSONObject param = new JSONObject();
619 param.put("code", code);
620 if(cardId!=null && cardId.length()>0){
621 param.put("card_id", cardId);
622 }
623
624 Token token = tokenManager.getCache();
625 WeixinResponse response = weixinExecutor.post(
626 String.format(card_code_consume,
627 token.getAccessToken()), JSON.toJSONString(param));
628
629 return response.getAsResult();
630 }
631
632
633
634
635
636
637
638
639
640
641
642
643 public JSONObject getGiftCardInfo(String code, String cardId) throws WeixinException {
644 String card_code_get = getRequestUri("card_code_get_uri");
645 JSONObject param = new JSONObject();
646 param.put("code", code);
647 if(cardId!=null && cardId.length()>0){
648 param.put("card_id", cardId);
649 }
650
651 Token token = tokenManager.getCache();
652 WeixinResponse response = weixinExecutor.post(
653 String.format(card_code_get,
654 token.getAccessToken()), JSON.toJSONString(param));
655
656 return response.getAsJson();
657 }
658
659
660
661
662
663
664
665
666
667 public ApiResult orderRefund(String orderId) throws WeixinException {
668 String card_gift_card_order_refund_uri = getRequestUri("card_gift_card_order_refund_uri");
669 JSONObject param = new JSONObject();
670 param.put("order_id", orderId);
671
672 Token token = tokenManager.getCache();
673 WeixinResponse response = weixinExecutor.post(
674 String.format(card_gift_card_order_refund_uri,
675 token.getAccessToken()), JSON.toJSONString(param));
676
677 return response.getAsResult();
678 }
679 }