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 ;
@@ -881,7 +883,8 @@ private void checkParameters(Map<String, String> parameters) {
881
883
}
882
884
883
885
@ Override
884
- public Map <String , String > getJSSDKPayInfo (String openId , String outTradeNo , double amt , String body , String tradeType , String ip , String callbackUrl ) {
886
+ public Map <String , String > getJSSDKPayInfo (String openId , String outTradeNo , double amt , String body , String tradeType , String ip , String callbackUrl )
887
+ throws WxErrorException {
885
888
Map <String , String > packageParams = new HashMap <String , String >();
886
889
packageParams .put ("appid" , wxMpConfigStorage .getAppId ());
887
890
packageParams .put ("mch_id" , wxMpConfigStorage .getPartnerId ());
@@ -897,8 +900,21 @@ public Map<String, String> getJSSDKPayInfo(String openId, String outTradeNo, dou
897
900
}
898
901
899
902
@ Override
900
- public Map <String , String > getJSSDKPayInfo (Map <String , String > parameters ) {
903
+ public Map <String , String > getJSSDKPayInfo (Map <String , String > parameters ) throws WxErrorException {
901
904
WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId (parameters );
905
+
906
+ if (!"SUCCESS" .equalsIgnoreCase (wxMpPrepayIdResult .getReturn_code ())
907
+ ||!"SUCCESS" .equalsIgnoreCase (wxMpPrepayIdResult .getResult_code ())) {
908
+ WxError error = new WxError ();
909
+ error .setErrorCode (-1 );
910
+ error .setErrorMsg ("return_code:" + wxMpPrepayIdResult .getReturn_code () +
911
+ ";return_msg:" + wxMpPrepayIdResult .getReturn_msg () +
912
+ ";result_code:" + wxMpPrepayIdResult .getResult_code () +
913
+ ";err_code" + wxMpPrepayIdResult .getErr_code () +
914
+ ";err_code_des" + wxMpPrepayIdResult .getErr_code_des ());
915
+ throw new WxErrorException (error );
916
+ }
917
+
902
918
String prepayId = wxMpPrepayIdResult .getPrepay_id ();
903
919
if (prepayId == null || prepayId .equals ("" )) {
904
920
throw new RuntimeException (String .format ("Failed to get prepay id due to error code '%s'(%s)." , wxMpPrepayIdResult .getErr_code (), wxMpPrepayIdResult .getErr_code_des ()));
@@ -971,6 +987,59 @@ public WxMpPayCallback getJSSDKCallbackData(String xmlData) {
971
987
return new WxMpPayCallback ();
972
988
}
973
989
990
+ @ Override
991
+ public WxMpPayRefundResult refundPay (Map <String , String > parameters ) throws WxErrorException {
992
+ SortedMap <String , String > refundParams = new TreeMap <String , String >(parameters );
993
+ refundParams .put ("appid" , wxMpConfigStorage .getAppId ());
994
+ refundParams .put ("mch_id" , wxMpConfigStorage .getPartnerId ());
995
+ refundParams .put ("nonce_str" , System .currentTimeMillis () + "" );
996
+ refundParams .put ("op_user_id" , wxMpConfigStorage .getPartnerId ());
997
+ String sign = WxCryptUtil .createSign (refundParams , wxMpConfigStorage .getPartnerKey ());
998
+ refundParams .put ("sign" , sign );
999
+
1000
+ StringBuilder request = new StringBuilder ("<xml>" );
1001
+ for (Entry <String , String > para : refundParams .entrySet ()) {
1002
+ request .append (String .format ("<%s>%s</%s>" , para .getKey (), para .getValue (), para .getKey ()));
1003
+ }
1004
+ request .append ("</xml>" );
1005
+
1006
+ HttpPost httpPost = new HttpPost ("https://api.mch.weixin.qq.com/secapi/pay/refund" );
1007
+ if (httpProxy != null ) {
1008
+ RequestConfig config = RequestConfig .custom ().setProxy (httpProxy ).build ();
1009
+ httpPost .setConfig (config );
1010
+ }
1011
+
1012
+ StringEntity entity = new StringEntity (request .toString (), Consts .UTF_8 );
1013
+ httpPost .setEntity (entity );
1014
+ try (
1015
+ CloseableHttpResponse response = getHttpclient ().execute (httpPost )) {
1016
+ String responseContent = Utf8ResponseHandler .INSTANCE .handleResponse (response );
1017
+ XStream xstream = XStreamInitializer .getInstance ();
1018
+ xstream .processAnnotations (WxMpPayRefundResult .class );
1019
+ WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult ) xstream .fromXML (responseContent );
1020
+
1021
+ if (!"SUCCESS" .equalsIgnoreCase (wxMpPayRefundResult .getResultCode ())
1022
+ ||!"SUCCESS" .equalsIgnoreCase (wxMpPayRefundResult .getReturnCode ())) {
1023
+ WxError error = new WxError ();
1024
+ error .setErrorCode (-1 );
1025
+ error .setErrorMsg ("return_code:" + wxMpPayRefundResult .getReturnCode () +
1026
+ ";return_msg:" + wxMpPayRefundResult .getReturnMsg () +
1027
+ ";result_code:" + wxMpPayRefundResult .getResultCode () +
1028
+ ";err_code" + wxMpPayRefundResult .getErrCode () +
1029
+ ";err_code_des" + wxMpPayRefundResult .getErrCodeDes ());
1030
+ throw new WxErrorException (error );
1031
+ }
1032
+
1033
+ return wxMpPayRefundResult ;
1034
+ } catch (IOException e ) {
1035
+ log .error (MessageFormatter .format ("The exception was happened when sending refund '{}'." , request .toString ()).getMessage (), e );
1036
+ WxError error = new WxError ();
1037
+ error .setErrorCode (-1 );
1038
+ error .setErrorMsg ("incorrect response." );
1039
+ throw new WxErrorException (error );
1040
+ }
1041
+ }
1042
+
974
1043
@ Override
975
1044
public boolean checkJSSDKCallbackDataSignature (Map <String , String > kvm , String signature ) {
976
1045
return signature .equals (WxCryptUtil .createSign (kvm , wxMpConfigStorage .getPartnerKey ()));
@@ -1074,6 +1143,7 @@ public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
1074
1143
*
1075
1144
* @param optionalSignParam 参与签名的参数数组。
1076
1145
* 可以为下列字段:app_id, card_id, card_type, code, openid, location_id
1146
+ * </br>注意:当做wx.chooseCard调用时,必须传入app_id参与签名,否则会造成签名失败导致拉取卡券列表为空
1077
1147
* @return 卡券Api签名对象
1078
1148
*/
1079
1149
@ Override
@@ -1148,11 +1218,17 @@ public WxMpCardResult queryCardCode(String cardId, String code, boolean checkCon
1148
1218
* @throws WxErrorException
1149
1219
*/
1150
1220
@ Override
1151
- public void consumeCardCode (String code ) throws WxErrorException {
1221
+ public String consumeCardCode (String code , String cardId ) throws WxErrorException {
1152
1222
String url = "https://api.weixin.qq.com/card/code/consume" ;
1153
1223
JsonObject param = new JsonObject ();
1154
1224
param .addProperty ("code" , code );
1155
- post (url , param .toString ());
1225
+
1226
+ if (cardId != null && !"" .equals (cardId )) {
1227
+ param .addProperty ("card_id" , cardId );
1228
+ }
1229
+
1230
+ String responseContent = post (url , param .toString ());
1231
+ return responseContent ;
1156
1232
}
1157
1233
1158
1234
/**
@@ -1183,4 +1259,26 @@ public void markCardCode(String code, String cardId, String openId, boolean isMa
1183
1259
log .warn ("朋友的券mark失败:{}" , cardResult .getErrorMsg ());
1184
1260
}
1185
1261
}
1262
+
1263
+ @ Override
1264
+ public String getCardDetail (String cardId ) throws WxErrorException {
1265
+ String url = "https://api.weixin.qq.com/card/get" ;
1266
+ JsonObject param = new JsonObject ();
1267
+ param .addProperty ("card_id" , cardId );
1268
+ String responseContent = post (url , param .toString ());
1269
+
1270
+ // 判断返回值
1271
+ JsonObject json = (new JsonParser ()).parse (responseContent ).getAsJsonObject ();
1272
+ String errcode = json .get ("errcode" ).getAsString ();
1273
+ if (!"0" .equals (errcode )) {
1274
+ String errmsg = json .get ("errmsg" ).getAsString ();
1275
+ WxError error = new WxError ();
1276
+ error .setErrorCode (Integer .valueOf (errcode ));
1277
+ error .setErrorMsg (errmsg );
1278
+ throw new WxErrorException (error );
1279
+ }
1280
+
1281
+ return responseContent ;
1282
+ }
1283
+
1186
1284
}
0 commit comments