From c82b9f06b391534defbacae75c916c803bba979c Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Sat, 29 Oct 2016 18:49:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=87=8D=E6=9E=84=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E6=94=AF=E4=BB=98=E6=8E=A5=E5=8F=A3=E5=AF=B9=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E7=9A=84=E5=BC=82=E5=B8=B8=E9=94=99=E8=AF=AF=E8=BE=93?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mp/api/impl/WxMpPayServiceImpl.java | 83 +++++-------------- 1 file changed, 23 insertions(+), 60 deletions(-) diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java index 7f628aff..05e98511 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java @@ -74,21 +74,20 @@ public class WxMpPayServiceImpl implements WxMpPayService { String url = PAY_BASE_URL + "/secapi/pay/refund"; String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), partnerId); - WxPayRefundResult wxMpPayRefundResult = (WxPayRefundResult) xstream.fromXML(responseContent); - - if (!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getResultCode()) - || !"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getReturnCode())) { - WxError error = new WxError(); - error.setErrorCode(-1); - error.setErrorMsg("return_code:" + wxMpPayRefundResult.getReturnCode() - + ";return_msg:" + wxMpPayRefundResult.getReturnMsg() - + ";result_code:" + wxMpPayRefundResult.getResultCode() + ";err_code" - + wxMpPayRefundResult.getErrCode() + ";err_code_des" - + wxMpPayRefundResult.getErrCodeDes()); - throw new WxErrorException(error); - } + WxPayRefundResult result = (WxPayRefundResult) xstream.fromXML(responseContent); + this.checkResult(result); + return result; + } - return wxMpPayRefundResult; + private void checkResult(WxPayBaseResult result) throws WxErrorException { + if (!"SUCCESS".equalsIgnoreCase(result.getReturnCode()) + || !"SUCCESS".equalsIgnoreCase(result.getResultCode())) { + throw new WxErrorException(WxError.newBuilder().setErrorCode(-1) + .setErrorMsg("返回代码:" + result.getReturnCode() + ", 返回信息: " + + result.getReturnMsg() + ", 结果代码: " + result.getResultCode() + ", 错误代码: " + + result.getErrCode() + ", 错误详情: " + result.getErrCodeDes()) + .build()); + } } private void checkParameters(WxPayRefundRequest request) throws WxErrorException { @@ -147,16 +146,10 @@ public class WxMpPayServiceImpl implements WxMpPayService { } String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), mchId); - WxPaySendRedpackResult redpackResult = (WxPaySendRedpackResult) xstream + WxPaySendRedpackResult result = (WxPaySendRedpackResult) xstream .fromXML(responseContent); - if ("FAIL".equals(redpackResult.getResultCode())) { - throw new WxErrorException(WxError.newBuilder() - .setErrorMsg( - redpackResult.getErrCode() + ":" + redpackResult.getErrCodeDes()) - .build()); - } - - return redpackResult; + this.checkResult(result); + return result; } /** @@ -209,12 +202,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { String responseContent = this.executeRequest(url, xstream.toXML(request)); WxPayOrderQueryResult result = (WxPayOrderQueryResult) xstream.fromXML(responseContent); result.composeCoupons(responseContent); - if ("FAIL".equals(result.getResultCode())) { - throw new WxErrorException(WxError.newBuilder() - .setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes()) - .build()); - } - + this.checkResult(result); return result; } @@ -242,11 +230,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { String responseContent = this.executeRequest(url, xstream.toXML(request)); WxPayOrderCloseResult result = (WxPayOrderCloseResult) xstream.fromXML(responseContent); - if ("FAIL".equals(result.getResultCode())) { - throw new WxErrorException(WxError.newBuilder() - .setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes()) - .build()); - } + this.checkResult(result); return result; } @@ -273,12 +257,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { String responseContent = this.executeRequest(url, xstream.toXML(request)); WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult) xstream .fromXML(responseContent); - if ("FAIL".equals(result.getResultCode()) || "FAIL".equals(result.getReturnCode())) { - throw new WxErrorException(WxError.newBuilder() - .setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes()) - .build()); - } - + this.checkResult(result); return result; } @@ -301,16 +280,6 @@ public class WxMpPayServiceImpl implements WxMpPayService { @Override public Map getPayInfo(WxPayUnifiedOrderRequest request) throws WxErrorException { WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request); - - if (!"SUCCESS".equalsIgnoreCase(unifiedOrderResult.getReturnCode()) - || !"SUCCESS".equalsIgnoreCase(unifiedOrderResult.getResultCode())) { - throw new WxErrorException(WxError.newBuilder().setErrorCode(-1) - .setErrorMsg("return_code:" + unifiedOrderResult.getReturnCode() + ";return_msg:" - + unifiedOrderResult.getReturnMsg() + ";result_code:" + unifiedOrderResult.getResultCode() + ";err_code" - + unifiedOrderResult.getErrCode() + ";err_code_des" + unifiedOrderResult.getErrCodeDes()) - .build()); - } - String prepayId = unifiedOrderResult.getPrepayId(); if (StringUtils.isBlank(prepayId)) { throw new RuntimeException(String.format("Failed to get prepay id due to error code '%s'(%s).", @@ -352,10 +321,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), request.getMchId()); WxEntPayResult result = (WxEntPayResult) xstream.fromXML(responseContent); - if ("FAIL".equals(result.getResultCode())) { - throw new WxErrorException( - WxError.newBuilder().setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes()).build()); - } + this.checkResult(result); return result; } @@ -377,10 +343,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), request.getMchId()); WxEntPayQueryResult result = (WxEntPayQueryResult) xstream.fromXML(responseContent); - if ("FAIL".equals(result.getResultCode())) { - throw new WxErrorException( - WxError.newBuilder().setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes()).build()); - } + this.checkResult(result); return result; } @@ -400,7 +363,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { } } catch (IOException e) { this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage()); - throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e); + throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg(e.getMessage()).build(), e); }finally { httpPost.releaseConnection(); } @@ -432,7 +395,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { } } catch (Exception e) { this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage()); - throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e); + throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg(e.getMessage()).build(), e); } }