Skip to content

Commit 1e4dd17

Browse files
committed
卡券接口代码提出单独类中维护,并添加单元测试
1 parent 3d6778f commit 1e4dd17

File tree

7 files changed

+574
-425
lines changed

7 files changed

+574
-425
lines changed
Lines changed: 68 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package me.chanjar.weixin.common.bean;
22

3+
import org.apache.commons.lang3.builder.ToStringBuilder;
4+
import org.apache.commons.lang3.builder.ToStringStyle;
5+
36
import java.io.Serializable;
47

58
/**
@@ -10,95 +13,100 @@
1013
*/
1114
public class WxCardApiSignature implements Serializable {
1215

13-
private static final long serialVersionUID = 158176707226975979L;
16+
private static final long serialVersionUID = 158176707226975979L;
1417

15-
private String appId;
18+
private String appId;
1619

17-
private String cardId;
20+
private String cardId;
1821

19-
private String cardType;
22+
private String cardType;
2023

21-
private String locationId;
24+
private String locationId;
2225

23-
private String code;
26+
private String code;
2427

25-
private String openId;
28+
private String openId;
2629

27-
private Long timestamp;
30+
private Long timestamp;
2831

29-
private String nonceStr;
32+
private String nonceStr;
3033

31-
private String signature;
34+
private String signature;
3235

33-
public String getAppId() {
34-
return appId;
35-
}
36+
@Override
37+
public String toString() {
38+
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
39+
}
3640

37-
public void setAppId(String appId) {
38-
this.appId = appId;
39-
}
41+
public String getAppId() {
42+
return appId;
43+
}
44+
45+
public void setAppId(String appId) {
46+
this.appId = appId;
47+
}
4048

41-
public String getCardId() {
42-
return cardId;
43-
}
49+
public String getCardId() {
50+
return cardId;
51+
}
4452

45-
public void setCardId(String cardId) {
46-
this.cardId = cardId;
47-
}
53+
public void setCardId(String cardId) {
54+
this.cardId = cardId;
55+
}
4856

49-
public String getCardType() {
50-
return cardType;
51-
}
57+
public String getCardType() {
58+
return cardType;
59+
}
5260

53-
public void setCardType(String cardType) {
54-
this.cardType = cardType;
55-
}
61+
public void setCardType(String cardType) {
62+
this.cardType = cardType;
63+
}
5664

57-
public String getLocationId() {
58-
return locationId;
59-
}
65+
public String getLocationId() {
66+
return locationId;
67+
}
6068

6169
public void setLocationId(String locationId) {
6270
this.locationId = locationId;
6371
}
6472

6573
public String getCode() {
66-
return code;
67-
}
74+
return code;
75+
}
6876

69-
public void setCode(String code) {
70-
this.code = code;
71-
}
77+
public void setCode(String code) {
78+
this.code = code;
79+
}
7280

73-
public String getOpenId() {
74-
return openId;
75-
}
81+
public String getOpenId() {
82+
return openId;
83+
}
7684

77-
public void setOpenId(String openId) {
78-
this.openId = openId;
79-
}
85+
public void setOpenId(String openId) {
86+
this.openId = openId;
87+
}
8088

81-
public Long getTimestamp() {
82-
return timestamp;
83-
}
89+
public Long getTimestamp() {
90+
return timestamp;
91+
}
8492

85-
public void setTimestamp(Long timestamp) {
86-
this.timestamp = timestamp;
87-
}
93+
public void setTimestamp(Long timestamp) {
94+
this.timestamp = timestamp;
95+
}
8896

89-
public String getNonceStr() {
90-
return nonceStr;
91-
}
97+
public String getNonceStr() {
98+
return nonceStr;
99+
}
92100

93-
public void setNonceStr(String nonceStr) {
94-
this.nonceStr = nonceStr;
95-
}
101+
public void setNonceStr(String nonceStr) {
102+
this.nonceStr = nonceStr;
103+
}
96104

97-
public String getSignature() {
98-
return signature;
99-
}
105+
public String getSignature() {
106+
return signature;
107+
}
100108

101-
public void setSignature(String signature) {
102-
this.signature = signature;
103-
}
109+
public void setSignature(String signature) {
110+
this.signature = signature;
111+
}
104112
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package me.chanjar.weixin.mp.api;
2+
3+
import me.chanjar.weixin.common.bean.WxCardApiSignature;
4+
import me.chanjar.weixin.common.exception.WxErrorException;
5+
import me.chanjar.weixin.mp.bean.result.WxMpCardResult;
6+
7+
/**
8+
* 卡券相关接口
9+
* Created by Binary Wang on 2016/7/27.
10+
* @author binarywang(https://github.com/binarywang)
11+
*/
12+
public interface WxMpCardService {
13+
14+
/**
15+
* 获得卡券api_ticket,不强制刷新卡券api_ticket
16+
*
17+
* @return 卡券api_ticket
18+
* @throws WxErrorException
19+
* @see #getCardApiTicket(boolean)
20+
*/
21+
String getCardApiTicket() throws WxErrorException;
22+
23+
/**
24+
* <pre>
25+
* 获得卡券api_ticket
26+
* 获得时会检查卡券apiToken是否过期,如果过期了,那么就刷新一下,否则就什么都不干
27+
*
28+
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94.9F.E6.88.90.E7.AE.97.E6.B3.95
29+
* </pre>
30+
*
31+
* @param forceRefresh 强制刷新
32+
* @return 卡券api_ticket
33+
* @throws WxErrorException
34+
*/
35+
String getCardApiTicket(boolean forceRefresh) throws WxErrorException;
36+
37+
/**
38+
* <pre>
39+
* 创建调用卡券api时所需要的签名
40+
*
41+
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD
42+
* .954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94
43+
* .9F.E6.88.90.E7.AE.97.E6.B3.95
44+
* </pre>
45+
*
46+
* @param optionalSignParam 参与签名的参数数组。
47+
* 可以为下列字段:app_id, card_id, card_type, code, openid, location_id
48+
* </br>注意:当做wx.chooseCard调用时,必须传入app_id参与签名,否则会造成签名失败导致拉取卡券列表为空
49+
* @return 卡券Api签名对象
50+
*/
51+
WxCardApiSignature createCardApiSignature(String... optionalSignParam) throws
52+
WxErrorException;
53+
54+
/**
55+
* 卡券Code解码
56+
*
57+
* @param encryptCode 加密Code,通过JSSDK的chooseCard接口获得
58+
* @return 解密后的Code
59+
*/
60+
String decryptCardCode(String encryptCode) throws WxErrorException;
61+
62+
/**
63+
* 卡券Code查询
64+
*
65+
* @param cardId 卡券ID代表一类卡券
66+
* @param code 单张卡券的唯一标准
67+
* @param checkConsume 是否校验code核销状态,填入true和false时的code异常状态返回数据不同
68+
* @return WxMpCardResult对象
69+
*/
70+
WxMpCardResult queryCardCode(String cardId, String code, boolean checkConsume)
71+
throws WxErrorException;
72+
73+
/**
74+
* 卡券Code核销。核销失败会抛出异常
75+
*
76+
* @param code 单张卡券的唯一标准
77+
* @return 调用返回的JSON字符串。
78+
* <br>可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。
79+
*/
80+
String consumeCardCode(String code) throws WxErrorException;
81+
82+
/**
83+
* 卡券Code核销。核销失败会抛出异常
84+
*
85+
* @param code 单张卡券的唯一标准
86+
* @param cardId 当自定义Code卡券时需要传入card_id
87+
* @return 调用返回的JSON字符串。
88+
* <br>可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。
89+
*/
90+
String consumeCardCode(String code, String cardId) throws WxErrorException;
91+
92+
/**
93+
* 卡券Mark接口。
94+
* 开发者在帮助消费者核销卡券之前,必须帮助先将此code(卡券串码)与一个openid绑定(即mark住),
95+
* 才能进一步调用核销接口,否则报错。
96+
*
97+
* @param code 卡券的code码
98+
* @param cardId 卡券的ID
99+
* @param openId 用券用户的openid
100+
* @param isMark 是否要mark(占用)这个code,填写true或者false,表示占用或解除占用
101+
*/
102+
void markCardCode(String code, String cardId, String openId, boolean isMark) throws
103+
WxErrorException;
104+
105+
/**
106+
* 查看卡券详情接口
107+
* 详见 https://mp.weixin.qq.com/wiki/14/8dd77aeaee85f922db5f8aa6386d385e.html#.E6.9F.A5.E7.9C.8B.E5.8D.A1.E5.88.B8.E8.AF.A6.E6.83.85
108+
*
109+
* @param cardId 卡券的ID
110+
* @return 返回的卡券详情JSON字符串
111+
* <br> [注] 由于返回的JSON格式过于复杂,难以定义其对应格式的Bean并且难以维护,因此只返回String格式的JSON串。
112+
* <br> 可由 com.google.gson.JsonParser#parse 等方法直接取JSON串中的某个字段。
113+
*/
114+
String getCardDetail(String cardId) throws WxErrorException;
115+
}

0 commit comments

Comments
 (0)