|
56 | 56 | import me.chanjar.weixin.mp.bean.result.WxMpMaterialVideoInfoResult;
|
57 | 57 | import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
|
58 | 58 | import me.chanjar.weixin.mp.bean.result.WxMpPayCallback;
|
| 59 | +import me.chanjar.weixin.mp.bean.result.WxMpPayRefundResult; |
59 | 60 | import me.chanjar.weixin.mp.bean.result.WxMpPayResult;
|
60 | 61 | import me.chanjar.weixin.mp.bean.result.WxMpPrepayIdResult;
|
61 | 62 | import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
|
|
97 | 98 | import com.google.gson.JsonArray;
|
98 | 99 | import com.google.gson.JsonElement;
|
99 | 100 | import com.google.gson.JsonObject;
|
| 101 | +import com.google.gson.JsonParser; |
100 | 102 | import com.google.gson.internal.Streams;
|
101 | 103 | import com.google.gson.reflect.TypeToken;
|
102 | 104 | import com.google.gson.stream.JsonReader;
|
@@ -971,6 +973,54 @@ public WxMpPayCallback getJSSDKCallbackData(String xmlData) {
|
971 | 973 | return new WxMpPayCallback();
|
972 | 974 | }
|
973 | 975 |
|
| 976 | + @Override |
| 977 | + public WxMpPayRefundResult refundPay(Map<String, String> parameters) throws WxErrorException { |
| 978 | + SortedMap<String, String> refundParams = new TreeMap<String, String>(parameters); |
| 979 | + refundParams.put("appid", wxMpConfigStorage.getAppId()); |
| 980 | + refundParams.put("mch_id", wxMpConfigStorage.getPartnerId()); |
| 981 | + refundParams.put("nonceStr", System.currentTimeMillis() + ""); |
| 982 | + refundParams.put("op_user_id", wxMpConfigStorage.getPartnerId()); |
| 983 | + String sign = WxCryptUtil.createSign(refundParams, wxMpConfigStorage.getPartnerKey()); |
| 984 | + refundParams.put("sign", sign); |
| 985 | + |
| 986 | + StringBuilder request = new StringBuilder("<xml>"); |
| 987 | + for (Entry<String, String> para : refundParams.entrySet()) { |
| 988 | + request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey())); |
| 989 | + } |
| 990 | + request.append("</xml>"); |
| 991 | + |
| 992 | + HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund"); |
| 993 | + if (httpProxy != null) { |
| 994 | + RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); |
| 995 | + httpPost.setConfig(config); |
| 996 | + } |
| 997 | + |
| 998 | + StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); |
| 999 | + httpPost.setEntity(entity); |
| 1000 | + try( |
| 1001 | + CloseableHttpResponse response = getHttpclient().execute(httpPost)) { |
| 1002 | + String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); |
| 1003 | + XStream xstream = XStreamInitializer.getInstance(); |
| 1004 | + xstream.processAnnotations(WxRedpackResult.class); |
| 1005 | + WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult) xstream.fromXML(responseContent); |
| 1006 | + |
| 1007 | + if ("FAIL".equals(wxMpPayRefundResult.getResultCode())) { |
| 1008 | + WxError error = new WxError(); |
| 1009 | + error.setErrorCode(-1); |
| 1010 | + error.setErrorMsg(wxMpPayRefundResult.getErrCodeDes()); |
| 1011 | + throw new WxErrorException(error); |
| 1012 | + } |
| 1013 | + |
| 1014 | + return wxMpPayRefundResult; |
| 1015 | + } catch (IOException e) { |
| 1016 | + log.error(MessageFormatter.format("The exception was happened when sending refund '{}'.", request.toString()).getMessage(), e); |
| 1017 | + WxError error = new WxError(); |
| 1018 | + error.setErrorCode(-1); |
| 1019 | + error.setErrorMsg("incorrect response."); |
| 1020 | + throw new WxErrorException(error); |
| 1021 | + } |
| 1022 | + } |
| 1023 | + |
974 | 1024 | @Override
|
975 | 1025 | public boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm, String signature) {
|
976 | 1026 | return signature.equals(WxCryptUtil.createSign(kvm, wxMpConfigStorage.getPartnerKey()));
|
@@ -1074,6 +1124,7 @@ public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
|
1074 | 1124 | *
|
1075 | 1125 | * @param optionalSignParam 参与签名的参数数组。
|
1076 | 1126 | * 可以为下列字段:app_id, card_id, card_type, code, openid, location_id
|
| 1127 | + * </br>注意:当做wx.chooseCard调用时,必须传入app_id参与签名,否则会造成签名失败导致拉取卡券列表为空 |
1077 | 1128 | * @return 卡券Api签名对象
|
1078 | 1129 | */
|
1079 | 1130 | @Override
|
@@ -1148,11 +1199,17 @@ public WxMpCardResult queryCardCode(String cardId, String code, boolean checkCon
|
1148 | 1199 | * @throws WxErrorException
|
1149 | 1200 | */
|
1150 | 1201 | @Override
|
1151 |
| - public void consumeCardCode(String code) throws WxErrorException { |
| 1202 | + public String consumeCardCode(String code, String cardId) throws WxErrorException { |
1152 | 1203 | String url = "https://api.weixin.qq.com/card/code/consume";
|
1153 | 1204 | JsonObject param = new JsonObject();
|
1154 | 1205 | param.addProperty("code", code);
|
1155 |
| - post(url, param.toString()); |
| 1206 | + |
| 1207 | + if (cardId != null && !"".equals(cardId)) { |
| 1208 | + param.addProperty("card_id", cardId); |
| 1209 | + } |
| 1210 | + |
| 1211 | + String responseContent = post(url, param.toString()); |
| 1212 | + return responseContent; |
1156 | 1213 | }
|
1157 | 1214 |
|
1158 | 1215 | /**
|
@@ -1183,4 +1240,26 @@ public void markCardCode(String code, String cardId, String openId, boolean isMa
|
1183 | 1240 | log.warn("朋友的券mark失败:{}", cardResult.getErrorMsg());
|
1184 | 1241 | }
|
1185 | 1242 | }
|
| 1243 | + |
| 1244 | + @Override |
| 1245 | + public String getCardDetail(String cardId) throws WxErrorException { |
| 1246 | + String url = "https://api.weixin.qq.com/card/get"; |
| 1247 | + JsonObject param = new JsonObject(); |
| 1248 | + param.addProperty("card_id", cardId); |
| 1249 | + String responseContent = post(url, param.toString()); |
| 1250 | + |
| 1251 | + // 判断返回值 |
| 1252 | + JsonObject json = (new JsonParser()).parse(responseContent).getAsJsonObject(); |
| 1253 | + String errcode = json.get("errcode").getAsString(); |
| 1254 | + if (!"0".equals(errcode)) { |
| 1255 | + String errmsg = json.get("errmsg").getAsString(); |
| 1256 | + WxError error = new WxError(); |
| 1257 | + error.setErrorCode(Integer.valueOf(errcode)); |
| 1258 | + error.setErrorMsg(errmsg); |
| 1259 | + throw new WxErrorException(error); |
| 1260 | + } |
| 1261 | + |
| 1262 | + return responseContent; |
| 1263 | + } |
| 1264 | + |
1186 | 1265 | }
|
0 commit comments