parameters) throws WxErrorException;
+
/**
*
* 计算Map键值对是否和签名相符,
@@ -770,7 +787,7 @@ public interface WxMpService {
* @return
*/
public boolean checkJSSDKCallbackDataSignature(Map kvm, String signature);
-
+
/**
* 发送微信红包给个人用户
* @param parameters
@@ -778,4 +795,95 @@ public interface WxMpService {
* @throws WxErrorException
*/
public WxRedpackResult sendRedpack(Map parameters) throws WxErrorException;
+
+ /**
+ * 获得卡券api_ticket,不强制刷新卡券api_ticket
+ * @see #getCardApiTicket(boolean)
+ * @return 卡券api_ticket
+ * @throws WxErrorException
+ */
+ public String getCardApiTicket() throws WxErrorException;
+
+ /**
+ *
+ * 获得卡券api_ticket
+ * 获得时会检查卡券apiToken是否过期,如果过期了,那么就刷新一下,否则就什么都不干
+ *
+ * 详情请见: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
+ *
+ * @param forceRefresh 强制刷新
+ * @return 卡券api_ticket
+ * @throws WxErrorException
+ */
+ public String getCardApiTicket(boolean forceRefresh) throws WxErrorException;
+
+ /**
+ *
+ * 创建调用卡券api时所需要的签名
+ *
+ * 详情请见: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
+ *
+ *
+ * @param optionalSignParam 参与签名的参数数组。
+ * 可以为下列字段:app_id, card_id, card_type, code, openid, location_id
+ * 注意:当做wx.chooseCard调用时,必须传入app_id参与签名,否则会造成签名失败导致拉取卡券列表为空
+ * @return 卡券Api签名对象
+ */
+ public WxCardApiSignature createCardApiSignature(String... optionalSignParam) throws
+ WxErrorException;
+
+ /**
+ * 卡券Code解码
+ * @param encryptCode 加密Code,通过JSSDK的chooseCard接口获得
+ * @return 解密后的Code
+ * @throws WxErrorException
+ */
+ public String decryptCardCode(String encryptCode) throws WxErrorException;
+
+ /**
+ * 卡券Code查询
+ * @param cardId 卡券ID代表一类卡券
+ * @param code 单张卡券的唯一标准
+ * @param checkConsume 是否校验code核销状态,填入true和false时的code异常状态返回数据不同
+ * @return WxMpCardResult对象
+ * @throws WxErrorException
+ */
+ public WxMpCardResult queryCardCode(String cardId, String code, boolean checkConsume)
+ throws WxErrorException;
+
+ /**
+ * 卡券Code核销。核销失败会抛出异常
+ * @param code 单张卡券的唯一标准
+ * @param cardId 当自定义Code卡券时需要传入card_id
+ * @return 调用返回的JSON字符串。
+ *
可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。
+ * @throws WxErrorException
+ */
+ public String consumeCardCode(String code, String cardId) throws WxErrorException;
+
+ /**
+ * 卡券Mark接口。
+ * 开发者在帮助消费者核销卡券之前,必须帮助先将此code(卡券串码)与一个openid绑定(即mark住),
+ * 才能进一步调用核销接口,否则报错。
+ * @param code 卡券的code码
+ * @param cardId 卡券的ID
+ * @param openId 用券用户的openid
+ * @param isMark 是否要mark(占用)这个code,填写true或者false,表示占用或解除占用
+ * @throws WxErrorException
+ */
+ public void markCardCode(String code, String cardId, String openId, boolean isMark) throws
+ WxErrorException;
+
+ /**
+ * 查看卡券详情接口
+ * 详见 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
+ * @param cardId 卡券的ID
+ * @return 返回的卡券详情JSON字符串
+ *
[注] 由于返回的JSON格式过于复杂,难以定义其对应格式的Bean并且难以维护,因此只返回String格式的JSON串。
+ *
可由 com.google.gson.JsonParser#parse 等方法直接取JSON串中的某个字段。
+ * @throws WxErrorException
+ */
+ public String getCardDetail(String cardId) throws WxErrorException;
}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java
index 4223caef3..99bb84637 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java
@@ -5,6 +5,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@@ -14,8 +15,10 @@ import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;
+import com.google.gson.JsonPrimitive;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxAccessToken;
+import me.chanjar.weixin.common.bean.WxCardApiSignature;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.result.WxError;
@@ -28,13 +31,7 @@ import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
import me.chanjar.weixin.common.util.fs.FileUtils;
-import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
-import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
-import me.chanjar.weixin.common.util.http.RequestExecutor;
-import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
-import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
-import me.chanjar.weixin.common.util.http.URIUtil;
-import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
+import me.chanjar.weixin.common.util.http.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
@@ -49,6 +46,7 @@ import me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate;
import me.chanjar.weixin.mp.bean.WxMpMaterialNews;
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
+import me.chanjar.weixin.mp.bean.result.WxMpCardResult;
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
import me.chanjar.weixin.mp.bean.result.WxMpMaterialCountResult;
@@ -58,6 +56,7 @@ import me.chanjar.weixin.mp.bean.result.WxMpMaterialUploadResult;
import me.chanjar.weixin.mp.bean.result.WxMpMaterialVideoInfoResult;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpPayCallback;
+import me.chanjar.weixin.mp.bean.result.WxMpPayRefundResult;
import me.chanjar.weixin.mp.bean.result.WxMpPayResult;
import me.chanjar.weixin.mp.bean.result.WxMpPrepayIdResult;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
@@ -99,6 +98,7 @@ import org.slf4j.helpers.MessageFormatter;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
@@ -118,6 +118,11 @@ public class WxMpServiceImpl implements WxMpService {
*/
protected final Object globalJsapiTicketRefreshLock = new Object();
+ /**
+ * 全局的是否正在刷新卡券api_ticket的锁
+ */
+ protected final Object globalCardApiTicketRefreshLock = new Object();
+
protected WxMpConfigStorage wxMpConfigStorage;
protected CloseableHttpClient httpClient;
@@ -158,14 +163,15 @@ public class WxMpServiceImpl implements WxMpService {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpGet.setConfig(config);
}
- CloseableHttpResponse response = getHttpclient().execute(httpGet);
- String resultContent = new BasicResponseHandler().handleResponse(response);
- WxError error = WxError.fromJson(resultContent);
- if (error.getErrorCode() != 0) {
- throw new WxErrorException(error);
+ try (CloseableHttpResponse response = getHttpclient().execute(httpGet)) {
+ String resultContent = new BasicResponseHandler().handleResponse(response);
+ WxError error = WxError.fromJson(resultContent);
+ if (error.getErrorCode() != 0) {
+ throw new WxErrorException(error);
+ }
+ WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
+ wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}
- WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
- wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
} catch (ClientProtocolException e) {
throw new RuntimeException(e);
} catch (IOException e) {
@@ -189,7 +195,7 @@ public class WxMpServiceImpl implements WxMpService {
synchronized (globalJsapiTicketRefreshLock) {
if (wxMpConfigStorage.isJsapiTicketExpired()) {
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
- String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
+ String responseContent = execute(new JoddGetRequestExecutor(), url, null);
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
@@ -226,33 +232,33 @@ public class WxMpServiceImpl implements WxMpService {
public void customMessageSend(WxMpCustomMessage message) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
- execute(new SimplePostRequestExecutor(), url, message.toJson());
+ execute(new JoddPostRequestExecutor(), url, message.toJson());
}
public void menuCreate(WxMenu menu) throws WxErrorException {
if (menu.getMatchRule() != null) {
String url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional";
- execute(new SimplePostRequestExecutor(), url, menu.toJson());
+ execute(new JoddPostRequestExecutor(), url, menu.toJson());
} else {
String url = "https://api.weixin.qq.com/cgi-bin/menu/create";
- execute(new SimplePostRequestExecutor(), url, menu.toJson());
+ execute(new JoddPostRequestExecutor(), url, menu.toJson());
}
}
public void menuDelete() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/delete";
- execute(new SimpleGetRequestExecutor(), url, null);
+ execute(new JoddGetRequestExecutor(), url, null);
}
public void menuDelete(String menuid) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional";
- execute(new SimpleGetRequestExecutor(), url, "menuid=" + menuid);
+ execute(new JoddGetRequestExecutor(), url, "menuid=" + menuid);
}
public WxMenu menuGet() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/get";
try {
- String resultContent = execute(new SimpleGetRequestExecutor(), url, null);
+ String resultContent = execute(new JoddGetRequestExecutor(), url, null);
return WxMenu.fromJson(resultContent);
} catch (WxErrorException e) {
// 46003 不存在的菜单数据
@@ -266,7 +272,7 @@ public class WxMpServiceImpl implements WxMpService {
public WxMenu menuTryMatch(String userid) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch";
try {
- String resultContent = execute(new SimpleGetRequestExecutor(), url, "user_id=" + userid);
+ String resultContent = execute(new JoddGetRequestExecutor(), url, "user_id=" + userid);
return WxMenu.fromJson(resultContent);
} catch (WxErrorException e) {
// 46003 不存在的菜单数据 46002 不存在的菜单版本
@@ -379,25 +385,25 @@ public class WxMpServiceImpl implements WxMpService {
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";
- String responseContent = execute(new SimplePostRequestExecutor(), url, news.toJson());
+ String responseContent = execute(new JoddPostRequestExecutor(), url, news.toJson());
return WxMpMassUploadResult.fromJson(responseContent);
}
public WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException {
String url = "http://file.api.weixin.qq.com/cgi-bin/media/uploadvideo";
- String responseContent = execute(new SimplePostRequestExecutor(), url, video.toJson());
+ String responseContent = execute(new JoddPostRequestExecutor(), url, video.toJson());
return WxMpMassUploadResult.fromJson(responseContent);
}
public WxMpMassSendResult massGroupMessageSend(WxMpMassGroupMessage message) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall";
- String responseContent = execute(new SimplePostRequestExecutor(), url, message.toJson());
+ String responseContent = execute(new JoddPostRequestExecutor(), url, message.toJson());
return WxMpMassSendResult.fromJson(responseContent);
}
public WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/send";
- String responseContent = execute(new SimplePostRequestExecutor(), url, message.toJson());
+ String responseContent = execute(new JoddPostRequestExecutor(), url, message.toJson());
return WxMpMassSendResult.fromJson(responseContent);
}
@@ -409,7 +415,7 @@ public class WxMpServiceImpl implements WxMpService {
groupJson.addProperty("name", name);
String responseContent = execute(
- new SimplePostRequestExecutor(),
+ new JoddPostRequestExecutor(),
url,
json.toString());
return WxMpGroup.fromJson(responseContent);
@@ -417,7 +423,7 @@ public class WxMpServiceImpl implements WxMpService {
public List groupGet() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/groups/get";
- String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
+ String responseContent = execute(new JoddGetRequestExecutor(), url, null);
/*
* 操蛋的微信API,创建时返回的是 { group : { id : ..., name : ...} }
* 查询时返回的是 { groups : [ { id : ..., name : ..., count : ... }, ... ] }
@@ -432,14 +438,14 @@ public class WxMpServiceImpl implements WxMpService {
String url = "https://api.weixin.qq.com/cgi-bin/groups/getid";
JsonObject o = new JsonObject();
o.addProperty("openid", openid);
- String responseContent = execute(new SimplePostRequestExecutor(), url, o.toString());
+ String responseContent = execute(new JoddPostRequestExecutor(), url, o.toString());
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("groupid"));
}
public void groupUpdate(WxMpGroup group) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/groups/update";
- execute(new SimplePostRequestExecutor(), url, group.toJson());
+ execute(new JoddPostRequestExecutor(), url, group.toJson());
}
public void userUpdateGroup(String openid, long to_groupid) throws WxErrorException {
@@ -447,7 +453,7 @@ public class WxMpServiceImpl implements WxMpService {
JsonObject json = new JsonObject();
json.addProperty("openid", openid);
json.addProperty("to_groupid", to_groupid);
- execute(new SimplePostRequestExecutor(), url, json.toString());
+ execute(new JoddPostRequestExecutor(), url, json.toString());
}
public void userUpdateRemark(String openid, String remark) throws WxErrorException {
@@ -455,19 +461,19 @@ public class WxMpServiceImpl implements WxMpService {
JsonObject json = new JsonObject();
json.addProperty("openid", openid);
json.addProperty("remark", remark);
- execute(new SimplePostRequestExecutor(), url, json.toString());
+ execute(new JoddPostRequestExecutor(), url, json.toString());
}
public WxMpUser userInfo(String openid, String lang) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/user/info";
lang = lang == null ? "zh_CN" : lang;
- String responseContent = execute(new SimpleGetRequestExecutor(), url, "openid=" + openid + "&lang=" + lang);
+ String responseContent = execute(new JoddGetRequestExecutor(), url, "openid=" + openid + "&lang=" + lang);
return WxMpUser.fromJson(responseContent);
}
public WxMpUserList userList(String next_openid) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/user/get";
- String responseContent = execute(new SimpleGetRequestExecutor(), url, next_openid == null ? null : "next_openid=" + next_openid);
+ String responseContent = execute(new JoddGetRequestExecutor(), url, next_openid == null ? null : "next_openid=" + next_openid);
return WxMpUserList.fromJson(responseContent);
}
@@ -483,7 +489,7 @@ public class WxMpServiceImpl implements WxMpService {
scene.addProperty("scene_id", scene_id);
actionInfo.add("scene", scene);
json.add("action_info", actionInfo);
- String responseContent = execute(new SimplePostRequestExecutor(), url, json.toString());
+ String responseContent = execute(new JoddPostRequestExecutor(), url, json.toString());
return WxMpQrCodeTicket.fromJson(responseContent);
}
@@ -496,7 +502,7 @@ public class WxMpServiceImpl implements WxMpService {
scene.addProperty("scene_id", scene_id);
actionInfo.add("scene", scene);
json.add("action_info", actionInfo);
- String responseContent = execute(new SimplePostRequestExecutor(), url, json.toString());
+ String responseContent = execute(new JoddPostRequestExecutor(), url, json.toString());
return WxMpQrCodeTicket.fromJson(responseContent);
}
@@ -509,7 +515,7 @@ public class WxMpServiceImpl implements WxMpService {
scene.addProperty("scene_str", scene_str);
actionInfo.add("scene", scene);
json.add("action_info", actionInfo);
- String responseContent = execute(new SimplePostRequestExecutor(), url, json.toString());
+ String responseContent = execute(new JoddPostRequestExecutor(), url, json.toString());
return WxMpQrCodeTicket.fromJson(responseContent);
}
@@ -523,14 +529,14 @@ public class WxMpServiceImpl implements WxMpService {
JsonObject o = new JsonObject();
o.addProperty("action", "long2short");
o.addProperty("long_url", long_url);
- String responseContent = execute(new SimplePostRequestExecutor(), url, o.toString());
+ String responseContent = execute(new JoddPostRequestExecutor(), url, o.toString());
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
}
public String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send";
- String responseContent = execute(new SimplePostRequestExecutor(), url, templateMessage.toJson());
+ String responseContent = execute(new JoddPostRequestExecutor(), url, templateMessage.toJson());
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
final JsonObject jsonObject = tmpJsonElement.getAsJsonObject();
if (jsonObject.get("errcode").getAsInt() == 0)
@@ -540,7 +546,7 @@ public class WxMpServiceImpl implements WxMpService {
public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException {
String url = "https://api.weixin.qq.com/semantic/semproxy/search";
- String responseContent = execute(new SimplePostRequestExecutor(), url, semanticQuery.toJson());
+ String responseContent = execute(new JoddPostRequestExecutor(), url, semanticQuery.toJson());
return WxMpSemanticQueryResult.fromJson(responseContent);
}
@@ -572,7 +578,7 @@ public class WxMpServiceImpl implements WxMpService {
url += "&grant_type=authorization_code";
try {
- RequestExecutor executor = new SimpleGetRequestExecutor();
+ RequestExecutor executor = new JoddGetRequestExecutor();
String responseText = executor.execute(getHttpclient(), httpProxy, url, null);
return WxMpOAuth2AccessToken.fromJson(responseText);
} catch (ClientProtocolException e) {
@@ -590,7 +596,7 @@ public class WxMpServiceImpl implements WxMpService {
url += "&refresh_token=" + refreshToken;
try {
- RequestExecutor executor = new SimpleGetRequestExecutor();
+ RequestExecutor executor = new JoddGetRequestExecutor();
String responseText = executor.execute(getHttpclient(), httpProxy, url, null);
return WxMpOAuth2AccessToken.fromJson(responseText);
} catch (ClientProtocolException e) {
@@ -612,7 +618,7 @@ public class WxMpServiceImpl implements WxMpService {
}
try {
- RequestExecutor executor = new SimpleGetRequestExecutor();
+ RequestExecutor executor = new JoddGetRequestExecutor();
String responseText = executor.execute(getHttpclient(), httpProxy, url, null);
return WxMpUser.fromJson(responseText);
} catch (ClientProtocolException e) {
@@ -629,7 +635,7 @@ public class WxMpServiceImpl implements WxMpService {
url += "&openid=" + oAuth2AccessToken.getOpenId();
try {
- RequestExecutor executor = new SimpleGetRequestExecutor();
+ RequestExecutor executor = new JoddGetRequestExecutor();
executor.execute(getHttpclient(), httpProxy, url, null);
} catch (ClientProtocolException e) {
throw new RuntimeException(e);
@@ -682,11 +688,11 @@ public class WxMpServiceImpl implements WxMpService {
}
public String get(String url, String queryParam) throws WxErrorException {
- return execute(new SimpleGetRequestExecutor(), url, queryParam);
+ return execute(new JoddGetRequestExecutor(), url, queryParam);
}
public String post(String url, String postData) throws WxErrorException {
- return execute(new SimplePostRequestExecutor(), url, postData);
+ return execute(new JoddPostRequestExecutor(), url, postData);
}
/**
@@ -851,8 +857,7 @@ public class WxMpServiceImpl implements WxMpService {
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
httpPost.setEntity(entity);
- try {
- CloseableHttpResponse response = getHttpclient().execute(httpPost);
+ try(CloseableHttpResponse response = getHttpclient().execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
XStream xstream = XStreamInitializer.getInstance();
xstream.alias("xml", WxMpPrepayIdResult.class);
@@ -878,13 +883,14 @@ public class WxMpServiceImpl implements WxMpService {
}
@Override
- public Map getJSSDKPayInfo(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl) {
+ public Map getJSSDKPayInfo(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl)
+ throws WxErrorException {
Map packageParams = new HashMap();
packageParams.put("appid", wxMpConfigStorage.getAppId());
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
packageParams.put("body", body);
packageParams.put("out_trade_no", outTradeNo);
- packageParams.put("total_fee", (int) (amt * 100) + "");
+ packageParams.put("total_fee", String.format("%.0f", amt * 100));
packageParams.put("spbill_create_ip", ip);
packageParams.put("notify_url", callbackUrl);
packageParams.put("trade_type", tradeType);
@@ -894,8 +900,21 @@ public class WxMpServiceImpl implements WxMpService {
}
@Override
- public Map getJSSDKPayInfo(Map parameters) {
+ public Map getJSSDKPayInfo(Map parameters) throws WxErrorException {
WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId(parameters);
+
+ if (!"SUCCESS".equalsIgnoreCase(wxMpPrepayIdResult.getReturn_code())
+ ||!"SUCCESS".equalsIgnoreCase(wxMpPrepayIdResult.getResult_code())) {
+ WxError error = new WxError();
+ error.setErrorCode(-1);
+ error.setErrorMsg("return_code:" + wxMpPrepayIdResult.getReturn_code() +
+ ";return_msg:" + wxMpPrepayIdResult.getReturn_msg() +
+ ";result_code:" + wxMpPrepayIdResult.getResult_code() +
+ ";err_code" + wxMpPrepayIdResult.getErr_code() +
+ ";err_code_des" + wxMpPrepayIdResult.getErr_code_des());
+ throw new WxErrorException(error);
+ }
+
String prepayId = wxMpPrepayIdResult.getPrepay_id();
if (prepayId == null || prepayId.equals("")) {
throw new RuntimeException(String.format("Failed to get prepay id due to error code '%s'(%s).", wxMpPrepayIdResult.getErr_code(), wxMpPrepayIdResult.getErr_code_des()));
@@ -944,8 +963,7 @@ public class WxMpServiceImpl implements WxMpService {
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
httpPost.setEntity(entity);
- try {
- CloseableHttpResponse response = httpClient.execute(httpPost);
+ try(CloseableHttpResponse response = httpClient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
XStream xstream = XStreamInitializer.getInstance();
xstream.alias("xml", WxMpPayResult.class);
@@ -969,6 +987,59 @@ public class WxMpServiceImpl implements WxMpService {
return new WxMpPayCallback();
}
+ @Override
+ public WxMpPayRefundResult refundPay(Map parameters) throws WxErrorException {
+ SortedMap refundParams = new TreeMap(parameters);
+ refundParams.put("appid", wxMpConfigStorage.getAppId());
+ refundParams.put("mch_id", wxMpConfigStorage.getPartnerId());
+ refundParams.put("nonce_str", System.currentTimeMillis() + "");
+ refundParams.put("op_user_id", wxMpConfigStorage.getPartnerId());
+ String sign = WxCryptUtil.createSign(refundParams, wxMpConfigStorage.getPartnerKey());
+ refundParams.put("sign", sign);
+
+ StringBuilder request = new StringBuilder("");
+ for (Entry para : refundParams.entrySet()) {
+ request.append(String.format("<%s>%s%s>", para.getKey(), para.getValue(), para.getKey()));
+ }
+ request.append("");
+
+ HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund");
+ if (httpProxy != null) {
+ RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
+ httpPost.setConfig(config);
+ }
+
+ StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
+ httpPost.setEntity(entity);
+ try(
+ CloseableHttpResponse response = getHttpclient().execute(httpPost)) {
+ String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
+ XStream xstream = XStreamInitializer.getInstance();
+ xstream.processAnnotations(WxMpPayRefundResult.class);
+ WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult) xstream.fromXML(responseContent);
+
+ if (!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getResultCode())
+ ||!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getReturnCode())) {
+ WxError error = new WxError();
+ error.setErrorCode(-1);
+ error.setErrorMsg("return_code:" + wxMpPayRefundResult.getReturnCode() +
+ ";return_msg:" + wxMpPayRefundResult.getReturnMsg() +
+ ";result_code:" + wxMpPayRefundResult.getResultCode() +
+ ";err_code" + wxMpPayRefundResult.getErrCode() +
+ ";err_code_des" + wxMpPayRefundResult.getErrCodeDes());
+ throw new WxErrorException(error);
+ }
+
+ return wxMpPayRefundResult;
+ } catch (IOException e) {
+ log.error(MessageFormatter.format("The exception was happened when sending refund '{}'.", request.toString()).getMessage(), e);
+ WxError error = new WxError();
+ error.setErrorCode(-1);
+ error.setErrorMsg("incorrect response.");
+ throw new WxErrorException(error);
+ }
+ }
+
@Override
public boolean checkJSSDKCallbackDataSignature(Map kvm, String signature) {
return signature.equals(WxCryptUtil.createSign(kvm, wxMpConfigStorage.getPartnerKey()));
@@ -1000,8 +1071,7 @@ public class WxMpServiceImpl implements WxMpService {
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
httpPost.setEntity(entity);
- try {
- CloseableHttpResponse response = getHttpclient().execute(httpPost);
+ try(CloseableHttpResponse response = getHttpclient().execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(WxRedpackResult.class);
@@ -1014,4 +1084,201 @@ public class WxMpServiceImpl implements WxMpService {
throw new WxErrorException(error);
}
}
+
+ /**
+ * 获得卡券api_ticket,不强制刷新卡券api_ticket
+ *
+ * @return 卡券api_ticket
+ * @throws WxErrorException
+ * @see #getCardApiTicket(boolean)
+ */
+ @Override
+ public String getCardApiTicket() throws WxErrorException {
+ return getCardApiTicket(false);
+ }
+
+ /**
+ *
+ * 获得卡券api_ticket
+ * 获得时会检查卡券apiToken是否过期,如果过期了,那么就刷新一下,否则就什么都不干
+ *
+ * 详情请见: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
+ *
+ *
+ * @param forceRefresh 强制刷新
+ * @return 卡券api_ticket
+ * @throws WxErrorException
+ */
+ @Override
+ public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
+ if (forceRefresh) {
+ wxMpConfigStorage.expireCardApiTicket();
+ }
+ if (wxMpConfigStorage.isCardApiTicketExpired()) {
+ synchronized (globalCardApiTicketRefreshLock) {
+ if (wxMpConfigStorage.isCardApiTicketExpired()) {
+ String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card";
+ String responseContent = execute(new JoddGetRequestExecutor(), url, null);
+ JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
+ JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
+ String cardApiTicket = tmpJsonObject.get("ticket").getAsString();
+ int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
+ wxMpConfigStorage.updateCardApiTicket(cardApiTicket, expiresInSeconds);
+ }
+ }
+ }
+ return wxMpConfigStorage.getCardApiTicket();
+ }
+
+ /**
+ *
+ * 创建调用卡券api时所需要的签名
+ *
+ * 详情请见: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
+ *
+ *
+ * @param optionalSignParam 参与签名的参数数组。
+ * 可以为下列字段:app_id, card_id, card_type, code, openid, location_id
+ * 注意:当做wx.chooseCard调用时,必须传入app_id参与签名,否则会造成签名失败导致拉取卡券列表为空
+ * @return 卡券Api签名对象
+ */
+ @Override
+ public WxCardApiSignature createCardApiSignature(String... optionalSignParam) throws
+ WxErrorException {
+ long timestamp = System.currentTimeMillis() / 1000;
+ String nonceStr = RandomUtils.getRandomStr();
+ String cardApiTicket = getCardApiTicket(false);
+
+ String[] signParam = Arrays.copyOf(optionalSignParam, optionalSignParam.length + 3);
+ signParam[optionalSignParam.length] = String.valueOf(timestamp);
+ signParam[optionalSignParam.length + 1] = nonceStr;
+ signParam[optionalSignParam.length + 2] = cardApiTicket;
+ try {
+ String signature = SHA1.gen(signParam);
+ WxCardApiSignature cardApiSignature = new WxCardApiSignature();
+ cardApiSignature.setTimestamp(timestamp);
+ cardApiSignature.setNonceStr(nonceStr);
+ cardApiSignature.setSignature(signature);
+ return cardApiSignature;
+ } catch (NoSuchAlgorithmException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * 卡券Code解码
+ *
+ * @param encryptCode 加密Code,通过JSSDK的chooseCard接口获得
+ * @return 解密后的Code
+ * @throws WxErrorException
+ */
+ @Override
+ public String decryptCardCode(String encryptCode) throws WxErrorException {
+ String url = "https://api.weixin.qq.com/card/code/decrypt";
+ JsonObject param = new JsonObject();
+ param.addProperty("encrypt_code", encryptCode);
+ String responseContent = post(url, param.toString());
+ JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
+ JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
+ JsonPrimitive jsonPrimitive = tmpJsonObject.getAsJsonPrimitive("code");
+ return jsonPrimitive.getAsString();
+ }
+
+ /**
+ * 卡券Code查询
+ *
+ * @param cardId 卡券ID代表一类卡券
+ * @param code 单张卡券的唯一标准
+ * @param checkConsume 是否校验code核销状态,填入true和false时的code异常状态返回数据不同
+ * @return WxMpCardResult对象
+ * @throws WxErrorException
+ */
+ @Override
+ public WxMpCardResult queryCardCode(String cardId, String code, boolean checkConsume) throws WxErrorException {
+ String url = "https://api.weixin.qq.com/card/code/get";
+ JsonObject param = new JsonObject();
+ param.addProperty("card_id", cardId);
+ param.addProperty("code", code);
+ param.addProperty("check_consume", checkConsume);
+ String responseContent = post(url, param.toString());
+ JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
+ return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement,
+ new TypeToken() {
+ }.getType());
+ }
+
+ /**
+ * 卡券Code核销。核销失败会抛出异常
+ *
+ * @param code 单张卡券的唯一标准
+ * @throws WxErrorException
+ */
+ @Override
+ public String consumeCardCode(String code, String cardId) throws WxErrorException {
+ String url = "https://api.weixin.qq.com/card/code/consume";
+ JsonObject param = new JsonObject();
+ param.addProperty("code", code);
+
+ if (cardId != null && !"".equals(cardId)) {
+ param.addProperty("card_id", cardId);
+ }
+
+ String responseContent = post(url, param.toString());
+ return responseContent;
+ }
+
+ /**
+ * 卡券Mark接口。
+ * 开发者在帮助消费者核销卡券之前,必须帮助先将此code(卡券串码)与一个openid绑定(即mark住),
+ * 才能进一步调用核销接口,否则报错。
+ *
+ * @param code 卡券的code码
+ * @param cardId 卡券的ID
+ * @param openId 用券用户的openid
+ * @param isMark 是否要mark(占用)这个code,填写true或者false,表示占用或解除占用
+ * @throws WxErrorException
+ */
+ @Override
+ public void markCardCode(String code, String cardId, String openId, boolean isMark) throws
+ WxErrorException {
+ String url = "https://api.weixin.qq.com/card/code/mark";
+ JsonObject param = new JsonObject();
+ param.addProperty("code", code);
+ param.addProperty("card_id", cardId);
+ param.addProperty("openid", openId);
+ param.addProperty("is_mark", isMark);
+ String responseContent = post(url, param.toString());
+ JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
+ WxMpCardResult cardResult = WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement,
+ new TypeToken() { }.getType());
+ if (!cardResult.getErrorCode().equals("0")) {
+ log.warn("朋友的券mark失败:{}", cardResult.getErrorMsg());
+ }
+ }
+
+ @Override
+ public String getCardDetail(String cardId) throws WxErrorException {
+ String url = "https://api.weixin.qq.com/card/get";
+ JsonObject param = new JsonObject();
+ param.addProperty("card_id", cardId);
+ String responseContent = post(url, param.toString());
+
+ // 判断返回值
+ JsonObject json = (new JsonParser()).parse(responseContent).getAsJsonObject();
+ String errcode = json.get("errcode").getAsString();
+ if (!"0".equals(errcode)) {
+ String errmsg = json.get("errmsg").getAsString();
+ WxError error = new WxError();
+ error.setErrorCode(Integer.valueOf(errcode));
+ error.setErrorMsg(errmsg);
+ throw new WxErrorException(error);
+ }
+
+ return responseContent;
+ }
+
}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpCard.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpCard.java
new file mode 100644
index 000000000..d530fbe82
--- /dev/null
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpCard.java
@@ -0,0 +1,72 @@
+package me.chanjar.weixin.mp.bean;
+
+
+/**
+ * 微信卡券
+ *
+ * @author YuJian
+ * @version 15/11/11
+ */
+public class WxMpCard {
+
+ private String cardId;
+
+ private Long beginTime;
+
+ private Long endTime;
+
+ private String userCardStatus;
+
+ private Boolean canConsume;
+
+ public String getCardId() {
+ return cardId;
+ }
+
+ public void setCardId(String cardId) {
+ this.cardId = cardId;
+ }
+
+ public Long getBeginTime() {
+ return beginTime;
+ }
+
+ public void setBeginTime(Long beginTime) {
+ this.beginTime = beginTime;
+ }
+
+ public Long getEndTime() {
+ return endTime;
+ }
+
+ public void setEndTime(Long endTime) {
+ this.endTime = endTime;
+ }
+
+ public String getUserCardStatus() {
+ return userCardStatus;
+ }
+
+ public void setUserCardStatus(String userCardStatus) {
+ this.userCardStatus = userCardStatus;
+ }
+
+ public Boolean getCanConsume() {
+ return canConsume;
+ }
+
+ public void setCanConsume(Boolean canConsume) {
+ this.canConsume = canConsume;
+ }
+
+ @Override
+ public String toString() {
+ return "WxMpCard{" +
+ "cardId='" + cardId + '\'' +
+ ", beginTime=" + beginTime +
+ ", endTime=" + endTime +
+ ", userCardStatus='" + userCardStatus + '\'' +
+ ", canConsume=" + canConsume +
+ '}';
+ }
+}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpXmlMessage.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpXmlMessage.java
index 8796b1105..9f42ffc70 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpXmlMessage.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpXmlMessage.java
@@ -150,6 +150,31 @@ public class WxMpXmlMessage implements Serializable {
@XStreamAlias("ErrorCount")
private Integer errorCount;
+ ///////////////////////////////////////
+ // 卡券相关事件推送
+ ///////////////////////////////////////
+ @XStreamAlias("CardId")
+ @XStreamConverter(value=XStreamCDataConverter.class)
+ private String cardId;
+
+ @XStreamAlias("FriendUserName")
+ @XStreamConverter(value=XStreamCDataConverter.class)
+ private String friendUserName;
+
+ @XStreamAlias("IsGiveByFriend")
+ private Integer isGiveByFriend; // 是否为转赠,1代表是,0代表否
+
+ @XStreamAlias("UserCardCode")
+ @XStreamConverter(value=XStreamCDataConverter.class)
+ private String userCardCode;
+
+ @XStreamAlias("OldUserCardCode")
+ @XStreamConverter(value=XStreamCDataConverter.class)
+ private String oldUserCardCode;
+
+ @XStreamAlias("OuterId")
+ private Integer outerId;
+
@XStreamAlias("ScanCodeInfo")
private ScanCodeInfo scanCodeInfo = new ScanCodeInfo();
@@ -456,6 +481,54 @@ public class WxMpXmlMessage implements Serializable {
this.errorCount = errorCount;
}
+ public String getCardId() {
+ return cardId;
+ }
+
+ public void setCardId(String cardId) {
+ this.cardId = cardId;
+ }
+
+ public String getFriendUserName() {
+ return friendUserName;
+ }
+
+ public void setFriendUserName(String friendUserName) {
+ this.friendUserName = friendUserName;
+ }
+
+ public Integer getIsGiveByFriend() {
+ return isGiveByFriend;
+ }
+
+ public void setIsGiveByFriend(Integer isGiveByFriend) {
+ this.isGiveByFriend = isGiveByFriend;
+ }
+
+ public String getUserCardCode() {
+ return userCardCode;
+ }
+
+ public void setUserCardCode(String userCardCode) {
+ this.userCardCode = userCardCode;
+ }
+
+ public String getOldUserCardCode() {
+ return oldUserCardCode;
+ }
+
+ public void setOldUserCardCode(String oldUserCardCode) {
+ this.oldUserCardCode = oldUserCardCode;
+ }
+
+ public Integer getOuterId() {
+ return outerId;
+ }
+
+ public void setOuterId(Integer outerId) {
+ this.outerId = outerId;
+ }
+
public WxMpXmlMessage.ScanCodeInfo getScanCodeInfo() {
return scanCodeInfo;
}
@@ -652,6 +725,11 @@ public class WxMpXmlMessage implements Serializable {
", filterCount=" + filterCount +
", sentCount=" + sentCount +
", errorCount=" + errorCount +
+ ", cardId='" + cardId + '\'' +
+ ", isGiveByFriend=" + isGiveByFriend +
+ ", userCardCode='" + userCardCode + '\'' +
+ ", oldUserCardCode='" + oldUserCardCode + '\'' +
+ ", outerId=" + outerId +
", scanCodeInfo=" + scanCodeInfo +
", sendPicsInfo=" + sendPicsInfo +
", sendLocationInfo=" + sendLocationInfo +
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpCardResult.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpCardResult.java
new file mode 100644
index 000000000..e6cd9a5f8
--- /dev/null
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpCardResult.java
@@ -0,0 +1,87 @@
+package me.chanjar.weixin.mp.bean.result;
+
+import me.chanjar.weixin.mp.bean.WxMpCard;
+
+import java.io.Serializable;
+
+/**
+ * 卡券查询Code,核销Code接口返回结果
+ *
+ * @author YuJian
+ * @version 15/11/11
+ */
+public class WxMpCardResult implements Serializable {
+
+ private String errorCode;
+
+ private String errorMsg;
+
+ private String openId;
+
+ private WxMpCard card;
+
+ private String userCardStatus;
+
+ private Boolean canConsume;
+
+ public String getErrorCode() {
+ return errorCode;
+ }
+
+ public void setErrorCode(String errorCode) {
+ this.errorCode = errorCode;
+ }
+
+ public String getErrorMsg() {
+ return errorMsg;
+ }
+
+ public void setErrorMsg(String errorMsg) {
+ this.errorMsg = errorMsg;
+ }
+
+ public String getOpenId() {
+ return openId;
+ }
+
+ public void setOpenId(String openId) {
+ this.openId = openId;
+ }
+
+ public WxMpCard getCard() {
+ return card;
+ }
+
+ public void setCard(WxMpCard card) {
+ this.card = card;
+ }
+
+ @Override
+ public String toString() {
+ return "WxMpCardResult{" +
+ "errorCode='" + errorCode + '\'' +
+ ", errorMsg='" + errorMsg + '\'' +
+ ", openId='" + openId + '\'' +
+ ", card=" + card +
+ ", userCardStatus='" + userCardStatus + '\'' +
+ ", canConsume=" + canConsume +
+ '}';
+ }
+
+ public String getUserCardStatus() {
+ return userCardStatus;
+ }
+
+ public void setUserCardStatus(String userCardStatus) {
+ this.userCardStatus = userCardStatus;
+ }
+
+ public Boolean getCanConsume() {
+ return canConsume;
+ }
+
+ public void setCanConsume(Boolean canConsume) {
+ this.canConsume = canConsume;
+ }
+
+}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpPayRefundResult.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpPayRefundResult.java
new file mode 100644
index 000000000..1e8327e30
--- /dev/null
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpPayRefundResult.java
@@ -0,0 +1,278 @@
+package me.chanjar.weixin.mp.bean.result;
+
+import java.io.Serializable;
+
+import com.thoughtworks.xstream.annotations.XStreamAlias;
+
+/**
+ * 微信支付-申请退款返回结果
+ * https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
+ * @author liukaitj
+ *
+ */
+@XStreamAlias("xml")
+public class WxMpPayRefundResult implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ @XStreamAlias("return_code")
+ private String returnCode;
+
+ @XStreamAlias("return_msg")
+ private String returnMsg;
+
+ @XStreamAlias("result_code")
+ private String resultCode;
+
+ @XStreamAlias("err_code")
+ private String errCode;
+
+ @XStreamAlias("err_code_des")
+ private String errCodeDes;
+
+ @XStreamAlias("appid")
+ private String appid;
+
+ @XStreamAlias("mch_id")
+ private String mchId;
+
+ @XStreamAlias("device_info")
+ private String deviceInfo;
+
+ @XStreamAlias("nonce_str")
+ private String nonceStr;
+
+ @XStreamAlias("sign")
+ private String sign;
+
+ @XStreamAlias("transaction_id")
+ private String transactionId;
+
+ @XStreamAlias("out_trade_no")
+ private String outTradeNo;
+
+ @XStreamAlias("out_refund_no")
+ private String outRefundNo;
+
+ @XStreamAlias("refund_id")
+ private String refundId;
+
+ @XStreamAlias("refund_channel")
+ private String refundChannel;
+
+ @XStreamAlias("refund_fee")
+ private String refundFee;
+
+ @XStreamAlias("total_fee")
+ private String totalFee;
+
+ @XStreamAlias("fee_type")
+ private String feeType;
+
+ @XStreamAlias("cash_fee")
+ private String cashFee;
+
+ @XStreamAlias("cash_refund_fee")
+ private String cashRefundfee;
+
+ @XStreamAlias("coupon_refund_fee")
+ private String couponRefundFee;
+
+ @XStreamAlias("coupon_refund_count")
+ private String couponRefundCount;
+
+ @XStreamAlias("coupon_refund_id")
+ private String couponRefundId;
+
+ public String getReturnCode() {
+ return returnCode;
+ }
+
+ public void setReturnCode(String returnCode) {
+ this.returnCode = returnCode;
+ }
+
+ public String getReturnMsg() {
+ return returnMsg;
+ }
+
+ public void setReturnMsg(String returnMsg) {
+ this.returnMsg = returnMsg;
+ }
+
+ public String getResultCode() {
+ return resultCode;
+ }
+
+ public void setResultCode(String resultCode) {
+ this.resultCode = resultCode;
+ }
+
+ public String getErrCode() {
+ return errCode;
+ }
+
+ public void setErrCode(String errCode) {
+ this.errCode = errCode;
+ }
+
+ public String getErrCodeDes() {
+ return errCodeDes;
+ }
+
+ public void setErrCodeDes(String errCodeDes) {
+ this.errCodeDes = errCodeDes;
+ }
+
+ public String getAppid() {
+ return appid;
+ }
+
+ public void setAppid(String appid) {
+ this.appid = appid;
+ }
+
+ public String getMchId() {
+ return mchId;
+ }
+
+ public void setMchId(String mchId) {
+ this.mchId = mchId;
+ }
+
+ public String getDeviceInfo() {
+ return deviceInfo;
+ }
+
+ public void setDeviceInfo(String deviceInfo) {
+ this.deviceInfo = deviceInfo;
+ }
+
+ public String getNonceStr() {
+ return nonceStr;
+ }
+
+ public void setNonceStr(String nonceStr) {
+ this.nonceStr = nonceStr;
+ }
+
+ public String getSign() {
+ return sign;
+ }
+
+ public void setSign(String sign) {
+ this.sign = sign;
+ }
+
+ public String getTransactionId() {
+ return transactionId;
+ }
+
+ public void setTransactionId(String transactionId) {
+ this.transactionId = transactionId;
+ }
+
+ public String getOutTradeNo() {
+ return outTradeNo;
+ }
+
+ public void setOutTradeNo(String outTradeNo) {
+ this.outTradeNo = outTradeNo;
+ }
+
+ public String getOutRefundNo() {
+ return outRefundNo;
+ }
+
+ public void setOutRefundNo(String outRefundNo) {
+ this.outRefundNo = outRefundNo;
+ }
+
+ public String getRefundId() {
+ return refundId;
+ }
+
+ public void setRefundId(String refundId) {
+ this.refundId = refundId;
+ }
+
+ public String getRefundChannel() {
+ return refundChannel;
+ }
+
+ public void setRefundChannel(String refundChannel) {
+ this.refundChannel = refundChannel;
+ }
+
+ public String getRefundFee() {
+ return refundFee;
+ }
+
+ public void setRefundFee(String refundFee) {
+ this.refundFee = refundFee;
+ }
+
+ public String getTotalFee() {
+ return totalFee;
+ }
+
+ public void setTotalFee(String totalFee) {
+ this.totalFee = totalFee;
+ }
+
+ public String getFeeType() {
+ return feeType;
+ }
+
+ public void setFeeType(String feeType) {
+ this.feeType = feeType;
+ }
+
+ public String getCashFee() {
+ return cashFee;
+ }
+
+ public void setCashFee(String cashFee) {
+ this.cashFee = cashFee;
+ }
+
+ public String getCashRefundfee() {
+ return cashRefundfee;
+ }
+
+ public void setCashRefundfee(String cashRefundfee) {
+ this.cashRefundfee = cashRefundfee;
+ }
+
+ public String getCouponRefundFee() {
+ return couponRefundFee;
+ }
+
+ public void setCouponRefundFee(String couponRefundFee) {
+ this.couponRefundFee = couponRefundFee;
+ }
+
+ public String getCouponRefundCount() {
+ return couponRefundCount;
+ }
+
+ public void setCouponRefundCount(String couponRefundCount) {
+ this.couponRefundCount = couponRefundCount;
+ }
+
+ public String getCouponRefundId() {
+ return couponRefundId;
+ }
+
+ public void setCouponRefundId(String couponRefundId) {
+ this.couponRefundId = couponRefundId;
+ }
+
+ @Override
+ public String toString() {
+ return "[" +
+ "return_code:" + returnCode + ";" +
+ "return_msg" + returnMsg + ";";
+ }
+
+}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardGsonAdapter.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardGsonAdapter.java
new file mode 100644
index 000000000..dab414a3e
--- /dev/null
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardGsonAdapter.java
@@ -0,0 +1,38 @@
+package me.chanjar.weixin.mp.util.json;
+
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+import com.google.gson.reflect.TypeToken;
+import me.chanjar.weixin.common.util.json.GsonHelper;
+import me.chanjar.weixin.mp.bean.WxMpCard;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Type;
+import java.util.List;
+
+/**
+ * Created by YuJian on 15/11/11.
+ *
+ * @author YuJian
+ * @version 15/11/11
+ */
+public class WxMpCardGsonAdapter implements JsonDeserializer {
+
+ @Override
+ public WxMpCard deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext
+ jsonDeserializationContext) throws JsonParseException {
+ WxMpCard card = new WxMpCard();
+ JsonObject jsonObject = jsonElement.getAsJsonObject();
+
+ card.setCardId(GsonHelper.getString(jsonObject, "card_id"));
+ card.setBeginTime(GsonHelper.getLong(jsonObject, "begin_time"));
+ card.setEndTime(GsonHelper.getLong(jsonObject, "end_time"));
+
+ return card;
+ }
+
+}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardResultGsonAdapter.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardResultGsonAdapter.java
new file mode 100644
index 000000000..be1a12da3
--- /dev/null
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardResultGsonAdapter.java
@@ -0,0 +1,45 @@
+package me.chanjar.weixin.mp.util.json;
+
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+import com.google.gson.reflect.TypeToken;
+import me.chanjar.weixin.common.util.json.GsonHelper;
+import me.chanjar.weixin.mp.bean.WxMpCard;
+import me.chanjar.weixin.mp.bean.result.WxMpCardResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Type;
+import java.text.ParseException;
+import java.util.List;
+
+/**
+ * Created by YuJian on 15/11/11.
+ *
+ * @author YuJian
+ * @version 15/11/11
+ */
+public class WxMpCardResultGsonAdapter implements JsonDeserializer {
+ @Override
+ public WxMpCardResult deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
+ WxMpCardResult cardResult = new WxMpCardResult();
+ JsonObject jsonObject = jsonElement.getAsJsonObject();
+
+ cardResult.setOpenId(GsonHelper.getString(jsonObject, "openid"));
+ cardResult.setErrorCode(GsonHelper.getString(jsonObject, "errcode"));
+ cardResult.setErrorMsg(GsonHelper.getString(jsonObject, "errmsg"));
+ cardResult.setCanConsume(GsonHelper.getBoolean(jsonObject, "can_consume"));
+ cardResult.setUserCardStatus(GsonHelper.getString(jsonObject, "user_card_status"));
+
+ WxMpCard card = WxMpGsonBuilder.INSTANCE.create().fromJson(jsonObject.get("card"),
+ new TypeToken() {
+ }.getType());
+
+ cardResult.setCard(card);
+
+ return cardResult;
+ }
+}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java
index 20f8d9d47..316f22f54 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java
@@ -38,6 +38,8 @@ public class WxMpGsonBuilder {
INSTANCE.registerTypeAdapter(WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem.class, new WxMpMaterialNewsBatchGetGsonItemAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialFileBatchGetResult.class, new WxMpMaterialFileBatchGetGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem.class, new WxMpMaterialFileBatchGetGsonItemAdapter());
+ INSTANCE.registerTypeAdapter(WxMpCardResult.class, new WxMpCardResultGsonAdapter());
+ INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter());
}
public static Gson create() {