Skip to content

Commit 61d9331

Browse files
committed
优化微信支付请求代码
1 parent c494fbe commit 61d9331

File tree

3 files changed

+41
-39
lines changed

3 files changed

+41
-39
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayBaseRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected void checkFields() throws WxPayException {
119119
try {
120120
BeanUtils.checkRequiredFields(this);
121121
} catch (WxErrorException e) {
122-
throw new WxPayException(e.getError().getErrorMsg());
122+
throw new WxPayException(e.getError().getErrorMsg(), e);
123123
}
124124

125125
//check other parameters

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
import jodd.http.HttpRequest;
1212
import jodd.http.HttpResponse;
1313
import jodd.http.net.SSLSocketHttpConnectionProvider;
14-
import org.apache.commons.lang3.CharEncoding;
1514
import org.apache.commons.lang3.StringUtils;
1615
import org.slf4j.Logger;
1716
import org.slf4j.LoggerFactory;
1817

1918
import javax.net.ssl.SSLContext;
2019
import java.io.File;
21-
import java.io.UnsupportedEncodingException;
20+
import java.nio.charset.StandardCharsets;
2221
import java.util.HashMap;
2322
import java.util.LinkedList;
2423
import java.util.List;
@@ -76,7 +75,7 @@ public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeN
7675
request.checkAndSign(this.getConfig());
7776

7877
String url = this.getPayBaseUrl() + "/pay/refundquery";
79-
String responseContent = this.post(url, request.toXML(), true);
78+
String responseContent = this.post(url, request.toXML());
8079
WxPayRefundQueryResult result = WxPayBaseResult.fromXML(responseContent, WxPayRefundQueryResult.class);
8180
result.composeRefundRecords();
8281
result.checkResult(this);
@@ -96,7 +95,7 @@ public WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxPayE
9695
throw e;
9796
} catch (Exception e) {
9897
log.error(e.getMessage(), e);
99-
throw new WxPayException("发生异常," + e.getMessage());
98+
throw new WxPayException("发生异常," + e.getMessage(), e);
10099
}
101100
}
102101

@@ -139,7 +138,7 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
139138
request.checkAndSign(this.getConfig());
140139

141140
String url = this.getPayBaseUrl() + "/pay/orderquery";
142-
String responseContent = this.post(url, request.toXML(), true);
141+
String responseContent = this.post(url, request.toXML());
143142
if (StringUtils.isBlank(responseContent)) {
144143
throw new WxPayException("无响应结果");
145144
}
@@ -161,7 +160,7 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException
161160
request.checkAndSign(this.getConfig());
162161

163162
String url = this.getPayBaseUrl() + "/pay/closeorder";
164-
String responseContent = this.post(url, request.toXML(), true);
163+
String responseContent = this.post(url, request.toXML());
165164
WxPayOrderCloseResult result = WxPayBaseResult.fromXML(responseContent, WxPayOrderCloseResult.class);
166165
result.checkResult(this);
167166

@@ -173,7 +172,7 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) th
173172
request.checkAndSign(this.getConfig());
174173

175174
String url = this.getPayBaseUrl() + "/pay/unifiedorder";
176-
String responseContent = this.post(url, request.toXML(), true);
175+
String responseContent = this.post(url, request.toXML());
177176
WxPayUnifiedOrderResult result = WxPayBaseResult.fromXML(responseContent, WxPayUnifiedOrderResult.class);
178177
result.checkResult(this);
179178
return result;
@@ -294,7 +293,7 @@ public void report(WxPayReportRequest request) throws WxPayException {
294293
request.checkAndSign(this.getConfig());
295294

296295
String url = this.getPayBaseUrl() + "/payitil/report";
297-
String responseContent = this.post(url, request.toXML(), true);
296+
String responseContent = this.post(url, request.toXML());
298297
WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class);
299298
result.checkResult(this);
300299
}
@@ -310,7 +309,7 @@ public WxPayBillResult downloadBill(String billDate, String billType, String tar
310309
request.checkAndSign(this.getConfig());
311310

312311
String url = this.getPayBaseUrl() + "/pay/downloadbill";
313-
String responseContent = this.post(url, request.toXML(), true);
312+
String responseContent = this.post(url, request.toXML());
314313
if (responseContent.startsWith("<")) {
315314
WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class);
316315
result.checkResult(this);
@@ -397,7 +396,7 @@ public WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxPayEx
397396
request.checkAndSign(this.getConfig());
398397

399398
String url = this.getPayBaseUrl() + "/pay/micropay";
400-
String responseContent = this.post(url, request.toXML(), true);
399+
String responseContent = this.post(url, request.toXML());
401400
WxPayMicropayResult result = WxPayBaseResult.fromXML(responseContent, WxPayMicropayResult.class);
402401
result.checkResult(this);
403402
return result;
@@ -419,7 +418,7 @@ public String shorturl(WxPayShorturlRequest request) throws WxPayException {
419418
request.checkAndSign(this.getConfig());
420419

421420
String url = this.getPayBaseUrl() + "/tools/shorturl";
422-
String responseContent = this.post(url, request.toXML(), true);
421+
String responseContent = this.post(url, request.toXML());
423422
WxPayShorturlResult result = WxPayBaseResult.fromXML(responseContent, WxPayShorturlResult.class);
424423
result.checkResult(this);
425424
return result.getShortUrl();
@@ -435,7 +434,7 @@ public String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxPayE
435434
request.checkAndSign(this.getConfig());
436435

437436
String url = this.getPayBaseUrl() + "/tools/authcodetoopenid";
438-
String responseContent = this.post(url, request.toXML(), true);
437+
String responseContent = this.post(url, request.toXML());
439438
WxPayAuthcode2OpenidResult result = WxPayBaseResult.fromXML(responseContent, WxPayAuthcode2OpenidResult.class);
440439
result.checkResult(this);
441440
return result.getOpenid();
@@ -452,26 +451,21 @@ public String getSandboxSignKey() throws WxPayException {
452451
request.checkAndSign(this.getConfig());
453452

454453
String url = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey";
455-
String responseContent = this.post(url, request.toXML(), false);
454+
String responseContent = this.post(url, request.toXML());
456455
WxPaySandboxSignKeyResult result = WxPayBaseResult.fromXML(responseContent, WxPaySandboxSignKeyResult.class);
457456
result.checkResult(this);
458457
return result.getSandboxSignKey();
459458
}
460459

461460
/**
462-
* @param url 请求地址
463-
* @param xmlParam 请求字符串
464-
* @param needTransferEncoding 是否需要对结果进行重编码
461+
* 执行post请求
462+
*
463+
* @param url 请求地址
464+
* @param xmlParam 请求字符串
465465
* @return 返回请求结果
466466
*/
467-
private String post(String url, String xmlParam, boolean needTransferEncoding) {
468-
String requestString = xmlParam;
469-
try {
470-
requestString = new String(xmlParam.getBytes(CharEncoding.UTF_8), CharEncoding.ISO_8859_1);
471-
} catch (UnsupportedEncodingException e) {
472-
//实际上不会发生该异常
473-
e.printStackTrace();
474-
}
467+
private String post(String url, String xmlParam) throws WxPayException {
468+
String requestString = new String(xmlParam.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
475469

476470
HttpRequest request = HttpRequest.post(url).body(requestString);
477471
String responseString = this.getResponseString(request.send());
@@ -481,7 +475,10 @@ private String post(String url, String xmlParam, boolean needTransferEncoding) {
481475
}
482476

483477
/**
484-
* ecoolper(20170418),修改为jodd-http方式
478+
* 带证书发送post请求
479+
*
480+
* @param url 请求地址
481+
* @param requestStr 请求信息
485482
*/
486483
private String postWithKey(String url, String requestStr) throws WxPayException {
487484
try {
@@ -501,22 +498,27 @@ private String postWithKey(String url, String requestStr) throws WxPayException
501498
return responseString;
502499
} catch (Exception e) {
503500
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
504-
throw new WxPayException(e.getMessage());
501+
throw new WxPayException(e.getMessage(), e);
505502
}
506503
}
507504

508-
private String getResponseString(HttpResponse response) {
509-
this.log.debug("【微信服务器响应头信息】:\n{}", response.toString(false));
505+
private String getResponseString(HttpResponse response) throws WxPayException {
506+
try {
507+
this.log.debug("【微信服务器响应头信息】:\n{}", response.toString(false));
508+
} catch (NullPointerException e) {
509+
throw new WxPayException("response.toString() 居然抛出空指针异常了", e);
510+
}
510511

511512
String responseString = response.bodyText();
512513

514+
if (StringUtils.isBlank(responseString)) {
515+
throw new WxPayException("响应信息为空");
516+
}
517+
513518
if (StringUtils.isBlank(response.charset())) {
514-
try {
515-
responseString = new String(response.bodyText().getBytes(CharEncoding.ISO_8859_1), CharEncoding.UTF_8);
516-
} catch (UnsupportedEncodingException e) {
517-
e.printStackTrace();
518-
}
519+
responseString = new String(responseString.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
519520
}
521+
520522
return responseString;
521523
}
522524

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void testRefund() throws Exception {
8383
}
8484

8585
/**
86-
* Test method for {@link WxPayService#refundQuery(java.lang.String, java.lang.String, java.lang.String, java.lang.String)} .
86+
* Test method for {@link WxPayService#refundQuery(String, String, String, String)} .
8787
*/
8888
@Test
8989
public void testRefundQuery() throws Exception {
@@ -121,7 +121,7 @@ public void testSendRedpack() throws Exception {
121121
}
122122

123123
/**
124-
* Test method for {@link WxPayService#queryRedpack(java.lang.String)}.
124+
* Test method for {@link WxPayService#queryRedpack(String)}.
125125
*/
126126
@Test
127127
public void testQueryRedpack() throws Exception {
@@ -148,7 +148,7 @@ public void testUnifiedOrder() throws WxPayException {
148148
}
149149

150150
/**
151-
* Test method for {@link WxPayService#queryOrder(java.lang.String, java.lang.String)} .
151+
* Test method for {@link WxPayService#queryOrder(String, String)} .
152152
*/
153153
@Test
154154
public void testQueryOrder() throws WxPayException {
@@ -157,7 +157,7 @@ public void testQueryOrder() throws WxPayException {
157157
}
158158

159159
/**
160-
* Test method for {@link WxPayService#closeOrder(java.lang.String)} .
160+
* Test method for {@link WxPayService#closeOrder(String)} .
161161
*/
162162
@Test
163163
public void testCloseOrder() throws WxPayException {
@@ -174,7 +174,7 @@ public void testEntPay() throws WxPayException {
174174
}
175175

176176
/**
177-
* Test method for {@link WxPayService#queryEntPay(java.lang.String)}.
177+
* Test method for {@link WxPayService#queryEntPay(String)}.
178178
*/
179179
@Test
180180
public void testQueryEntPay() throws WxPayException {

0 commit comments

Comments
 (0)