Skip to content

Commit c82b9f0

Browse files
committed
优化重构微信支付接口对结果的异常错误输出
1 parent 4fa672d commit c82b9f0

File tree

1 file changed

+23
-60
lines changed

1 file changed

+23
-60
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,20 @@ public WxPayRefundResult refund(WxPayRefundRequest request, File keyFile)
7474

7575
String url = PAY_BASE_URL + "/secapi/pay/refund";
7676
String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), partnerId);
77-
WxPayRefundResult wxMpPayRefundResult = (WxPayRefundResult) xstream.fromXML(responseContent);
78-
79-
if (!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getResultCode())
80-
|| !"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getReturnCode())) {
81-
WxError error = new WxError();
82-
error.setErrorCode(-1);
83-
error.setErrorMsg("return_code:" + wxMpPayRefundResult.getReturnCode()
84-
+ ";return_msg:" + wxMpPayRefundResult.getReturnMsg()
85-
+ ";result_code:" + wxMpPayRefundResult.getResultCode() + ";err_code"
86-
+ wxMpPayRefundResult.getErrCode() + ";err_code_des"
87-
+ wxMpPayRefundResult.getErrCodeDes());
88-
throw new WxErrorException(error);
89-
}
77+
WxPayRefundResult result = (WxPayRefundResult) xstream.fromXML(responseContent);
78+
this.checkResult(result);
79+
return result;
80+
}
9081

91-
return wxMpPayRefundResult;
82+
private void checkResult(WxPayBaseResult result) throws WxErrorException {
83+
if (!"SUCCESS".equalsIgnoreCase(result.getReturnCode())
84+
|| !"SUCCESS".equalsIgnoreCase(result.getResultCode())) {
85+
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1)
86+
.setErrorMsg("返回代码:" + result.getReturnCode() + ", 返回信息: "
87+
+ result.getReturnMsg() + ", 结果代码: " + result.getResultCode() + ", 错误代码: "
88+
+ result.getErrCode() + ", 错误详情: " + result.getErrCodeDes())
89+
.build());
90+
}
9291
}
9392

9493
private void checkParameters(WxPayRefundRequest request) throws WxErrorException {
@@ -147,16 +146,10 @@ public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request, File
147146
}
148147

149148
String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), mchId);
150-
WxPaySendRedpackResult redpackResult = (WxPaySendRedpackResult) xstream
149+
WxPaySendRedpackResult result = (WxPaySendRedpackResult) xstream
151150
.fromXML(responseContent);
152-
if ("FAIL".equals(redpackResult.getResultCode())) {
153-
throw new WxErrorException(WxError.newBuilder()
154-
.setErrorMsg(
155-
redpackResult.getErrCode() + ":" + redpackResult.getErrCodeDes())
156-
.build());
157-
}
158-
159-
return redpackResult;
151+
this.checkResult(result);
152+
return result;
160153
}
161154

162155
/**
@@ -209,12 +202,7 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
209202
String responseContent = this.executeRequest(url, xstream.toXML(request));
210203
WxPayOrderQueryResult result = (WxPayOrderQueryResult) xstream.fromXML(responseContent);
211204
result.composeCoupons(responseContent);
212-
if ("FAIL".equals(result.getResultCode())) {
213-
throw new WxErrorException(WxError.newBuilder()
214-
.setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes())
215-
.build());
216-
}
217-
205+
this.checkResult(result);
218206
return result;
219207
}
220208

@@ -242,11 +230,7 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorExcepti
242230

243231
String responseContent = this.executeRequest(url, xstream.toXML(request));
244232
WxPayOrderCloseResult result = (WxPayOrderCloseResult) xstream.fromXML(responseContent);
245-
if ("FAIL".equals(result.getResultCode())) {
246-
throw new WxErrorException(WxError.newBuilder()
247-
.setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes())
248-
.build());
249-
}
233+
this.checkResult(result);
250234

251235
return result;
252236
}
@@ -273,12 +257,7 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request)
273257
String responseContent = this.executeRequest(url, xstream.toXML(request));
274258
WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult) xstream
275259
.fromXML(responseContent);
276-
if ("FAIL".equals(result.getResultCode()) || "FAIL".equals(result.getReturnCode())) {
277-
throw new WxErrorException(WxError.newBuilder()
278-
.setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes())
279-
.build());
280-
}
281-
260+
this.checkResult(result);
282261
return result;
283262
}
284263

@@ -301,16 +280,6 @@ private void checkParameters(WxPayUnifiedOrderRequest request) throws WxErrorExc
301280
@Override
302281
public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxErrorException {
303282
WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
304-
305-
if (!"SUCCESS".equalsIgnoreCase(unifiedOrderResult.getReturnCode())
306-
|| !"SUCCESS".equalsIgnoreCase(unifiedOrderResult.getResultCode())) {
307-
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1)
308-
.setErrorMsg("return_code:" + unifiedOrderResult.getReturnCode() + ";return_msg:"
309-
+ unifiedOrderResult.getReturnMsg() + ";result_code:" + unifiedOrderResult.getResultCode() + ";err_code"
310-
+ unifiedOrderResult.getErrCode() + ";err_code_des" + unifiedOrderResult.getErrCodeDes())
311-
.build());
312-
}
313-
314283
String prepayId = unifiedOrderResult.getPrepayId();
315284
if (StringUtils.isBlank(prepayId)) {
316285
throw new RuntimeException(String.format("Failed to get prepay id due to error code '%s'(%s).",
@@ -352,10 +321,7 @@ public WxEntPayResult entPay(WxEntPayRequest request, File keyFile) throws WxErr
352321

353322
String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), request.getMchId());
354323
WxEntPayResult result = (WxEntPayResult) xstream.fromXML(responseContent);
355-
if ("FAIL".equals(result.getResultCode())) {
356-
throw new WxErrorException(
357-
WxError.newBuilder().setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes()).build());
358-
}
324+
this.checkResult(result);
359325
return result;
360326
}
361327

@@ -377,10 +343,7 @@ public WxEntPayQueryResult queryEntPay(String partnerTradeNo, File keyFile) thro
377343

378344
String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), request.getMchId());
379345
WxEntPayQueryResult result = (WxEntPayQueryResult) xstream.fromXML(responseContent);
380-
if ("FAIL".equals(result.getResultCode())) {
381-
throw new WxErrorException(
382-
WxError.newBuilder().setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes()).build());
383-
}
346+
this.checkResult(result);
384347
return result;
385348
}
386349

@@ -400,7 +363,7 @@ private String executeRequest( String url, String requestStr) throws WxErrorExce
400363
}
401364
} catch (IOException e) {
402365
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage());
403-
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
366+
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg(e.getMessage()).build(), e);
404367
}finally {
405368
httpPost.releaseConnection();
406369
}
@@ -432,7 +395,7 @@ private String executeRequestWithKeyFile( String url, File keyFile, String reque
432395
}
433396
} catch (Exception e) {
434397
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage());
435-
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
398+
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg(e.getMessage()).build(), e);
436399
}
437400
}
438401

0 commit comments

Comments
 (0)