Browse Source

非法参数的异常统一使用WxPayException

master
Binary Wang 7 years ago
parent
commit
70d90c8b9b
11 changed files with 28 additions and 25 deletions
  1. +1
    -1
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayBaseRequest.java
  2. +4
    -3
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayDownloadBillRequest.java
  3. +3
    -2
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayOrderQueryRequest.java
  4. +3
    -2
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayOrderReverseRequest.java
  5. +3
    -2
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayRefundQueryRequest.java
  6. +3
    -3
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayRefundRequest.java
  7. +4
    -4
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayUnifiedOrderRequest.java
  8. +3
    -4
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayBaseResult.java
  9. +2
    -2
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java
  10. +1
    -1
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImpl.java
  11. +1
    -1
      weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java

+ 1
- 1
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayBaseRequest.java View File

@@ -122,7 +122,7 @@ public abstract class WxPayBaseRequest {
/** /**
* 检查约束情况 * 检查约束情况
*/ */
protected abstract void checkConstraints();
protected abstract void checkConstraints() throws WxPayException;


public String getAppid() { public String getAppid() {
return this.appid; return this.appid;


+ 4
- 3
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayDownloadBillRequest.java View File

@@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.bean.request; package com.github.binarywang.wxpay.bean.request;


import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.annotation.Required; import me.chanjar.weixin.common.annotation.Required;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
@@ -128,13 +129,13 @@ public class WxPayDownloadBillRequest extends WxPayBaseRequest {
} }


@Override @Override
protected void checkConstraints() {
protected void checkConstraints() throws WxPayException {
if (StringUtils.isNotBlank(this.getTarType()) && !"GZIP".equals(this.getTarType())) { if (StringUtils.isNotBlank(this.getTarType()) && !"GZIP".equals(this.getTarType())) {
throw new IllegalArgumentException("tar_type值如果存在,只能为GZIP");
throw new WxPayException("tar_type值如果存在,只能为GZIP");
} }


if (!ArrayUtils.contains(BILL_TYPE, this.getBillType())) { if (!ArrayUtils.contains(BILL_TYPE, this.getBillType())) {
throw new IllegalArgumentException(String.format("bill_tpye目前必须为%s其中之一,实际值:%s",
throw new WxPayException(String.format("bill_tpye目前必须为%s其中之一,实际值:%s",
Arrays.toString(BILL_TYPE), this.getBillType())); Arrays.toString(BILL_TYPE), this.getBillType()));
} }
} }


+ 3
- 2
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayOrderQueryRequest.java View File

@@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.bean.request; package com.github.binarywang.wxpay.bean.request;


import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;


@@ -64,10 +65,10 @@ public class WxPayOrderQueryRequest extends WxPayBaseRequest {
} }


@Override @Override
protected void checkConstraints() {
protected void checkConstraints() throws WxPayException {
if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) || if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) ||
(StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo))) { (StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo))) {
throw new IllegalArgumentException("transaction_id 和 out_trade_no 不能同时存在或同时为空,必须二选一");
throw new WxPayException("transaction_id 和 out_trade_no 不能同时存在或同时为空,必须二选一");
} }
} }
} }

+ 3
- 2
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayOrderReverseRequest.java View File

@@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.bean.request; package com.github.binarywang.wxpay.bean.request;


import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;


@@ -91,9 +92,9 @@ public class WxPayOrderReverseRequest extends WxPayBaseRequest {
} }


@Override @Override
protected void checkConstraints() {
protected void checkConstraints() throws WxPayException {
if (StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) { if (StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) {
throw new IllegalArgumentException("transaction_id 和 out_trade_no不能同时为空!");
throw new WxPayException("transaction_id 和 out_trade_no不能同时为空!");
} }
} }




+ 3
- 2
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayRefundQueryRequest.java View File

@@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.bean.request; package com.github.binarywang.wxpay.bean.request;


import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;


@@ -135,12 +136,12 @@ public class WxPayRefundQueryRequest extends WxPayBaseRequest {
} }


@Override @Override
protected void checkConstraints() {
protected void checkConstraints() throws WxPayException {
if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo) if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)
&& StringUtils.isBlank(outRefundNo) && StringUtils.isBlank(refundId)) || && StringUtils.isBlank(outRefundNo) && StringUtils.isBlank(refundId)) ||
(StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo) (StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo)
&& StringUtils.isNotBlank(outRefundNo) && StringUtils.isNotBlank(refundId))) { && StringUtils.isNotBlank(outRefundNo) && StringUtils.isNotBlank(refundId))) {
throw new IllegalArgumentException("transaction_id,out_trade_no,out_refund_no,refund_id 必须四选一");
throw new WxPayException("transaction_id,out_trade_no,out_refund_no,refund_id 必须四选一");
} }


} }


+ 3
- 3
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayRefundRequest.java View File

@@ -271,16 +271,16 @@ public class WxPayRefundRequest extends WxPayBaseRequest {
} }


@Override @Override
protected void checkConstraints() {
protected void checkConstraints() throws WxPayException {
if (StringUtils.isNotBlank(this.getRefundAccount())) { if (StringUtils.isNotBlank(this.getRefundAccount())) {
if (!ArrayUtils.contains(REFUND_ACCOUNT, this.getRefundAccount())) { if (!ArrayUtils.contains(REFUND_ACCOUNT, this.getRefundAccount())) {
throw new IllegalArgumentException(String.format("refund_account目前必须为%s其中之一,实际值:%s",
throw new WxPayException(String.format("refund_account目前必须为%s其中之一,实际值:%s",
Arrays.toString(REFUND_ACCOUNT), this.getRefundAccount())); Arrays.toString(REFUND_ACCOUNT), this.getRefundAccount()));
} }
} }


if (StringUtils.isBlank(this.getOutTradeNo()) && StringUtils.isBlank(this.getTransactionId())) { if (StringUtils.isBlank(this.getOutTradeNo()) && StringUtils.isBlank(this.getTransactionId())) {
throw new IllegalArgumentException("transaction_id 和 out_trade_no 不能同时为空,必须提供一个");
throw new WxPayException("transaction_id 和 out_trade_no 不能同时为空,必须提供一个");
} }
} }




+ 4
- 4
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayUnifiedOrderRequest.java View File

@@ -500,18 +500,18 @@ public class WxPayUnifiedOrderRequest extends WxPayBaseRequest {
} }


@Override @Override
protected void checkConstraints() {
protected void checkConstraints() throws WxPayException {
// if (!ArrayUtils.contains(TRADE_TYPES, this.getTradeType())) { // if (!ArrayUtils.contains(TRADE_TYPES, this.getTradeType())) {
// throw new IllegalArgumentException(String.format("trade_type目前必须为%s其中之一,实际值:%s",
// throw new WxPayException(String.format("trade_type目前必须为%s其中之一,实际值:%s",
// Arrays.toString(TRADE_TYPES), this.getTradeType())); // Arrays.toString(TRADE_TYPES), this.getTradeType()));
// } // }


if ("JSAPI".equals(this.getTradeType()) && this.getOpenid() == null) { if ("JSAPI".equals(this.getTradeType()) && this.getOpenid() == null) {
throw new IllegalArgumentException("当 trade_type是'JSAPI'时未指定openid");
throw new WxPayException("当 trade_type是'JSAPI'时未指定openid");
} }


if ("NATIVE".equals(this.getTradeType()) && this.getProductId() == null) { if ("NATIVE".equals(this.getTradeType()) && this.getProductId() == null) {
throw new IllegalArgumentException("当 trade_type是'NATIVE'时未指定product_id");
throw new WxPayException("当 trade_type是'NATIVE'时未指定product_id");
} }
} }




+ 3
- 4
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayBaseResult.java View File

@@ -316,8 +316,8 @@ public abstract class WxPayBaseResult {
} }


//校验结果是否成功 //校验结果是否成功
if (!"SUCCESS".equalsIgnoreCase(getReturnCode())
|| !"SUCCESS".equalsIgnoreCase(getResultCode())) {
if (!StringUtils.equalsAny(StringUtils.trimToEmpty(getReturnCode()).toUpperCase(), "SUCCESS", "")
|| !StringUtils.equalsAny(StringUtils.trimToEmpty(getResultCode()).toUpperCase(), "SUCCESS", "")) {
StringBuilder errorMsg = new StringBuilder(); StringBuilder errorMsg = new StringBuilder();
if (getReturnCode() != null) { if (getReturnCode() != null) {
errorMsg.append("返回代码:").append(getReturnCode()); errorMsg.append("返回代码:").append(getReturnCode());
@@ -335,8 +335,7 @@ public abstract class WxPayBaseResult {
errorMsg.append(",错误详情:").append(getErrCodeDes()); errorMsg.append(",错误详情:").append(getErrCodeDes());
} }


this.getLogger().error("\n结果业务代码异常,返回結果:{},\n{}",
map, errorMsg.toString());
this.getLogger().error("\n结果业务代码异常,返回結果:{},\n{}", map, errorMsg.toString());
throw WxPayException.from(this); throw WxPayException.from(this);
} }
} }


+ 2
- 2
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java View File

@@ -160,11 +160,11 @@ public class WxPayConfig {


public SSLContext initSSLContext() throws WxPayException { public SSLContext initSSLContext() throws WxPayException {
if (StringUtils.isBlank(mchId)) { if (StringUtils.isBlank(mchId)) {
throw new IllegalArgumentException("请确保商户号mchId已设置");
throw new WxPayException("请确保商户号mchId已设置");
} }


if (StringUtils.isBlank(this.keyPath)) { if (StringUtils.isBlank(this.keyPath)) {
throw new IllegalArgumentException("请确保证书文件地址keyPath已配置");
throw new WxPayException("请确保证书文件地址keyPath已配置");
} }


InputStream inputStream; InputStream inputStream;


+ 1
- 1
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImpl.java View File

@@ -161,7 +161,7 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
@Override @Override
public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException { public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException {
if (StringUtils.isBlank(outTradeNo)) { if (StringUtils.isBlank(outTradeNo)) {
throw new IllegalArgumentException("out_trade_no不能为空");
throw new WxPayException("out_trade_no不能为空");
} }


WxPayOrderCloseRequest request = new WxPayOrderCloseRequest(); WxPayOrderCloseRequest request = new WxPayOrderCloseRequest();


+ 1
- 1
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java View File

@@ -139,7 +139,7 @@ public class WxPayServiceAbstractImplTest {
.unifiedOrder(WxPayUnifiedOrderRequest.newBuilder() .unifiedOrder(WxPayUnifiedOrderRequest.newBuilder()
.body("我去") .body("我去")
.totalFee(1) .totalFee(1)
.spbillCreateIp("111111")
.spbillCreateIp("11.1.11.1")
.notifyURL("111111") .notifyURL("111111")
.tradeType("JSAPI") .tradeType("JSAPI")
.openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid()) .openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid())


Loading…
Cancel
Save