@@ -76,7 +76,7 @@ public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeN
7676 request .checkAndSign (this .getConfig ());
7777
7878 String url = this .getPayBaseUrl () + "/pay/refundquery" ;
79- String responseContent = this .post (url , request .toXML ());
79+ String responseContent = this .post (url , request .toXML (), true );
8080 WxPayRefundQueryResult result = WxPayBaseResult .fromXML (responseContent , WxPayRefundQueryResult .class );
8181 result .composeRefundRecords ();
8282 result .checkResult (this );
@@ -139,7 +139,7 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
139139 request .checkAndSign (this .getConfig ());
140140
141141 String url = this .getPayBaseUrl () + "/pay/orderquery" ;
142- String responseContent = this .post (url , request .toXML ());
142+ String responseContent = this .post (url , request .toXML (), true );
143143 if (StringUtils .isBlank (responseContent )) {
144144 throw new WxPayException ("无响应结果" );
145145 }
@@ -161,7 +161,7 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException
161161 request .checkAndSign (this .getConfig ());
162162
163163 String url = this .getPayBaseUrl () + "/pay/closeorder" ;
164- String responseContent = this .post (url , request .toXML ());
164+ String responseContent = this .post (url , request .toXML (), true );
165165 WxPayOrderCloseResult result = WxPayBaseResult .fromXML (responseContent , WxPayOrderCloseResult .class );
166166 result .checkResult (this );
167167
@@ -173,7 +173,7 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) th
173173 request .checkAndSign (this .getConfig ());
174174
175175 String url = this .getPayBaseUrl () + "/pay/unifiedorder" ;
176- String responseContent = this .post (url , request .toXML ());
176+ String responseContent = this .post (url , request .toXML (), true );
177177 WxPayUnifiedOrderResult result = WxPayBaseResult .fromXML (responseContent , WxPayUnifiedOrderResult .class );
178178 result .checkResult (this );
179179 return result ;
@@ -294,7 +294,7 @@ public void report(WxPayReportRequest request) throws WxPayException {
294294 request .checkAndSign (this .getConfig ());
295295
296296 String url = this .getPayBaseUrl () + "/payitil/report" ;
297- String responseContent = this .post (url , request .toXML ());
297+ String responseContent = this .post (url , request .toXML (), true );
298298 WxPayCommonResult result = WxPayBaseResult .fromXML (responseContent , WxPayCommonResult .class );
299299 result .checkResult (this );
300300 }
@@ -310,7 +310,7 @@ public WxPayBillResult downloadBill(String billDate, String billType, String tar
310310 request .checkAndSign (this .getConfig ());
311311
312312 String url = this .getPayBaseUrl () + "/pay/downloadbill" ;
313- String responseContent = this .post (url , request .toXML ());
313+ String responseContent = this .post (url , request .toXML (), true );
314314 if (responseContent .startsWith ("<" )) {
315315 WxPayCommonResult result = WxPayBaseResult .fromXML (responseContent , WxPayCommonResult .class );
316316 result .checkResult (this );
@@ -397,7 +397,7 @@ public WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxPayEx
397397 request .checkAndSign (this .getConfig ());
398398
399399 String url = this .getPayBaseUrl () + "/pay/micropay" ;
400- String responseContent = this .post (url , request .toXML ());
400+ String responseContent = this .post (url , request .toXML (), true );
401401 WxPayMicropayResult result = WxPayBaseResult .fromXML (responseContent , WxPayMicropayResult .class );
402402 result .checkResult (this );
403403 return result ;
@@ -419,7 +419,7 @@ public String shorturl(WxPayShorturlRequest request) throws WxPayException {
419419 request .checkAndSign (this .getConfig ());
420420
421421 String url = this .getPayBaseUrl () + "/tools/shorturl" ;
422- String responseContent = this .post (url , request .toXML ());
422+ String responseContent = this .post (url , request .toXML (), true );
423423 WxPayShorturlResult result = WxPayBaseResult .fromXML (responseContent , WxPayShorturlResult .class );
424424 result .checkResult (this );
425425 return result .getShortUrl ();
@@ -435,7 +435,7 @@ public String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxPayE
435435 request .checkAndSign (this .getConfig ());
436436
437437 String url = this .getPayBaseUrl () + "/tools/authcodetoopenid" ;
438- String responseContent = this .post (url , request .toXML ());
438+ String responseContent = this .post (url , request .toXML (), true );
439439 WxPayAuthcode2OpenidResult result = WxPayBaseResult .fromXML (responseContent , WxPayAuthcode2OpenidResult .class );
440440 result .checkResult (this );
441441 return result .getOpenid ();
@@ -446,7 +446,25 @@ public String authcode2Openid(String authCode) throws WxPayException {
446446 return this .authcode2Openid (new WxPayAuthcode2OpenidRequest (authCode ));
447447 }
448448
449- private String post (String url , String xmlParam ) {
449+ @ Override
450+ public String getSandboxSignKey () throws WxPayException {
451+ WxPayDefaultRequest request = new WxPayDefaultRequest ();
452+ request .checkAndSign (this .getConfig ());
453+
454+ String url = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey" ;
455+ String responseContent = this .post (url , request .toXML (), false );
456+ WxPaySandboxSignKeyResult result = WxPayBaseResult .fromXML (responseContent , WxPaySandboxSignKeyResult .class );
457+ result .checkResult (this );
458+ return result .getSandboxSignKey ();
459+ }
460+
461+ /**
462+ * @param url 请求地址
463+ * @param xmlParam 请求字符串
464+ * @param needTransferEncoding 是否需要对结果进行重编码
465+ * @return 返回请求结果
466+ */
467+ private String post (String url , String xmlParam , boolean needTransferEncoding ) {
450468 String requestString = xmlParam ;
451469 try {
452470 requestString = new String (xmlParam .getBytes (CharEncoding .UTF_8 ), CharEncoding .ISO_8859_1 );
@@ -457,11 +475,14 @@ private String post(String url, String xmlParam) {
457475
458476 HttpRequest request = HttpRequest .post (url ).body (requestString );
459477 HttpResponse response = request .send ();
460- String responseString = null ;
461- try {
462- responseString = new String (response .bodyText ().getBytes (CharEncoding .ISO_8859_1 ), CharEncoding .UTF_8 );
463- } catch (UnsupportedEncodingException e ) {
464- e .printStackTrace ();
478+ String responseString = response .bodyText ();
479+
480+ if (needTransferEncoding ) {
481+ try {
482+ responseString = new String (response .bodyText ().getBytes (CharEncoding .ISO_8859_1 ), CharEncoding .UTF_8 );
483+ } catch (UnsupportedEncodingException e ) {
484+ e .printStackTrace ();
485+ }
465486 }
466487
467488 this .log .debug ("\n [URL]: {}\n [PARAMS]: {}\n [RESPONSE]: {}" , url , xmlParam , responseString );
0 commit comments