5656import me .chanjar .weixin .mp .bean .result .WxMpMaterialVideoInfoResult ;
5757import me .chanjar .weixin .mp .bean .result .WxMpOAuth2AccessToken ;
5858import me .chanjar .weixin .mp .bean .result .WxMpPayCallback ;
59+ import me .chanjar .weixin .mp .bean .result .WxMpPayRefundResult ;
5960import me .chanjar .weixin .mp .bean .result .WxMpPayResult ;
6061import me .chanjar .weixin .mp .bean .result .WxMpPrepayIdResult ;
6162import me .chanjar .weixin .mp .bean .result .WxMpQrCodeTicket ;
9798import com .google .gson .JsonArray ;
9899import com .google .gson .JsonElement ;
99100import com .google .gson .JsonObject ;
101+ import com .google .gson .JsonParser ;
100102import com .google .gson .internal .Streams ;
101103import com .google .gson .reflect .TypeToken ;
102104import com .google .gson .stream .JsonReader ;
@@ -881,7 +883,8 @@ private void checkParameters(Map<String, String> parameters) {
881883 }
882884
883885 @ 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 {
885888 Map <String , String > packageParams = new HashMap <String , String >();
886889 packageParams .put ("appid" , wxMpConfigStorage .getAppId ());
887890 packageParams .put ("mch_id" , wxMpConfigStorage .getPartnerId ());
@@ -897,8 +900,21 @@ public Map<String, String> getJSSDKPayInfo(String openId, String outTradeNo, dou
897900 }
898901
899902 @ Override
900- public Map <String , String > getJSSDKPayInfo (Map <String , String > parameters ) {
903+ public Map <String , String > getJSSDKPayInfo (Map <String , String > parameters ) throws WxErrorException {
901904 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+
902918 String prepayId = wxMpPrepayIdResult .getPrepay_id ();
903919 if (prepayId == null || prepayId .equals ("" )) {
904920 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) {
971987 return new WxMpPayCallback ();
972988 }
973989
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+
9741043 @ Override
9751044 public boolean checkJSSDKCallbackDataSignature (Map <String , String > kvm , String signature ) {
9761045 return signature .equals (WxCryptUtil .createSign (kvm , wxMpConfigStorage .getPartnerKey ()));
@@ -1074,6 +1143,7 @@ public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
10741143 *
10751144 * @param optionalSignParam 参与签名的参数数组。
10761145 * 可以为下列字段:app_id, card_id, card_type, code, openid, location_id
1146+ * </br>注意:当做wx.chooseCard调用时,必须传入app_id参与签名,否则会造成签名失败导致拉取卡券列表为空
10771147 * @return 卡券Api签名对象
10781148 */
10791149 @ Override
@@ -1148,11 +1218,17 @@ public WxMpCardResult queryCardCode(String cardId, String code, boolean checkCon
11481218 * @throws WxErrorException
11491219 */
11501220 @ Override
1151- public void consumeCardCode (String code ) throws WxErrorException {
1221+ public String consumeCardCode (String code , String cardId ) throws WxErrorException {
11521222 String url = "https://api.weixin.qq.com/card/code/consume" ;
11531223 JsonObject param = new JsonObject ();
11541224 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 ;
11561232 }
11571233
11581234 /**
@@ -1183,4 +1259,26 @@ public void markCardCode(String code, String cardId, String openId, boolean isMa
11831259 log .warn ("朋友的券mark失败:{}" , cardResult .getErrorMsg ());
11841260 }
11851261 }
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+
11861284}
0 commit comments