🎨 优化微信支付异常处理代码,兼容混乱且变态的微信官方字段命名习惯

This commit is contained in:
Binary Wang 2023-07-19 18:51:52 +08:00
parent 76fd3a20b5
commit 6852617bd7
2 changed files with 21 additions and 3 deletions

View File

@ -59,11 +59,18 @@ public abstract class BaseWxPayResult {
*/ */
@XStreamAlias("result_code") @XStreamAlias("result_code")
private String resultCode; private String resultCode;
/** /**
* 错误代码. * 错误代码.
*/ */
@XStreamAlias("err_code") @XStreamAlias("err_code")
private String errCode; private String errCode;
/**
* 错误代码描述.
*/
@XStreamAlias("err_code_des")
private String errCodeDes;
/** /**
* 错误代码. * 错误代码.
*/ */
@ -72,8 +79,9 @@ public abstract class BaseWxPayResult {
/** /**
* 错误代码描述. * 错误代码描述.
*/ */
@XStreamAlias("err_code_des") @XStreamAlias("error_message")
private String errCodeDes; private String errorMessage;
/** /**
* 公众账号ID. * 公众账号ID.
*/ */

View File

@ -89,7 +89,7 @@ public class WxPayException extends Exception {
* @return the wx pay exception * @return the wx pay exception
*/ */
public static WxPayException from(BaseWxPayResult payBaseResult) { public static WxPayException from(BaseWxPayResult payBaseResult) {
return WxPayException.newBuilder() WxPayException exception = WxPayException.newBuilder()
.xmlString(payBaseResult.getXmlString()) .xmlString(payBaseResult.getXmlString())
.returnMsg(payBaseResult.getReturnMsg()) .returnMsg(payBaseResult.getReturnMsg())
.returnCode(payBaseResult.getReturnCode()) .returnCode(payBaseResult.getReturnCode())
@ -97,6 +97,16 @@ public class WxPayException extends Exception {
.errCode(payBaseResult.getErrCode()) .errCode(payBaseResult.getErrCode())
.errCodeDes(payBaseResult.getErrCodeDes()) .errCodeDes(payBaseResult.getErrCodeDes())
.build(); .build();
if (payBaseResult.getErrorCode() != null) {
exception.setErrCode(payBaseResult.getErrorCode());
}
if (payBaseResult.getErrorMessage() != null) {
exception.setErrCodeDes(payBaseResult.getErrorMessage());
}
return exception;
} }
/** /**