Skip to content

Commit 57eab45

Browse files
committed
优化微信支付代码,避免过度检查
1 parent 6cabfc3 commit 57eab45

2 files changed

Lines changed: 45 additions & 40 deletions

File tree

‎weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayBaseResult.java‎

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -215,38 +215,42 @@ protected Integer getXmlValueAsInt(String... path) {
215215

216216
/**
217217
* 校验返回结果签名
218+
*
219+
* @param checkSuccess 是否同时检查结果是否成功
218220
*/
219-
public void checkResult(WxPayServiceAbstractImpl wxPayService) throws WxPayException {
221+
public void checkResult(WxPayServiceAbstractImpl wxPayService, boolean checkSuccess) throws WxPayException {
220222
//校验返回结果签名
221223
Map<String, String> map = toMap();
222224
if (getSign() != null && !SignUtils.checkSign(map, wxPayService.getConfig().getMchKey())) {
223225
this.getLogger().debug("校验结果签名失败,参数:{}", map);
224226
throw new WxPayException("参数格式校验错误!");
225227
}
226228

227-
List<String> successStrings = Lists.newArrayList("SUCCESS", "");
228229
//校验结果是否成功
229-
if (!successStrings.contains(StringUtils.trimToEmpty(getReturnCode()).toUpperCase())
230-
|| !successStrings.contains(StringUtils.trimToEmpty(getResultCode()).toUpperCase())) {
231-
StringBuilder errorMsg = new StringBuilder();
232-
if (getReturnCode() != null) {
233-
errorMsg.append("返回代码:").append(getReturnCode());
234-
}
235-
if (getReturnMsg() != null) {
236-
errorMsg.append(",返回信息:").append(getReturnMsg());
237-
}
238-
if (getResultCode() != null) {
239-
errorMsg.append(",结果代码:").append(getResultCode());
240-
}
241-
if (getErrCode() != null) {
242-
errorMsg.append(",错误代码:").append(getErrCode());
243-
}
244-
if (getErrCodeDes() != null) {
245-
errorMsg.append(",错误详情:").append(getErrCodeDes());
246-
}
230+
if (checkSuccess) {
231+
List<String> successStrings = Lists.newArrayList("SUCCESS", "");
232+
if (!successStrings.contains(StringUtils.trimToEmpty(getReturnCode()).toUpperCase())
233+
|| !successStrings.contains(StringUtils.trimToEmpty(getResultCode()).toUpperCase())) {
234+
StringBuilder errorMsg = new StringBuilder();
235+
if (getReturnCode() != null) {
236+
errorMsg.append("返回代码:").append(getReturnCode());
237+
}
238+
if (getReturnMsg() != null) {
239+
errorMsg.append(",返回信息:").append(getReturnMsg());
240+
}
241+
if (getResultCode() != null) {
242+
errorMsg.append(",结果代码:").append(getResultCode());
243+
}
244+
if (getErrCode() != null) {
245+
errorMsg.append(",错误代码:").append(getErrCode());
246+
}
247+
if (getErrCodeDes() != null) {
248+
errorMsg.append(",错误详情:").append(getErrCodeDes());
249+
}
247250

248-
this.getLogger().error("\n结果业务代码异常,返回結果:{},\n{}", map, errorMsg.toString());
249-
throw WxPayException.from(this);
251+
this.getLogger().error("\n结果业务代码异常,返回結果:{},\n{}", map, errorMsg.toString());
252+
throw WxPayException.from(this);
253+
}
250254
}
251255
}
252256
}

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public WxPayRefundResult refund(WxPayRefundRequest request) throws WxPayExceptio
7777
String url = this.getPayBaseUrl() + "/secapi/pay/refund";
7878
String responseContent = this.post(url, request.toXML(), true);
7979
WxPayRefundResult result = WxPayBaseResult.fromXML(responseContent, WxPayRefundResult.class);
80-
result.checkResult(this);
80+
result.checkResult(this, true);
8181
return result;
8282
}
8383

@@ -96,7 +96,7 @@ public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeN
9696
String responseContent = this.post(url, request.toXML(), false);
9797
WxPayRefundQueryResult result = WxPayBaseResult.fromXML(responseContent, WxPayRefundQueryResult.class);
9898
result.composeRefundRecords();
99-
result.checkResult(this);
99+
result.checkResult(this, true);
100100
return result;
101101
}
102102

@@ -112,7 +112,7 @@ public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) throws WxPa
112112
log.debug("微信支付异步通知请求参数:{}", xmlData);
113113
WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlData);
114114
log.debug("微信支付异步通知请求解析后的对象:{}", result);
115-
result.checkResult(this);
115+
result.checkResult(this, false);
116116
return result;
117117
} catch (WxPayException e) {
118118
log.error(e.getMessage(), e);
@@ -163,7 +163,7 @@ public WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxPayExcept
163163
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/gethbinfo";
164164
String responseContent = this.post(url, request.toXML(), true);
165165
WxPayRedpackQueryResult result = WxPayBaseResult.fromXML(responseContent, WxPayRedpackQueryResult.class);
166-
result.checkResult(this);
166+
result.checkResult(this, true);
167167
return result;
168168
}
169169

@@ -182,7 +182,7 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
182182

183183
WxPayOrderQueryResult result = WxPayBaseResult.fromXML(responseContent, WxPayOrderQueryResult.class);
184184
result.composeCoupons();
185-
result.checkResult(this);
185+
result.checkResult(this, true);
186186
return result;
187187
}
188188

@@ -199,7 +199,7 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException
199199
String url = this.getPayBaseUrl() + "/pay/closeorder";
200200
String responseContent = this.post(url, request.toXML(), false);
201201
WxPayOrderCloseResult result = WxPayBaseResult.fromXML(responseContent, WxPayOrderCloseResult.class);
202-
result.checkResult(this);
202+
result.checkResult(this, true);
203203

204204
return result;
205205
}
@@ -272,11 +272,12 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) th
272272
String url = this.getPayBaseUrl() + "/pay/unifiedorder";
273273
String responseContent = this.post(url, request.toXML(), false);
274274
WxPayUnifiedOrderResult result = WxPayBaseResult.fromXML(responseContent, WxPayUnifiedOrderResult.class);
275-
result.checkResult(this);
275+
result.checkResult(this, true);
276276
return result;
277277
}
278278

279279
@Override
280+
@Deprecated
280281
public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxPayException {
281282
WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
282283
String prepayId = unifiedOrderResult.getPrepayId();
@@ -331,7 +332,7 @@ public WxEntPayResult entPay(WxEntPayRequest request) throws WxPayException {
331332

332333
String responseContent = this.post(url, request.toXML(), true);
333334
WxEntPayResult result = WxPayBaseResult.fromXML(responseContent, WxEntPayResult.class);
334-
result.checkResult(this);
335+
result.checkResult(this, true);
335336
return result;
336337
}
337338

@@ -344,7 +345,7 @@ public WxEntPayQueryResult queryEntPay(String partnerTradeNo) throws WxPayExcept
344345
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/gettransferinfo";
345346
String responseContent = this.post(url, request.toXML(), true);
346347
WxEntPayQueryResult result = WxPayBaseResult.fromXML(responseContent, WxEntPayQueryResult.class);
347-
result.checkResult(this);
348+
result.checkResult(this, true);
348349
return result;
349350
}
350351

@@ -396,7 +397,7 @@ public void report(WxPayReportRequest request) throws WxPayException {
396397
String url = this.getPayBaseUrl() + "/payitil/report";
397398
String responseContent = this.post(url, request.toXML(), false);
398399
WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class);
399-
result.checkResult(this);
400+
result.checkResult(this, true);
400401
}
401402

402403
@Override
@@ -494,7 +495,7 @@ public WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxPayEx
494495
String url = this.getPayBaseUrl() + "/pay/micropay";
495496
String responseContent = this.post(url, request.toXML(), false);
496497
WxPayMicropayResult result = WxPayBaseResult.fromXML(responseContent, WxPayMicropayResult.class);
497-
result.checkResult(this);
498+
result.checkResult(this, true);
498499
return result;
499500
}
500501

@@ -505,7 +506,7 @@ public WxPayOrderReverseResult reverseOrder(WxPayOrderReverseRequest request) th
505506
String url = this.getPayBaseUrl() + "/secapi/pay/reverse";
506507
String responseContent = this.post(url, request.toXML(), true);
507508
WxPayOrderReverseResult result = WxPayBaseResult.fromXML(responseContent, WxPayOrderReverseResult.class);
508-
result.checkResult(this);
509+
result.checkResult(this, true);
509510
return result;
510511
}
511512

@@ -516,7 +517,7 @@ public String shorturl(WxPayShorturlRequest request) throws WxPayException {
516517
String url = this.getPayBaseUrl() + "/tools/shorturl";
517518
String responseContent = this.post(url, request.toXML(), false);
518519
WxPayShorturlResult result = WxPayBaseResult.fromXML(responseContent, WxPayShorturlResult.class);
519-
result.checkResult(this);
520+
result.checkResult(this, true);
520521
return result.getShortUrl();
521522
}
522523

@@ -532,7 +533,7 @@ public String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxPayE
532533
String url = this.getPayBaseUrl() + "/tools/authcodetoopenid";
533534
String responseContent = this.post(url, request.toXML(), false);
534535
WxPayAuthcode2OpenidResult result = WxPayBaseResult.fromXML(responseContent, WxPayAuthcode2OpenidResult.class);
535-
result.checkResult(this);
536+
result.checkResult(this, true);
536537
return result.getOpenid();
537538
}
538539

@@ -549,7 +550,7 @@ public String getSandboxSignKey() throws WxPayException {
549550
String url = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey";
550551
String responseContent = this.post(url, request.toXML(), false);
551552
WxPaySandboxSignKeyResult result = WxPayBaseResult.fromXML(responseContent, WxPaySandboxSignKeyResult.class);
552-
result.checkResult(this);
553+
result.checkResult(this, true);
553554
return result.getSandboxSignKey();
554555
}
555556

@@ -560,7 +561,7 @@ public WxPayCouponSendResult sendCoupon(WxPayCouponSendRequest request) throws W
560561
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/send_coupon";
561562
String responseContent = this.post(url, request.toXML(), true);
562563
WxPayCouponSendResult result = WxPayBaseResult.fromXML(responseContent, WxPayCouponSendResult.class);
563-
result.checkResult(this);
564+
result.checkResult(this, true);
564565
return result;
565566
}
566567

@@ -571,7 +572,7 @@ public WxPayCouponStockQueryResult queryCouponStock(WxPayCouponStockQueryRequest
571572
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/query_coupon_stock";
572573
String responseContent = this.post(url, request.toXML(), false);
573574
WxPayCouponStockQueryResult result = WxPayBaseResult.fromXML(responseContent, WxPayCouponStockQueryResult.class);
574-
result.checkResult(this);
575+
result.checkResult(this, true);
575576
return result;
576577
}
577578

@@ -582,7 +583,7 @@ public WxPayCouponInfoQueryResult queryCouponInfo(WxPayCouponInfoQueryRequest re
582583
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/querycouponsinfo";
583584
String responseContent = this.post(url, request.toXML(), false);
584585
WxPayCouponInfoQueryResult result = WxPayBaseResult.fromXML(responseContent, WxPayCouponInfoQueryResult.class);
585-
result.checkResult(this);
586+
result.checkResult(this, true);
586587
return result;
587588
}
588589

0 commit comments

Comments
 (0)