diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/util/ServiceExceptionUtil.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/util/ServiceExceptionUtil.java index 7a9d62dd1..966109fbf 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/util/ServiceExceptionUtil.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/util/ServiceExceptionUtil.java @@ -2,6 +2,7 @@ package cn.iocoder.yudao.framework.common.exception.util; import cn.iocoder.yudao.framework.common.exception.ErrorCode; import cn.iocoder.yudao.framework.common.exception.ServiceException; +import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; import com.google.common.annotations.VisibleForTesting; import lombok.extern.slf4j.Slf4j; @@ -80,6 +81,10 @@ public class ServiceExceptionUtil { return new ServiceException(code, message); } + public static ServiceException invalidParamException(String messagePattern, Object... params) { + return exception0(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), messagePattern, params); + } + // ========== 格式化方法 ========== /** diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/exception/PayException.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/exception/PayException.java index 95fe7fb12..75f4c3975 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/exception/PayException.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/exception/PayException.java @@ -1,26 +1,17 @@ package cn.iocoder.yudao.framework.pay.core.client.exception; -import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; -import lombok.NoArgsConstructor; /** - * 业务逻辑异常 Exception + * 支付系统异常 Exception */ @Data @EqualsAndHashCode(callSuper = true) -@NoArgsConstructor -@AllArgsConstructor public class PayException extends RuntimeException { - /** - * 第三方平台的错误码 - */ - private String code; - /** - * 第三方平台的错误提示 - */ - private String message; + public PayException(Throwable cause) { + super(cause); + } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/AbstractPayClient.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/AbstractPayClient.java index 262f9ab8e..0225739ca 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/AbstractPayClient.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/AbstractPayClient.java @@ -1,24 +1,19 @@ package cn.iocoder.yudao.framework.pay.core.client.impl; -import cn.hutool.core.date.LocalDateTimeUtil; +import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.iocoder.yudao.framework.pay.core.client.PayClient; import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedRespDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedRespDTO; -import com.alipay.api.AlipayResponse; +import cn.iocoder.yudao.framework.pay.core.client.exception.PayException; import lombok.extern.slf4j.Slf4j; import javax.validation.Validation; -import java.time.LocalDateTime; -import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMATTER; -import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0; import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; -import static cn.iocoder.yudao.framework.pay.core.enums.PayFrameworkErrorCodeConstants.PAY_EXCEPTION; -// TODO 芋艿:优化下,替换异常; /** * 支付客户端的抽象类,提供模板方法,减少子类的冗余代码 * @@ -78,16 +73,19 @@ public abstract class AbstractPayClient implemen @Override public final PayOrderUnifiedRespDTO unifiedOrder(PayOrderUnifiedReqDTO reqDTO) { Validation.buildDefaultValidatorFactory().getValidator().validate(reqDTO); - // 执行短信发送 - PayOrderUnifiedRespDTO result; + // 执行统一下单 + PayOrderUnifiedRespDTO resp; try { - result = doUnifiedOrder(reqDTO); + resp = doUnifiedOrder(reqDTO); + } catch (ServiceException ex) { + // 业务异常,都是实现类已经翻译,所以直接抛出即可 + throw ex; } catch (Throwable ex) { - // 打印异常日志 - log.error("[unifiedOrder][request({}) 发起支付失败]", toJsonString(reqDTO), ex); + // 系统异常,则包装成 PayException 异常抛出 + log.error("[unifiedRefund][request({}) 发起支付异常]", toJsonString(reqDTO), ex); throw buildException(ex); } - return result; + return resp; } protected abstract PayOrderUnifiedRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) @@ -95,12 +93,17 @@ public abstract class AbstractPayClient implemen @Override public PayRefundUnifiedRespDTO unifiedRefund(PayRefundUnifiedReqDTO reqDTO) { + Validation.buildDefaultValidatorFactory().getValidator().validate(reqDTO); + // 执行统一退款 PayRefundUnifiedRespDTO resp; try { resp = doUnifiedRefund(reqDTO); - } catch (Throwable ex) { - // 记录异常日志 - log.error("[unifiedRefund][request({}) 发起退款失败]", toJsonString(reqDTO), ex); + } catch (ServiceException ex) { + // 业务异常,都是实现类已经翻译,所以直接抛出即可 + throw ex; + } catch (Throwable ex) { + // 系统异常,则包装成 PayException 异常抛出 + log.error("[unifiedRefund][request({}) 发起退款异常]", toJsonString(reqDTO), ex); throw buildException(ex); } return resp; @@ -110,32 +113,11 @@ public abstract class AbstractPayClient implemen // ========== 各种工具方法 ========== - private RuntimeException buildException(Throwable ex) { - if (ex instanceof RuntimeException) { - return (RuntimeException) ex; + private PayException buildException(Throwable ex) { + if (ex instanceof PayException) { + return (PayException) ex; } - throw new RuntimeException(ex); - } - - protected void validateSuccess(AlipayResponse response) { - if (response.isSuccess()) { - return; - } - throw exception0(PAY_EXCEPTION.getCode(), response.getSubMsg()); - } - - protected String formatAmount(Integer amount) { - return String.valueOf(amount / 100.0); - } - - protected String formatTime(LocalDateTime time) { - // "yyyy-MM-dd HH:mm:ss" - return LocalDateTimeUtil.format(time, NORM_DATETIME_FORMATTER); - } - - protected LocalDateTime parseTime(String str) { - // "yyyy-MM-dd HH:mm:ss" - return LocalDateTimeUtil.parse(str, NORM_DATETIME_FORMATTER); + throw new PayException(ex); } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AbstractAlipayPayClient.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AbstractAlipayPayClient.java index c326c2ba7..a1f6047c4 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AbstractAlipayPayClient.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AbstractAlipayPayClient.java @@ -1,7 +1,9 @@ package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.http.HttpUtil; +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayNotifyReqDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayOrderNotifyRespDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayRefundNotifyRespDTO; @@ -9,9 +11,7 @@ import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReq import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedRespDTO; import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; import cn.iocoder.yudao.framework.pay.core.enums.PayNotifyRefundStatusEnum; -import com.alipay.api.AlipayApiException; -import com.alipay.api.AlipayConfig; -import com.alipay.api.DefaultAlipayClient; +import com.alipay.api.*; import com.alipay.api.domain.AlipayTradeRefundModel; import com.alipay.api.internal.util.AlipaySignature; import com.alipay.api.request.AlipayTradeRefundRequest; @@ -20,9 +20,13 @@ import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import java.nio.charset.StandardCharsets; +import java.time.LocalDateTime; import java.util.Map; +import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMATTER; +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0; import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; +import static cn.iocoder.yudao.framework.pay.core.enums.PayFrameworkErrorCodeConstants.ORDER_UNIFIED_ERROR; /** * 支付宝抽象类,实现支付宝统一的接口、以及部分实现(退款) @@ -58,7 +62,7 @@ public abstract class AbstractAlipayPayClient extends AbstractPayClient