Skip to content

Commit c3cb050

Browse files
committed
优化简化微信支付请求代码
1 parent d3c691a commit c3cb050

File tree

1 file changed

+30
-45
lines changed

1 file changed

+30
-45
lines changed

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

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public WxPayRefundResult refund(WxPayRefundRequest request) throws WxPayExceptio
5757
request.checkAndSign(this.getConfig());
5858

5959
String url = this.getPayBaseUrl() + "/secapi/pay/refund";
60-
String responseContent = this.postWithKey(url, request.toXML());
60+
String responseContent = this.post(url, request.toXML(), true);
6161
WxPayRefundResult result = WxPayBaseResult.fromXML(responseContent, WxPayRefundResult.class);
6262
result.checkResult(this);
6363
return result;
@@ -75,7 +75,7 @@ public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeN
7575
request.checkAndSign(this.getConfig());
7676

7777
String url = this.getPayBaseUrl() + "/pay/refundquery";
78-
String responseContent = this.post(url, request.toXML());
78+
String responseContent = this.post(url, request.toXML(), false);
7979
WxPayRefundQueryResult result = WxPayBaseResult.fromXML(responseContent, WxPayRefundQueryResult.class);
8080
result.composeRefundRecords();
8181
result.checkResult(this);
@@ -109,7 +109,7 @@ public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request) throw
109109
url = this.getPayBaseUrl() + "/mmpaymkttransfers/sendgroupredpack";
110110
}
111111

112-
String responseContent = this.postWithKey(url, request.toXML());
112+
String responseContent = this.post(url, request.toXML(), true);
113113
WxPaySendRedpackResult result = WxPayBaseResult.fromXML(responseContent, WxPaySendRedpackResult.class);
114114
//毋须校验,因为没有返回签名信息
115115
// this.checkResult(result);
@@ -124,7 +124,7 @@ public WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxPayExcept
124124
request.checkAndSign(this.getConfig());
125125

126126
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/gethbinfo";
127-
String responseContent = this.postWithKey(url, request.toXML());
127+
String responseContent = this.post(url, request.toXML(), true);
128128
WxPayRedpackQueryResult result = WxPayBaseResult.fromXML(responseContent, WxPayRedpackQueryResult.class);
129129
result.checkResult(this);
130130
return result;
@@ -138,7 +138,7 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
138138
request.checkAndSign(this.getConfig());
139139

140140
String url = this.getPayBaseUrl() + "/pay/orderquery";
141-
String responseContent = this.post(url, request.toXML());
141+
String responseContent = this.post(url, request.toXML(), false);
142142
if (StringUtils.isBlank(responseContent)) {
143143
throw new WxPayException("无响应结果");
144144
}
@@ -160,7 +160,7 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException
160160
request.checkAndSign(this.getConfig());
161161

162162
String url = this.getPayBaseUrl() + "/pay/closeorder";
163-
String responseContent = this.post(url, request.toXML());
163+
String responseContent = this.post(url, request.toXML(), false);
164164
WxPayOrderCloseResult result = WxPayBaseResult.fromXML(responseContent, WxPayOrderCloseResult.class);
165165
result.checkResult(this);
166166

@@ -172,7 +172,7 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) th
172172
request.checkAndSign(this.getConfig());
173173

174174
String url = this.getPayBaseUrl() + "/pay/unifiedorder";
175-
String responseContent = this.post(url, request.toXML());
175+
String responseContent = this.post(url, request.toXML(), false);
176176
WxPayUnifiedOrderResult result = WxPayBaseResult.fromXML(responseContent, WxPayUnifiedOrderResult.class);
177177
result.checkResult(this);
178178
return result;
@@ -227,7 +227,7 @@ public WxEntPayResult entPay(WxEntPayRequest request) throws WxPayException {
227227
request.checkAndSign(this.getConfig());
228228
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/promotion/transfers";
229229

230-
String responseContent = this.postWithKey(url, request.toXML());
230+
String responseContent = this.post(url, request.toXML(), true);
231231
WxEntPayResult result = WxPayBaseResult.fromXML(responseContent, WxEntPayResult.class);
232232
result.checkResult(this);
233233
return result;
@@ -240,7 +240,7 @@ public WxEntPayQueryResult queryEntPay(String partnerTradeNo) throws WxPayExcept
240240
request.checkAndSign(this.getConfig());
241241

242242
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/gettransferinfo";
243-
String responseContent = this.postWithKey(url, request.toXML());
243+
String responseContent = this.post(url, request.toXML(), true);
244244
WxEntPayQueryResult result = WxPayBaseResult.fromXML(responseContent, WxEntPayQueryResult.class);
245245
result.checkResult(this);
246246
return result;
@@ -293,7 +293,7 @@ public void report(WxPayReportRequest request) throws WxPayException {
293293
request.checkAndSign(this.getConfig());
294294

295295
String url = this.getPayBaseUrl() + "/payitil/report";
296-
String responseContent = this.post(url, request.toXML());
296+
String responseContent = this.post(url, request.toXML(), false);
297297
WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class);
298298
result.checkResult(this);
299299
}
@@ -309,7 +309,7 @@ public WxPayBillResult downloadBill(String billDate, String billType, String tar
309309
request.checkAndSign(this.getConfig());
310310

311311
String url = this.getPayBaseUrl() + "/pay/downloadbill";
312-
String responseContent = this.post(url, request.toXML());
312+
String responseContent = this.post(url, request.toXML(), false);
313313
if (responseContent.startsWith("<")) {
314314
WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class);
315315
result.checkResult(this);
@@ -396,7 +396,7 @@ public WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxPayEx
396396
request.checkAndSign(this.getConfig());
397397

398398
String url = this.getPayBaseUrl() + "/pay/micropay";
399-
String responseContent = this.post(url, request.toXML());
399+
String responseContent = this.post(url, request.toXML(), false);
400400
WxPayMicropayResult result = WxPayBaseResult.fromXML(responseContent, WxPayMicropayResult.class);
401401
result.checkResult(this);
402402
return result;
@@ -407,7 +407,7 @@ public WxPayOrderReverseResult reverseOrder(WxPayOrderReverseRequest request) th
407407
request.checkAndSign(this.getConfig());
408408

409409
String url = this.getPayBaseUrl() + "/secapi/pay/reverse";
410-
String responseContent = this.postWithKey(url, request.toXML());
410+
String responseContent = this.post(url, request.toXML(), true);
411411
WxPayOrderReverseResult result = WxPayBaseResult.fromXML(responseContent, WxPayOrderReverseResult.class);
412412
result.checkResult(this);
413413
return result;
@@ -418,7 +418,7 @@ public String shorturl(WxPayShorturlRequest request) throws WxPayException {
418418
request.checkAndSign(this.getConfig());
419419

420420
String url = this.getPayBaseUrl() + "/tools/shorturl";
421-
String responseContent = this.post(url, request.toXML());
421+
String responseContent = this.post(url, request.toXML(), false);
422422
WxPayShorturlResult result = WxPayBaseResult.fromXML(responseContent, WxPayShorturlResult.class);
423423
result.checkResult(this);
424424
return result.getShortUrl();
@@ -434,7 +434,7 @@ public String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxPayE
434434
request.checkAndSign(this.getConfig());
435435

436436
String url = this.getPayBaseUrl() + "/tools/authcodetoopenid";
437-
String responseContent = this.post(url, request.toXML());
437+
String responseContent = this.post(url, request.toXML(), false);
438438
WxPayAuthcode2OpenidResult result = WxPayBaseResult.fromXML(responseContent, WxPayAuthcode2OpenidResult.class);
439439
result.checkResult(this);
440440
return result.getOpenid();
@@ -451,57 +451,42 @@ public String getSandboxSignKey() throws WxPayException {
451451
request.checkAndSign(this.getConfig());
452452

453453
String url = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey";
454-
String responseContent = this.post(url, request.toXML());
454+
String responseContent = this.post(url, request.toXML(), false);
455455
WxPaySandboxSignKeyResult result = WxPayBaseResult.fromXML(responseContent, WxPaySandboxSignKeyResult.class);
456456
result.checkResult(this);
457457
return result.getSandboxSignKey();
458458
}
459459

460460
/**
461-
* 执行post请求
462-
*
463-
* @param url 请求地址
464-
* @param xmlParam 请求字符串
465-
* @return 返回请求结果
466-
*/
467-
private String post(String url, String xmlParam) throws WxPayException {
468-
HttpRequest request = HttpRequest
469-
.post(url)
470-
.timeout(this.config.getHttpTimeout())
471-
.connectionTimeout(this.config.getHttpConnectionTimeout())
472-
.body(new String(xmlParam.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
473-
String responseString = this.getResponseString(request.send());
474-
475-
this.log.info("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", url, xmlParam, responseString);
476-
return responseString;
477-
}
478-
479-
/**
480-
* 带证书发送post请求
461+
* 发送post请求
481462
*
482463
* @param url 请求地址
483464
* @param requestStr 请求信息
465+
* @param useKey 是否使用证书
466+
* @return 返回请求结果字符串
484467
*/
485-
private String postWithKey(String url, String requestStr) throws WxPayException {
468+
private String post(String url, String requestStr, boolean useKey) throws WxPayException {
486469
try {
487-
SSLContext sslContext = this.getConfig().getSslContext();
488-
if (null == sslContext) {
489-
sslContext = this.getConfig().initSSLContext();
490-
}
491-
492470
HttpRequest request = HttpRequest
493471
.post(url)
494472
.timeout(this.config.getHttpTimeout())
495473
.connectionTimeout(this.config.getHttpConnectionTimeout())
496-
.withConnectionProvider(new SSLSocketHttpConnectionProvider(sslContext))
497474
.bodyText(requestStr);
498475

476+
if (useKey) {
477+
SSLContext sslContext = this.getConfig().getSslContext();
478+
if (null == sslContext) {
479+
sslContext = this.getConfig().initSSLContext();
480+
}
481+
request.withConnectionProvider(new SSLSocketHttpConnectionProvider(sslContext));
482+
}
483+
499484
String responseString = this.getResponseString(request.send());
500485

501-
this.log.info("\n【请求地址】: {}\n请求参数】:{}\n【响应数据】:{}", url, requestStr, responseString);
486+
this.log.info("\n【请求地址】{}\n请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
502487
return responseString;
503488
} catch (Exception e) {
504-
this.log.error("\n【请求地址】: {}\n请求参数】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
489+
this.log.error("\n【请求地址】{}\n请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
505490
throw new WxPayException(e.getMessage(), e);
506491
}
507492
}

0 commit comments

Comments
 (0)