@@ -76,7 +76,7 @@ public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeN
76
76
request .checkAndSign (this .getConfig ());
77
77
78
78
String url = this .getPayBaseUrl () + "/pay/refundquery" ;
79
- String responseContent = this .post (url , request .toXML ());
79
+ String responseContent = this .post (url , request .toXML (), true );
80
80
WxPayRefundQueryResult result = WxPayBaseResult .fromXML (responseContent , WxPayRefundQueryResult .class );
81
81
result .composeRefundRecords ();
82
82
result .checkResult (this );
@@ -139,7 +139,7 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
139
139
request .checkAndSign (this .getConfig ());
140
140
141
141
String url = this .getPayBaseUrl () + "/pay/orderquery" ;
142
- String responseContent = this .post (url , request .toXML ());
142
+ String responseContent = this .post (url , request .toXML (), true );
143
143
if (StringUtils .isBlank (responseContent )) {
144
144
throw new WxPayException ("无响应结果" );
145
145
}
@@ -161,7 +161,7 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException
161
161
request .checkAndSign (this .getConfig ());
162
162
163
163
String url = this .getPayBaseUrl () + "/pay/closeorder" ;
164
- String responseContent = this .post (url , request .toXML ());
164
+ String responseContent = this .post (url , request .toXML (), true );
165
165
WxPayOrderCloseResult result = WxPayBaseResult .fromXML (responseContent , WxPayOrderCloseResult .class );
166
166
result .checkResult (this );
167
167
@@ -173,7 +173,7 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) th
173
173
request .checkAndSign (this .getConfig ());
174
174
175
175
String url = this .getPayBaseUrl () + "/pay/unifiedorder" ;
176
- String responseContent = this .post (url , request .toXML ());
176
+ String responseContent = this .post (url , request .toXML (), true );
177
177
WxPayUnifiedOrderResult result = WxPayBaseResult .fromXML (responseContent , WxPayUnifiedOrderResult .class );
178
178
result .checkResult (this );
179
179
return result ;
@@ -294,7 +294,7 @@ public void report(WxPayReportRequest request) throws WxPayException {
294
294
request .checkAndSign (this .getConfig ());
295
295
296
296
String url = this .getPayBaseUrl () + "/payitil/report" ;
297
- String responseContent = this .post (url , request .toXML ());
297
+ String responseContent = this .post (url , request .toXML (), true );
298
298
WxPayCommonResult result = WxPayBaseResult .fromXML (responseContent , WxPayCommonResult .class );
299
299
result .checkResult (this );
300
300
}
@@ -310,7 +310,7 @@ public WxPayBillResult downloadBill(String billDate, String billType, String tar
310
310
request .checkAndSign (this .getConfig ());
311
311
312
312
String url = this .getPayBaseUrl () + "/pay/downloadbill" ;
313
- String responseContent = this .post (url , request .toXML ());
313
+ String responseContent = this .post (url , request .toXML (), true );
314
314
if (responseContent .startsWith ("<" )) {
315
315
WxPayCommonResult result = WxPayBaseResult .fromXML (responseContent , WxPayCommonResult .class );
316
316
result .checkResult (this );
@@ -397,7 +397,7 @@ public WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxPayEx
397
397
request .checkAndSign (this .getConfig ());
398
398
399
399
String url = this .getPayBaseUrl () + "/pay/micropay" ;
400
- String responseContent = this .post (url , request .toXML ());
400
+ String responseContent = this .post (url , request .toXML (), true );
401
401
WxPayMicropayResult result = WxPayBaseResult .fromXML (responseContent , WxPayMicropayResult .class );
402
402
result .checkResult (this );
403
403
return result ;
@@ -419,7 +419,7 @@ public String shorturl(WxPayShorturlRequest request) throws WxPayException {
419
419
request .checkAndSign (this .getConfig ());
420
420
421
421
String url = this .getPayBaseUrl () + "/tools/shorturl" ;
422
- String responseContent = this .post (url , request .toXML ());
422
+ String responseContent = this .post (url , request .toXML (), true );
423
423
WxPayShorturlResult result = WxPayBaseResult .fromXML (responseContent , WxPayShorturlResult .class );
424
424
result .checkResult (this );
425
425
return result .getShortUrl ();
@@ -435,7 +435,7 @@ public String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxPayE
435
435
request .checkAndSign (this .getConfig ());
436
436
437
437
String url = this .getPayBaseUrl () + "/tools/authcodetoopenid" ;
438
- String responseContent = this .post (url , request .toXML ());
438
+ String responseContent = this .post (url , request .toXML (), true );
439
439
WxPayAuthcode2OpenidResult result = WxPayBaseResult .fromXML (responseContent , WxPayAuthcode2OpenidResult .class );
440
440
result .checkResult (this );
441
441
return result .getOpenid ();
@@ -446,7 +446,25 @@ public String authcode2Openid(String authCode) throws WxPayException {
446
446
return this .authcode2Openid (new WxPayAuthcode2OpenidRequest (authCode ));
447
447
}
448
448
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 ) {
450
468
String requestString = xmlParam ;
451
469
try {
452
470
requestString = new String (xmlParam .getBytes (CharEncoding .UTF_8 ), CharEncoding .ISO_8859_1 );
@@ -457,11 +475,14 @@ private String post(String url, String xmlParam) {
457
475
458
476
HttpRequest request = HttpRequest .post (url ).body (requestString );
459
477
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
+ }
465
486
}
466
487
467
488
this .log .debug ("\n [URL]: {}\n [PARAMS]: {}\n [RESPONSE]: {}" , url , xmlParam , responseString );
0 commit comments