Skip to content

Commit 92c8a86

Browse files
committed
引入WxPayException,替代原有的异常处理类,并做相应的优化
1 parent ea13197 commit 92c8a86

File tree

9 files changed

+228
-69
lines changed

9 files changed

+228
-69
lines changed

weixin-java-pay/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
<groupId>org.jodd</groupId>
2828
<artifactId>jodd-http</artifactId>
2929
</dependency>
30+
31+
<dependency>
32+
<groupId>ch.qos.logback</groupId>
33+
<artifactId>logback-classic</artifactId>
34+
<scope>test</scope>
35+
</dependency>
3036
<dependency>
3137
<groupId>org.testng</groupId>
3238
<artifactId>testng</artifactId>

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayBaseRequest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.binarywang.wxpay.bean.request;
22

33
import com.github.binarywang.wxpay.config.WxPayConfig;
4+
import com.github.binarywang.wxpay.exception.WxPayException;
45
import com.github.binarywang.wxpay.util.SignUtils;
56
import com.thoughtworks.xstream.XStream;
67
import com.thoughtworks.xstream.annotations.XStreamAlias;
@@ -113,9 +114,13 @@ public static Integer yuanToFee(String yuan) {
113114
/**
114115
* 检查请求参数内容,包括必填参数以及特殊约束
115116
*/
116-
protected void checkFields() throws WxErrorException {
117+
protected void checkFields() throws WxPayException {
117118
//check required fields
118-
BeanUtils.checkRequiredFields(this);
119+
try {
120+
BeanUtils.checkRequiredFields(this);
121+
} catch (WxErrorException e) {
122+
throw new WxPayException(e.getError().getErrorMsg());
123+
}
119124

120125
//check other parameters
121126
this.checkConstraints();
@@ -210,7 +215,7 @@ public String toXML() {
210215
*
211216
* @param config 支付配置对象,用于读取相应系统配置信息
212217
*/
213-
public void checkAndSign(WxPayConfig config) throws WxErrorException {
218+
public void checkAndSign(WxPayConfig config) throws WxPayException {
214219
this.checkFields();
215220

216221
if (StringUtils.isBlank(getAppid())) {

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayRefundRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.github.binarywang.wxpay.bean.request;
22

33
import com.github.binarywang.wxpay.config.WxPayConfig;
4+
import com.github.binarywang.wxpay.exception.WxPayException;
45
import com.thoughtworks.xstream.annotations.XStreamAlias;
56
import me.chanjar.weixin.common.annotation.Required;
6-
import me.chanjar.weixin.common.exception.WxErrorException;
77
import org.apache.commons.lang3.ArrayUtils;
88
import org.apache.commons.lang3.StringUtils;
99

@@ -169,7 +169,7 @@ public static Builder newBuilder() {
169169
}
170170

171171
@Override
172-
public void checkAndSign(WxPayConfig config) throws WxErrorException {
172+
public void checkAndSign(WxPayConfig config) throws WxPayException {
173173
if (StringUtils.isBlank(this.getOpUserId())) {
174174
this.setOpUserId(config.getMchId());
175175
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayUnifiedOrderRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.github.binarywang.wxpay.bean.request;
22

33
import com.github.binarywang.wxpay.config.WxPayConfig;
4+
import com.github.binarywang.wxpay.exception.WxPayException;
45
import com.thoughtworks.xstream.annotations.XStreamAlias;
56
import me.chanjar.weixin.common.annotation.Required;
6-
import me.chanjar.weixin.common.exception.WxErrorException;
77
import org.apache.commons.lang3.ArrayUtils;
88
import org.apache.commons.lang3.StringUtils;
99

@@ -458,7 +458,7 @@ protected void checkConstraints() {
458458
}
459459

460460
@Override
461-
public void checkAndSign(WxPayConfig config) throws WxErrorException {
461+
public void checkAndSign(WxPayConfig config) throws WxPayException {
462462
if (StringUtils.isBlank(this.getNotifyURL())) {
463463
this.setNotifyURL(config.getNotifyUrl());
464464
}

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.github.binarywang.wxpay.bean.result;
22

3+
import com.github.binarywang.wxpay.exception.WxPayException;
34
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
45
import com.github.binarywang.wxpay.util.SignUtils;
56
import com.google.common.base.Joiner;
67
import com.google.common.collect.Maps;
78
import com.thoughtworks.xstream.XStream;
89
import com.thoughtworks.xstream.annotations.XStreamAlias;
9-
import me.chanjar.weixin.common.bean.result.WxError;
10-
import me.chanjar.weixin.common.exception.WxErrorException;
1110
import me.chanjar.weixin.common.util.ToStringUtils;
1211
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
1312
import org.apache.commons.lang3.StringUtils;
@@ -308,12 +307,12 @@ protected Integer getXmlValueAsInt(String... path) {
308307
/**
309308
* 校验返回结果签名
310309
*/
311-
public void checkResult(WxPayServiceImpl wxPayService) throws WxErrorException {
310+
public void checkResult(WxPayServiceImpl wxPayService) throws WxPayException {
312311
//校验返回结果签名
313312
Map<String, String> map = toMap();
314313
if (getSign() != null && !SignUtils.checkSign(map, wxPayService.getConfig().getMchKey())) {
315314
this.getLogger().debug("校验结果签名失败,参数:{}", map);
316-
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("参数格式校验错误!").build());
315+
throw new WxPayException("参数格式校验错误!");
317316
}
318317

319318
//校验结果是否成功
@@ -336,12 +335,9 @@ public void checkResult(WxPayServiceImpl wxPayService) throws WxErrorException {
336335
errorMsg.append(",错误详情:").append(getErrCodeDes());
337336
}
338337

339-
WxError error = WxError.newBuilder()
340-
.setErrorCode(-1)
341-
.setErrorMsg(errorMsg.toString())
342-
.build();
343-
this.getLogger().error("\n结果业务代码异常,返回結果:{},\n{}", map, error);
344-
throw new WxErrorException(error);
338+
this.getLogger().error("\n结果业务代码异常,返回結果:{},\n{}",
339+
map, errorMsg.toString());
340+
throw WxPayException.from(this);
345341
}
346342
}
347343
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package com.github.binarywang.wxpay.exception;
2+
3+
import com.github.binarywang.wxpay.bean.result.WxPayBaseResult;
4+
import com.google.common.base.Joiner;
5+
6+
/**
7+
* <pre>
8+
* 微信支付异常结果类
9+
* Created by Binary Wang on 2017-6-6.
10+
* </pre>
11+
*/
12+
public class WxPayException extends Exception {
13+
private String customErrorMsg;
14+
/**
15+
* 返回状态码
16+
*/
17+
private String returnCode;
18+
/**
19+
* 返回信息
20+
*/
21+
private String returnMsg;
22+
23+
/**
24+
* 业务结果
25+
*/
26+
private String resultCode;
27+
28+
/**
29+
* 错误代码
30+
*/
31+
private String errCode;
32+
33+
/**
34+
* 错误代码描述
35+
*/
36+
private String errCodeDes;
37+
38+
/**
39+
* 微信支付返回的结果xml字符串
40+
*/
41+
private String xmlString;
42+
43+
public WxPayException(String customErrorMsg) {
44+
super(customErrorMsg);
45+
this.customErrorMsg = customErrorMsg;
46+
}
47+
48+
private WxPayException(Builder builder) {
49+
super(builder.buildErrorMsg());
50+
returnCode = builder.returnCode;
51+
returnMsg = builder.returnMsg;
52+
resultCode = builder.resultCode;
53+
errCode = builder.errCode;
54+
errCodeDes = builder.errCodeDes;
55+
xmlString = builder.xmlString;
56+
}
57+
58+
public static WxPayException from(WxPayBaseResult payBaseResult) {
59+
return WxPayException.newBuilder()
60+
.xmlString(payBaseResult.getXmlString())
61+
.returnMsg(payBaseResult.getReturnMsg())
62+
.returnCode(payBaseResult.getReturnCode())
63+
.resultCode(payBaseResult.getResultCode())
64+
.errCode(payBaseResult.getErrCode())
65+
.errCodeDes(payBaseResult.getErrCodeDes())
66+
.build();
67+
}
68+
69+
public String getXmlString() {
70+
return this.xmlString;
71+
}
72+
73+
public String getReturnCode() {
74+
return this.returnCode;
75+
}
76+
77+
public String getReturnMsg() {
78+
return this.returnMsg;
79+
}
80+
81+
public String getResultCode() {
82+
return this.resultCode;
83+
}
84+
85+
public String getErrCode() {
86+
return this.errCode;
87+
}
88+
89+
public String getErrCodeDes() {
90+
return this.errCodeDes;
91+
}
92+
93+
public static Builder newBuilder() {
94+
return new Builder();
95+
}
96+
97+
public static final class Builder {
98+
private String returnCode;
99+
private String returnMsg;
100+
private String resultCode;
101+
private String errCode;
102+
private String errCodeDes;
103+
private String xmlString;
104+
105+
private Builder() {
106+
}
107+
108+
public Builder returnCode(String returnCode) {
109+
this.returnCode = returnCode;
110+
return this;
111+
}
112+
113+
public Builder returnMsg(String returnMsg) {
114+
this.returnMsg = returnMsg;
115+
return this;
116+
}
117+
118+
public Builder resultCode(String resultCode) {
119+
this.resultCode = resultCode;
120+
return this;
121+
}
122+
123+
public Builder errCode(String errCode) {
124+
this.errCode = errCode;
125+
return this;
126+
}
127+
128+
public Builder errCodeDes(String errCodeDes) {
129+
this.errCodeDes = errCodeDes;
130+
return this;
131+
}
132+
133+
public Builder xmlString(String xmlString) {
134+
this.xmlString = xmlString;
135+
return this;
136+
}
137+
138+
public WxPayException build() {
139+
return new WxPayException(this);
140+
}
141+
142+
public String buildErrorMsg() {
143+
return Joiner.on(",").skipNulls().join(new String[]{
144+
returnCode == null ? null : String.format("返回代码:[%s]", returnCode),
145+
returnMsg == null ? null : String.format("返回信息:[%s]", returnMsg),
146+
resultCode == null ? null : String.format("结果代码:[%s]", resultCode),
147+
errCode == null ? null : String.format("错误代码:[%s]", errCode),
148+
errCodeDes == null ? null : String.format("错误详情:[%s]", errCodeDes),
149+
xmlString == null ? null : "微信返回的原始报文:\n" + xmlString,
150+
});
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)