mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-04 06:07:59 +08:00 
			
		
		
		
	优化重构微信支付接口对结果的异常错误输出
This commit is contained in:
		@ -74,21 +74,20 @@ public class WxMpPayServiceImpl implements WxMpPayService {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    String url = PAY_BASE_URL + "/secapi/pay/refund";
 | 
					    String url = PAY_BASE_URL + "/secapi/pay/refund";
 | 
				
			||||||
    String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), partnerId);
 | 
					    String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), partnerId);
 | 
				
			||||||
    WxPayRefundResult wxMpPayRefundResult = (WxPayRefundResult) xstream.fromXML(responseContent);
 | 
					    WxPayRefundResult result = (WxPayRefundResult) xstream.fromXML(responseContent);
 | 
				
			||||||
 | 
					    this.checkResult(result);
 | 
				
			||||||
    if (!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getResultCode())
 | 
					    return result;
 | 
				
			||||||
        || !"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);
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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 {
 | 
					  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);
 | 
					    String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), mchId);
 | 
				
			||||||
    WxPaySendRedpackResult redpackResult = (WxPaySendRedpackResult) xstream
 | 
					    WxPaySendRedpackResult result = (WxPaySendRedpackResult) xstream
 | 
				
			||||||
        .fromXML(responseContent);
 | 
					        .fromXML(responseContent);
 | 
				
			||||||
    if ("FAIL".equals(redpackResult.getResultCode())) {
 | 
					    this.checkResult(result);
 | 
				
			||||||
      throw new WxErrorException(WxError.newBuilder()
 | 
					    return result;
 | 
				
			||||||
          .setErrorMsg(
 | 
					 | 
				
			||||||
              redpackResult.getErrCode() + ":" + redpackResult.getErrCodeDes())
 | 
					 | 
				
			||||||
          .build());
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return redpackResult;
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
@ -209,12 +202,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
 | 
				
			|||||||
    String responseContent = this.executeRequest(url, xstream.toXML(request));
 | 
					    String responseContent = this.executeRequest(url, xstream.toXML(request));
 | 
				
			||||||
    WxPayOrderQueryResult result = (WxPayOrderQueryResult) xstream.fromXML(responseContent);
 | 
					    WxPayOrderQueryResult result = (WxPayOrderQueryResult) xstream.fromXML(responseContent);
 | 
				
			||||||
    result.composeCoupons(responseContent);
 | 
					    result.composeCoupons(responseContent);
 | 
				
			||||||
    if ("FAIL".equals(result.getResultCode())) {
 | 
					    this.checkResult(result);
 | 
				
			||||||
      throw new WxErrorException(WxError.newBuilder()
 | 
					 | 
				
			||||||
        .setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes())
 | 
					 | 
				
			||||||
        .build());
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return result;
 | 
					    return result;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -242,11 +230,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    String responseContent = this.executeRequest(url, xstream.toXML(request));
 | 
					    String responseContent = this.executeRequest(url, xstream.toXML(request));
 | 
				
			||||||
    WxPayOrderCloseResult result = (WxPayOrderCloseResult) xstream.fromXML(responseContent);
 | 
					    WxPayOrderCloseResult result = (WxPayOrderCloseResult) xstream.fromXML(responseContent);
 | 
				
			||||||
    if ("FAIL".equals(result.getResultCode())) {
 | 
					    this.checkResult(result);
 | 
				
			||||||
      throw new WxErrorException(WxError.newBuilder()
 | 
					 | 
				
			||||||
        .setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes())
 | 
					 | 
				
			||||||
        .build());
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return result;
 | 
					    return result;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -273,12 +257,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
 | 
				
			|||||||
    String responseContent = this.executeRequest(url, xstream.toXML(request));
 | 
					    String responseContent = this.executeRequest(url, xstream.toXML(request));
 | 
				
			||||||
    WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult) xstream
 | 
					    WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult) xstream
 | 
				
			||||||
        .fromXML(responseContent);
 | 
					        .fromXML(responseContent);
 | 
				
			||||||
    if ("FAIL".equals(result.getResultCode()) || "FAIL".equals(result.getReturnCode())) {
 | 
					    this.checkResult(result);
 | 
				
			||||||
      throw new WxErrorException(WxError.newBuilder()
 | 
					 | 
				
			||||||
          .setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes())
 | 
					 | 
				
			||||||
          .build());
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return result;
 | 
					    return result;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -301,16 +280,6 @@ public class WxMpPayServiceImpl implements WxMpPayService {
 | 
				
			|||||||
  @Override
 | 
					  @Override
 | 
				
			||||||
  public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxErrorException {
 | 
					  public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxErrorException {
 | 
				
			||||||
    WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
 | 
					    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();
 | 
					    String prepayId = unifiedOrderResult.getPrepayId();
 | 
				
			||||||
    if (StringUtils.isBlank(prepayId)) {
 | 
					    if (StringUtils.isBlank(prepayId)) {
 | 
				
			||||||
      throw new RuntimeException(String.format("Failed to get prepay id due to error code '%s'(%s).",
 | 
					      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());
 | 
					    String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), request.getMchId());
 | 
				
			||||||
    WxEntPayResult result = (WxEntPayResult) xstream.fromXML(responseContent);
 | 
					    WxEntPayResult result = (WxEntPayResult) xstream.fromXML(responseContent);
 | 
				
			||||||
    if ("FAIL".equals(result.getResultCode())) {
 | 
					    this.checkResult(result);
 | 
				
			||||||
      throw new WxErrorException(
 | 
					 | 
				
			||||||
        WxError.newBuilder().setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes()).build());
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return result;
 | 
					    return result;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -377,10 +343,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), request.getMchId());
 | 
					    String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), request.getMchId());
 | 
				
			||||||
    WxEntPayQueryResult result = (WxEntPayQueryResult) xstream.fromXML(responseContent);
 | 
					    WxEntPayQueryResult result = (WxEntPayQueryResult) xstream.fromXML(responseContent);
 | 
				
			||||||
    if ("FAIL".equals(result.getResultCode())) {
 | 
					    this.checkResult(result);
 | 
				
			||||||
      throw new WxErrorException(
 | 
					 | 
				
			||||||
        WxError.newBuilder().setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes()).build());
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return result;
 | 
					    return result;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -400,7 +363,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    } catch (IOException e) {
 | 
					    } catch (IOException e) {
 | 
				
			||||||
      this.log.error("\n[URL]:  {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage());
 | 
					      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 {
 | 
					    }finally {
 | 
				
			||||||
      httpPost.releaseConnection();
 | 
					      httpPost.releaseConnection();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -432,7 +395,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    } catch (Exception e) {
 | 
					    } catch (Exception e) {
 | 
				
			||||||
      this.log.error("\n[URL]:  {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage());
 | 
					      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);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user