From 2093c3bdb51dfb4367987a71776db2e0c0603f3d Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 19 Feb 2023 22:16:15 +0800 Subject: [PATCH] =?UTF-8?q?pay:=20=E9=87=8D=E6=9E=84=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=EF=BC=8C=E7=A7=BB=E9=99=A4=20PayCom?= =?UTF-8?q?monResult=20=E7=B4=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/client/AbstractPayCodeMapping.java | 33 ----------- .../framework/pay/core/client/PayClient.java | 6 +- .../pay/core/client/PayCommonResult.java | 57 ------------------ .../core/client/impl/AbstractPayClient.java | 51 ++++++++-------- .../impl/alipay/AbstractAlipayClient.java | 22 ++++--- .../impl/alipay/AlipayBarPayClient.java | 38 ++++++------ .../impl/alipay/AlipayPayCodeMapping.java | 28 --------- .../client/impl/alipay/AlipayPcPayClient.java | 33 ++++------- .../client/impl/alipay/AlipayQrPayClient.java | 29 +++------ .../impl/alipay/AlipayWapPayClient.java | 30 +++------- .../core/client/impl/wx/WXCodeMapping.java | 56 ------------------ .../core/client/impl/wx/WXLitePayClient.java | 59 +++++++++---------- .../client/impl/wx/WXNativePayClient.java | 56 ++++++++---------- .../core/client/impl/wx/WXPubPayClient.java | 59 +++++++++---------- .../enums/PayFrameworkErrorCodeConstants.java | 6 +- .../PayClientFactoryImplIntegrationTest.java | 18 +++--- .../alipay/AlipayQrPayClientTest.java | 15 +++-- .../service/order/PayOrderServiceImpl.java | 7 +-- .../service/refund/PayRefundServiceImpl.java | 7 +-- yudao-ui-admin/src/views/pay/order/submit.vue | 48 ++++++++++++++- 20 files changed, 240 insertions(+), 418 deletions(-) delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/AbstractPayCodeMapping.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayCommonResult.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPayCodeMapping.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXCodeMapping.java diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/AbstractPayCodeMapping.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/AbstractPayCodeMapping.java deleted file mode 100644 index 88be97f00..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/AbstractPayCodeMapping.java +++ /dev/null @@ -1,33 +0,0 @@ -package cn.iocoder.yudao.framework.pay.core.client; - -import cn.iocoder.yudao.framework.common.exception.ErrorCode; -import cn.iocoder.yudao.framework.pay.core.enums.PayFrameworkErrorCodeConstants; -import lombok.extern.slf4j.Slf4j; - -/** - * 将 API 的错误码,转换为通用的错误码 - * - * @see PayCommonResult - * @see PayFrameworkErrorCodeConstants - * - * @author 芋道源码 - */ -@Slf4j -public abstract class AbstractPayCodeMapping { - - public final ErrorCode apply(String apiCode, String apiMsg) { - if (apiCode == null) { - log.error("[apply][API 错误码为空,请排查]"); - return PayFrameworkErrorCodeConstants.EXCEPTION; - } - ErrorCode errorCode = this.apply0(apiCode, apiMsg); - if (errorCode == null) { - log.error("[apply][API 错误码({}) 错误提示({}) 无法匹配]", apiCode, apiMsg); - return PayFrameworkErrorCodeConstants.PAY_UNKNOWN; - } - return errorCode; - } - - protected abstract ErrorCode apply0(String apiCode, String apiMsg); - -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClient.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClient.java index f933f34a7..375ff3ef8 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClient.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClient.java @@ -5,7 +5,7 @@ import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayNotifyDataDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayRefundNotifyDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayOrderNotifyRespDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; -import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; +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; /** @@ -28,7 +28,7 @@ public interface PayClient { * @param reqDTO 下单信息 * @return 各支付渠道的返回结果 */ - PayCommonResult unifiedOrder(PayOrderUnifiedReqDTO reqDTO); + PayOrderUnifiedRespDTO unifiedOrder(PayOrderUnifiedReqDTO reqDTO); /** * 解析支付单的通知结果 @@ -44,7 +44,7 @@ public interface PayClient { * @param reqDTO 统一退款请求信息 * @return 各支付渠道的统一返回结果 */ - PayCommonResult unifiedRefund(PayRefundUnifiedReqDTO reqDTO); + PayRefundUnifiedRespDTO unifiedRefund(PayRefundUnifiedReqDTO reqDTO); /** * 解析支付退款通知数据 diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayCommonResult.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayCommonResult.java deleted file mode 100644 index 8837a0ac9..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayCommonResult.java +++ /dev/null @@ -1,57 +0,0 @@ -package cn.iocoder.yudao.framework.pay.core.client; - -import cn.hutool.core.exceptions.ExceptionUtil; -import cn.hutool.core.lang.Assert; -import cn.iocoder.yudao.framework.common.exception.ErrorCode; -import cn.iocoder.yudao.framework.common.pojo.CommonResult; -import cn.iocoder.yudao.framework.pay.core.enums.PayFrameworkErrorCodeConstants; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.ToString; - -/** - * 支付的 CommonResult 拓展类 - * - * 考虑到不同的平台,返回的 code 和 msg 是不同的,所以统一额外返回 {@link #apiCode} 和 {@link #apiMsg} 字段 - * - * @author 芋道源码 - */ -@Data -@EqualsAndHashCode(callSuper = true) -@ToString(callSuper = true) -public class PayCommonResult extends CommonResult { - - /** - * API 返回错误码 - * - * 由于第三方的错误码可能是字符串,所以使用 String 类型 - */ - private String apiCode; - /** - * API 返回提示 - */ - private String apiMsg; - - private PayCommonResult() { - } - - public static PayCommonResult build(String apiCode, String apiMsg, T data, AbstractPayCodeMapping codeMapping) { - Assert.notNull(codeMapping, "参数 codeMapping 不能为空"); - PayCommonResult result = new PayCommonResult().setApiCode(apiCode).setApiMsg(apiMsg); - result.setData(data); - // 翻译错误码 - if (codeMapping != null) { - ErrorCode errorCode = codeMapping.apply(apiCode, apiMsg); - result.setCode(errorCode.getCode()).setMsg(errorCode.getMsg()); - } - return result; - } - - public static PayCommonResult error(Throwable ex) { - PayCommonResult result = new PayCommonResult<>(); - result.setCode(PayFrameworkErrorCodeConstants.EXCEPTION.getCode()); - result.setMsg(ExceptionUtil.getRootCauseMessage(ex)); - return result; - } - -} 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 5a616bf83..81f850895 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,25 +1,22 @@ package cn.iocoder.yudao.framework.pay.core.client.impl; -import cn.hutool.core.date.DatePattern; -import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.LocalDateTimeUtil; -import cn.iocoder.yudao.framework.common.util.date.DateUtils; -import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils; -import cn.iocoder.yudao.framework.pay.core.client.AbstractPayCodeMapping; 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.PayCommonResult; 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 lombok.extern.slf4j.Slf4j; +import com.alipay.api.AlipayResponse;import lombok.extern.slf4j.Slf4j; import javax.validation.Validation; - import java.time.LocalDateTime; import static cn.hutool.core.date.DatePattern.NORM_DATETIME_MS_FORMATTER; +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +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; /** * 支付客户端的抽象类,提供模板方法,减少子类的冗余代码 @@ -37,19 +34,14 @@ public abstract class AbstractPayClient implemen * 渠道编码 */ private final String channelCode; - /** - * 错误码枚举类 - */ - protected AbstractPayCodeMapping codeMapping; /** * 支付配置 */ protected Config config; - public AbstractPayClient(Long channelId, String channelCode, Config config, AbstractPayCodeMapping codeMapping) { + public AbstractPayClient(Long channelId, String channelCode, Config config) { this.channelId = channelId; this.channelCode = channelCode; - this.codeMapping = codeMapping; this.config = config; } @@ -83,41 +75,54 @@ public abstract class AbstractPayClient implemen } @Override - public final PayCommonResult unifiedOrder(PayOrderUnifiedReqDTO reqDTO) { + public final PayOrderUnifiedRespDTO unifiedOrder(PayOrderUnifiedReqDTO reqDTO) { Validation.buildDefaultValidatorFactory().getValidator().validate(reqDTO); // 执行短信发送 - PayCommonResult result; + PayOrderUnifiedRespDTO result; try { result = doUnifiedOrder(reqDTO); } catch (Throwable ex) { // 打印异常日志 log.error("[unifiedOrder][request({}) 发起支付失败]", toJsonString(reqDTO), ex); - // 封装返回 - return PayCommonResult.error(ex); + throw buildException(ex); } return result; } - protected abstract PayCommonResult doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) + protected abstract PayOrderUnifiedRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) throws Throwable; @Override - public PayCommonResult unifiedRefund(PayRefundUnifiedReqDTO reqDTO) { - PayCommonResult resp; + public PayRefundUnifiedRespDTO unifiedRefund(PayRefundUnifiedReqDTO reqDTO) { + PayRefundUnifiedRespDTO resp; try { resp = doUnifiedRefund(reqDTO); } catch (Throwable ex) { // 记录异常日志 log.error("[unifiedRefund][request({}) 发起退款失败]", toJsonString(reqDTO), ex); - resp = PayCommonResult.error(ex); + throw buildException(ex); } return resp; } - protected abstract PayCommonResult doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) throws Throwable; + protected abstract PayRefundUnifiedRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) throws Throwable; // ========== 各种工具方法 ========== + private RuntimeException buildException(Throwable ex) { + if (ex instanceof RuntimeException) { + return (RuntimeException) 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); } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AbstractAlipayClient.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AbstractAlipayClient.java index 234011e59..899d4a32b 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AbstractAlipayClient.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AbstractAlipayClient.java @@ -2,11 +2,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.iocoder.yudao.framework.pay.core.client.AbstractPayCodeMapping; -import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayNotifyDataDTO; -import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayRefundNotifyDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayOrderNotifyRespDTO; +import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayRefundNotifyDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedRespDTO; import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; @@ -37,9 +35,8 @@ public abstract class AbstractAlipayClient extends AbstractPayClient doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) { + protected PayRefundUnifiedRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) { AlipayTradeRefundModel model=new AlipayTradeRefundModel(); model.setTradeNo(reqDTO.getChannelOrderNo()); model.setOutTradeNo(reqDTO.getPayTradeNo()); @@ -127,19 +124,20 @@ public abstract class AbstractAlipayClient extends AbstractPayClient当面付 * * @author 芋道源码 */ @@ -25,12 +27,16 @@ import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString public class AlipayBarPayClient extends AbstractAlipayClient { public AlipayBarPayClient(Long channelId, AlipayPayClientConfig config) { - super(channelId, PayChannelEnum.ALIPAY_BAR.getCode(), config, - new AlipayPayCodeMapping()); + super(channelId, PayChannelEnum.ALIPAY_BAR.getCode(), config); } @Override - public PayCommonResult doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { + public PayOrderUnifiedRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) throws AlipayApiException { + String authCode = MapUtil.getStr(reqDTO.getChannelExtras(), "auth_code"); + if (StrUtil.isEmpty(authCode)) { + throw exception0(BAD_REQUEST.getCode(), "条形码不能为空"); + } + // 1.1 构建 AlipayTradePayModel 请求 AlipayTradePayModel model = new AlipayTradePayModel(); // ① 通用的参数 @@ -39,7 +45,8 @@ public class AlipayBarPayClient extends AbstractAlipayClient { model.setBody(reqDTO.getBody()); model.setTotalAmount(formatAmount(reqDTO.getAmount())); model.setScene("bar_code"); // 当面付条码支付场景 - // ② 个性化的参数【无】 + // ② 个性化的参数 + model.setAuthCode(authCode); // ③ 支付宝条码支付只有一种展示 String displayMode = PayDisplayModeEnum.BAR_CODE.getMode(); @@ -47,20 +54,13 @@ public class AlipayBarPayClient extends AbstractAlipayClient { AlipayTradePayRequest request = new AlipayTradePayRequest(); request.setBizModel(model); request.setNotifyUrl(reqDTO.getNotifyUrl()); - request.setReturnUrl(reqDTO.getReturnUrl()); // TODO 芋艿,待搞 + request.setReturnUrl(reqDTO.getReturnUrl()); // 2.1 执行请求 - AlipayTradePayResponse response; - try { - response = client.execute(request); - } catch (AlipayApiException e) { - log.error("[unifiedOrder][request({}) 发起支付失败]", toJsonString(reqDTO), e); - return PayCommonResult.build(e.getErrCode(), e.getErrMsg(), null, codeMapping); - } + AlipayTradePayResponse response = client.execute(request); // 2.2 处理结果 - PayOrderUnifiedRespDTO respDTO = new PayOrderUnifiedRespDTO() - .setDisplayMode(displayMode).setDisplayContent(response.getCode()); - return PayCommonResult.build(StrUtil.blankToDefault(response.getCode(),"10000"), - response.getMsg(), respDTO, codeMapping); + validateSuccess(response); + return new PayOrderUnifiedRespDTO() + .setDisplayMode(displayMode).setDisplayContent(""); } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPayCodeMapping.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPayCodeMapping.java deleted file mode 100644 index d9662a01b..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPayCodeMapping.java +++ /dev/null @@ -1,28 +0,0 @@ -package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; - -import cn.iocoder.yudao.framework.common.exception.ErrorCode; -import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; -import cn.iocoder.yudao.framework.pay.core.client.AbstractPayCodeMapping; - -import java.util.Objects; - -/** - * 支付宝的 PayCodeMapping 实现类 - * - * @author 芋道源码 - */ -public class AlipayPayCodeMapping extends AbstractPayCodeMapping { - - @Override - protected ErrorCode apply0(String apiCode, String apiMsg) { - if (Objects.equals(apiCode, "10000")) { - return GlobalErrorCodeConstants.SUCCESS; - } - // alipay wap api code 返回为null, 暂时定为-9999 - if (Objects.equals(apiCode, "-9999")) { - return GlobalErrorCodeConstants.SUCCESS; - } - return null; - } - -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPcPayClient.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPcPayClient.java index a33f39586..f5626a770 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPcPayClient.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPcPayClient.java @@ -3,8 +3,6 @@ package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.Method; -import cn.iocoder.yudao.framework.common.util.json.JsonUtils; -import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; 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.enums.PayChannelEnum; @@ -18,9 +16,9 @@ import lombok.extern.slf4j.Slf4j; import java.util.Objects; /** - * 支付宝【PC 网站支付】的 PayClient 实现类 + * 支付宝【PC 网站】的 PayClient 实现类 * - * 文档:https://opendocs.alipay.com/open/270/105898 + * 文档:电脑网站支付 * * @author XGD */ @@ -28,12 +26,11 @@ import java.util.Objects; public class AlipayPcPayClient extends AbstractAlipayClient { public AlipayPcPayClient(Long channelId, AlipayPayClientConfig config) { - super(channelId, PayChannelEnum.ALIPAY_PC.getCode(), config, - new AlipayPayCodeMapping()); + super(channelId, PayChannelEnum.ALIPAY_PC.getCode(), config); } @Override - public PayCommonResult doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { + public PayOrderUnifiedRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) throws AlipayApiException { // 1.1 构建 AlipayTradePagePayModel 请求 AlipayTradePagePayModel model = new AlipayTradePagePayModel(); // ① 通用的参数 @@ -54,26 +51,20 @@ public class AlipayPcPayClient extends AbstractAlipayClient { AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); request.setBizModel(model); request.setNotifyUrl(reqDTO.getNotifyUrl()); - request.setReturnUrl(""); // TODO 芋艿,待搞 + request.setReturnUrl(reqDTO.getReturnUrl()); // 2.1 执行请求 AlipayTradePagePayResponse response; - try { - if (Objects.equals(displayMode, PayDisplayModeEnum.FORM.getMode())) { - response = client.pageExecute(request, Method.POST.name()); // 需要特殊使用 POST 请求 - } else { - response = client.pageExecute(request, Method.GET.name()); - } - } catch (AlipayApiException e) { - log.error("[unifiedOrder][request({}) 发起支付失败]", JsonUtils.toJsonString(reqDTO), e); - return PayCommonResult.build(e.getErrCode(), e.getErrMsg(), null, codeMapping); + if (Objects.equals(displayMode, PayDisplayModeEnum.FORM.getMode())) { + response = client.pageExecute(request, Method.POST.name()); // 需要特殊使用 POST 请求 + } else { + response = client.pageExecute(request, Method.GET.name()); } // 2.2 处理结果 - PayOrderUnifiedRespDTO respDTO = new PayOrderUnifiedRespDTO() - .setDisplayMode(displayMode).setDisplayContent(response.getBody()); - return PayCommonResult.build(StrUtil.blankToDefault(response.getCode(),"10000"), - response.getMsg(), respDTO, codeMapping); + validateSuccess(response); + return new PayOrderUnifiedRespDTO().setDisplayMode(displayMode) + .setDisplayContent(response.getBody()); } /** diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayQrPayClient.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayQrPayClient.java index 8742741e4..4080824d1 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayQrPayClient.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayQrPayClient.java @@ -1,8 +1,5 @@ package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; -import cn.hutool.core.util.ObjectUtil; -import cn.hutool.core.util.StrUtil; -import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; 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.enums.PayChannelEnum; @@ -13,12 +10,10 @@ import com.alipay.api.request.AlipayTradePrecreateRequest; import com.alipay.api.response.AlipayTradePrecreateResponse; import lombok.extern.slf4j.Slf4j; -import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; - /** * 支付宝【扫码支付】的 PayClient 实现类 * - * 文档:https://opendocs.alipay.com/apis/02890k + * 文档:扫码支付 * * @author 芋道源码 */ @@ -26,12 +21,11 @@ import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString public class AlipayQrPayClient extends AbstractAlipayClient { public AlipayQrPayClient(Long channelId, AlipayPayClientConfig config) { - super(channelId, PayChannelEnum.ALIPAY_QR.getCode(), config, - new AlipayPayCodeMapping()); + super(channelId, PayChannelEnum.ALIPAY_QR.getCode(), config); } @Override - public PayCommonResult doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { + public PayOrderUnifiedRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) throws AlipayApiException { // 1.1 构建 AlipayTradePrecreateModel 请求 AlipayTradePrecreateModel model = new AlipayTradePrecreateModel(); // ① 通用的参数 @@ -39,7 +33,7 @@ public class AlipayQrPayClient extends AbstractAlipayClient { model.setSubject(reqDTO.getSubject()); model.setBody(reqDTO.getBody()); model.setTotalAmount(formatAmount(reqDTO.getAmount())); - model.setProductCode("FACE_TO_FACE_PAYMENT"); // 销售产品码. 目前扫码支付场景下仅支持 QUICK_WAP_PAY + model.setProductCode("FACE_TO_FACE_PAYMENT"); // 销售产品码. 目前扫码支付场景下仅支持 FACE_TO_FACE_PAYMENT // ② 个性化的参数【无】 // ③ 支付宝扫码支付只有一种展示,考虑到前端可能希望二维码扫描后,手机打开 String displayMode = PayDisplayModeEnum.QR_CODE.getMode(); @@ -48,20 +42,13 @@ public class AlipayQrPayClient extends AbstractAlipayClient { AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest(); request.setBizModel(model); request.setNotifyUrl(reqDTO.getNotifyUrl()); - request.setReturnUrl(reqDTO.getReturnUrl()); // TODO 芋艿,待搞 + request.setReturnUrl(reqDTO.getReturnUrl()); // 2.1 执行请求 - AlipayTradePrecreateResponse response; - try { - response = client.execute(request); - } catch (AlipayApiException e) { - log.error("[unifiedOrder][request({}) 发起支付失败]", toJsonString(reqDTO), e); - return PayCommonResult.build(e.getErrCode(), e.getErrMsg(), null, codeMapping); - } + AlipayTradePrecreateResponse response = client.execute(request); // 2.2 处理结果 - PayOrderUnifiedRespDTO respDTO = new PayOrderUnifiedRespDTO() + validateSuccess(response); + return new PayOrderUnifiedRespDTO() .setDisplayMode(displayMode).setDisplayContent(response.getQrCode()); - return PayCommonResult.build(StrUtil.blankToDefault(response.getCode(),"10000"), - response.getMsg(), respDTO, codeMapping); } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayWapPayClient.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayWapPayClient.java index a7407544e..68b91131c 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayWapPayClient.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayWapPayClient.java @@ -1,9 +1,7 @@ package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; import cn.hutool.core.util.ObjectUtil; -import cn.hutool.core.util.StrUtil; import cn.hutool.http.Method; -import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; 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.enums.PayChannelEnum; @@ -14,12 +12,10 @@ import com.alipay.api.request.AlipayTradeWapPayRequest; import com.alipay.api.response.AlipayTradeWapPayResponse; import lombok.extern.slf4j.Slf4j; -import java.util.Objects; - /** - * 支付宝【手机 网站】的 PayClient 实现类 + * 支付宝【Wap 网站】的 PayClient 实现类 * - * 文档:https://opendocs.alipay.com/apis/api_1/alipay.trade.wap.pay + * 文档:手机网站支付接口 * * @author 芋道源码 */ @@ -27,12 +23,11 @@ import java.util.Objects; public class AlipayWapPayClient extends AbstractAlipayClient { public AlipayWapPayClient(Long channelId, AlipayPayClientConfig config) { - super(channelId, PayChannelEnum.ALIPAY_WAP.getCode(), config, - new AlipayPayCodeMapping()); + super(channelId, PayChannelEnum.ALIPAY_WAP.getCode(), config); } @Override - public PayCommonResult doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { + public PayOrderUnifiedRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) throws AlipayApiException { // 1.1 构建 AlipayTradeWapPayModel 请求 AlipayTradeWapPayModel model = new AlipayTradeWapPayModel(); // ① 通用的参数 @@ -50,23 +45,16 @@ public class AlipayWapPayClient extends AbstractAlipayClient { AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); request.setBizModel(model); request.setNotifyUrl(reqDTO.getNotifyUrl()); - request.setReturnUrl(reqDTO.getReturnUrl()); // TODO 芋艿,待搞 - model.setQuitUrl(reqDTO.getReturnUrl()); // TODO 芋艿,待搞 + request.setReturnUrl(reqDTO.getReturnUrl()); + model.setQuitUrl(reqDTO.getReturnUrl()); // 2.1 执行请求 - AlipayTradeWapPayResponse response; - try { - response = client.pageExecute(request, Method.GET.name()); - } catch (AlipayApiException e) { - return PayCommonResult.build(e.getErrCode(), e.getErrMsg(), null, codeMapping); - } - System.out.println(response.getBody()); + AlipayTradeWapPayResponse response = client.pageExecute(request, Method.GET.name()); // 2.2 处理结果 - PayOrderUnifiedRespDTO respDTO = new PayOrderUnifiedRespDTO() + validateSuccess(response); + return new PayOrderUnifiedRespDTO() .setDisplayMode(displayMode).setDisplayContent(response.getBody()); - return PayCommonResult.build(StrUtil.blankToDefault(response.getCode(),"10000"), - response.getMsg(), respDTO, codeMapping); } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXCodeMapping.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXCodeMapping.java deleted file mode 100644 index cb5e872e7..000000000 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXCodeMapping.java +++ /dev/null @@ -1,56 +0,0 @@ -package cn.iocoder.yudao.framework.pay.core.client.impl.wx; - -import cn.hutool.core.util.StrUtil; -import cn.iocoder.yudao.framework.common.exception.ErrorCode; -import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; -import cn.iocoder.yudao.framework.pay.core.client.AbstractPayCodeMapping; - -import java.util.Objects; - -import static cn.iocoder.yudao.framework.pay.core.enums.PayFrameworkErrorCodeConstants.*; - -/** - * 微信支付 PayCodeMapping 实现类 - * - * @author 芋道源码 - */ -public class WXCodeMapping extends AbstractPayCodeMapping { - - /** - * 错误码 - 成功 - * 由于 weixin-java-pay 封装的 Result 未返回 code,所以自己定义下 - */ - public static final String CODE_SUCCESS = "SUCCESS"; - /** - * 错误提示 - 成功 - */ - public static final String MESSAGE_SUCCESS = "成功"; - - @Override - protected ErrorCode apply0(String apiCode, String apiMsg) { - if (Objects.equals(apiCode, CODE_SUCCESS)) { - return GlobalErrorCodeConstants.SUCCESS; - } - if (Objects.equals(apiCode, "FAIL")) { - if (Objects.equals(apiMsg, "AppID不存在,请检查后再试")) { - return PAY_CONFIG_APP_ID_ERROR; - } - if (Objects.equals(apiMsg, "签名错误,请检查后再试") - || Objects.equals(apiMsg, "签名错误")) { - return PAY_CONFIG_SIGN_ERROR; - } - } - if (Objects.equals(apiCode, "PARAM_ERROR")) { - if (Objects.equals(apiMsg, "无效的openid")) { - return PAY_OPENID_ERROR; - } - } - if (Objects.equals(apiCode, "CustomErrorCode")) { - if (StrUtil.contains(apiMsg, "必填字段")) { - return PAY_PARAM_MISSING; - } - } - return null; - } - -} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXLitePayClient.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXLitePayClient.java index 78c1a234f..48527d198 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXLitePayClient.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXLitePayClient.java @@ -7,12 +7,11 @@ import cn.hutool.core.lang.Assert; import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.util.io.FileUtils; -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; -import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayNotifyDataDTO; -import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayRefundNotifyDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayOrderNotifyRespDTO; +import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayRefundNotifyDTO; 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 cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; @@ -35,11 +34,6 @@ import java.time.ZoneId; import java.util.Date; import java.util.Objects; -import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; -import static cn.iocoder.yudao.framework.pay.core.client.impl.wx.WXCodeMapping.CODE_SUCCESS; -import static cn.iocoder.yudao.framework.pay.core.client.impl.wx.WXCodeMapping.MESSAGE_SUCCESS; - - /** * 微信小程序下支付 * @@ -51,7 +45,7 @@ public class WXLitePayClient extends AbstractPayClient { private WxPayService client; public WXLitePayClient(Long channelId, WXPayClientConfig config) { - super(channelId, PayChannelEnum.WX_LITE.getCode(), config, new WXCodeMapping()); + super(channelId, PayChannelEnum.WX_LITE.getCode(), config); } @Override @@ -76,28 +70,29 @@ public class WXLitePayClient extends AbstractPayClient { } @Override - public PayCommonResult doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { - WxPayMpOrderResult response; - try { - switch (config.getApiVersion()) { - case WXPayClientConfig.API_VERSION_V2: - response = this.unifiedOrderV2(reqDTO); - break; - case WXPayClientConfig.API_VERSION_V3: - WxPayUnifiedOrderV3Result.JsapiResult responseV3 = this.unifiedOrderV3(reqDTO); - // 将 V3 的结果,统一转换成 V2。返回的字段是一致的 - response = new WxPayMpOrderResult(); - BeanUtil.copyProperties(responseV3, response, true); - break; - default: - throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); - } - } catch (WxPayException e) { - log.error("[unifiedOrder][request({}) 发起支付失败,原因({})]", toJsonString(reqDTO), e); - return PayCommonResult.build(ObjectUtils.defaultIfNull(e.getErrCode(), e.getReturnCode(), "CustomErrorCode"), - ObjectUtils.defaultIfNull(e.getErrCodeDes(), e.getCustomErrorMsg()), null, codeMapping); - } - return PayCommonResult.build(CODE_SUCCESS, MESSAGE_SUCCESS, response, codeMapping); + public PayOrderUnifiedRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { + throw new UnsupportedOperationException(); +// WxPayMpOrderResult response; +// try { +// switch (config.getApiVersion()) { +// case WXPayClientConfig.API_VERSION_V2: +// response = this.unifiedOrderV2(reqDTO); +// break; +// case WXPayClientConfig.API_VERSION_V3: +// WxPayUnifiedOrderV3Result.JsapiResult responseV3 = this.unifiedOrderV3(reqDTO); +// // 将 V3 的结果,统一转换成 V2。返回的字段是一致的 +// response = new WxPayMpOrderResult(); +// BeanUtil.copyProperties(responseV3, response, true); +// break; +// default: +// throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); +// } +// } catch (WxPayException e) { +// log.error("[unifiedOrder][request({}) 发起支付失败,原因({})]", toJsonString(reqDTO), e); +// return PayCommonResult.build(ObjectUtils.defaultIfNull(e.getErrCode(), e.getReturnCode(), "CustomErrorCode"), +// ObjectUtils.defaultIfNull(e.getErrCodeDes(), e.getCustomErrorMsg()), null, codeMapping); +// } +// return PayCommonResult.build(CODE_SUCCESS, MESSAGE_SUCCESS, response, codeMapping); } private WxPayMpOrderResult unifiedOrderV2(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { @@ -204,7 +199,7 @@ public class WXLitePayClient extends AbstractPayClient { @Override - protected PayCommonResult doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) throws Throwable { + protected PayRefundUnifiedRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) { //TODO 需要实现 throw new UnsupportedOperationException(); } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXNativePayClient.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXNativePayClient.java index 14251b746..e04d1d3fe 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXNativePayClient.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXNativePayClient.java @@ -6,12 +6,11 @@ import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.util.io.FileUtils; -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; -import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayNotifyDataDTO; -import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayRefundNotifyDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayOrderNotifyRespDTO; +import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayRefundNotifyDTO; 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 cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; @@ -33,10 +32,6 @@ import java.time.ZoneId; import java.util.Date; import java.util.Objects; -import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; -import static cn.iocoder.yudao.framework.pay.core.client.impl.wx.WXCodeMapping.CODE_SUCCESS; -import static cn.iocoder.yudao.framework.pay.core.client.impl.wx.WXCodeMapping.MESSAGE_SUCCESS; - /** * 微信 App 支付 * @@ -48,7 +43,7 @@ public class WXNativePayClient extends AbstractPayClient { private WxPayService client; public WXNativePayClient(Long channelId, WXPayClientConfig config) { - super(channelId, PayChannelEnum.WX_NATIVE.getCode(), config, new WXCodeMapping()); + super(channelId, PayChannelEnum.WX_NATIVE.getCode(), config); } @Override @@ -73,27 +68,28 @@ public class WXNativePayClient extends AbstractPayClient { } @Override - public PayCommonResult doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { - // 这里原生的返回的是支付的 url 所以直接使用string接收 - // "invokeResponse": "weixin://wxpay/bizpayurl?pr=EGYAem7zz" - String responseV3; - try { - switch (config.getApiVersion()) { - case WXPayClientConfig.API_VERSION_V2: - responseV3 = unifiedOrderV2(reqDTO).getCodeUrl(); - break; - case WXPayClientConfig.API_VERSION_V3: - responseV3 = this.unifiedOrderV3(reqDTO); - break; - default: - throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); - } - } catch (WxPayException e) { - log.error("[unifiedOrder][request({}) 发起支付失败,原因({})]", toJsonString(reqDTO), e); - return PayCommonResult.build(ObjectUtils.defaultIfNull(e.getErrCode(), e.getReturnCode(), "CustomErrorCode"), - ObjectUtils.defaultIfNull(e.getErrCodeDes(), e.getCustomErrorMsg()), null, codeMapping); - } - return PayCommonResult.build(CODE_SUCCESS, MESSAGE_SUCCESS, responseV3, codeMapping); + public PayOrderUnifiedRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { + throw new UnsupportedOperationException(); +// // 这里原生的返回的是支付的 url 所以直接使用string接收 +// // "invokeResponse": "weixin://wxpay/bizpayurl?pr=EGYAem7zz" +// String responseV3; +// try { +// switch (config.getApiVersion()) { +// case WXPayClientConfig.API_VERSION_V2: +// responseV3 = unifiedOrderV2(reqDTO).getCodeUrl(); +// break; +// case WXPayClientConfig.API_VERSION_V3: +// responseV3 = this.unifiedOrderV3(reqDTO); +// break; +// default: +// throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); +// } +// } catch (WxPayException e) { +// log.error("[unifiedOrder][request({}) 发起支付失败,原因({})]", toJsonString(reqDTO), e); +// return PayCommonResult.build(ObjectUtils.defaultIfNull(e.getErrCode(), e.getReturnCode(), "CustomErrorCode"), +// ObjectUtils.defaultIfNull(e.getErrCodeDes(), e.getCustomErrorMsg()), null, codeMapping); +// } +// return PayCommonResult.build(CODE_SUCCESS, MESSAGE_SUCCESS, responseV3, codeMapping); } private WxPayNativeOrderResult unifiedOrderV2(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { @@ -186,7 +182,7 @@ public class WXNativePayClient extends AbstractPayClient { @Override - protected PayCommonResult doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) throws Throwable { + protected PayRefundUnifiedRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) { // TODO 需要实现 throw new UnsupportedOperationException(); } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXPubPayClient.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXPubPayClient.java index a5943d601..b2866efee 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXPubPayClient.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXPubPayClient.java @@ -7,12 +7,11 @@ import cn.hutool.core.lang.Assert; import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.util.io.FileUtils; -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; -import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayNotifyDataDTO; -import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayRefundNotifyDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayOrderNotifyRespDTO; +import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayRefundNotifyDTO; 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 cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; @@ -35,10 +34,6 @@ import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Objects; -import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; -import static cn.iocoder.yudao.framework.pay.core.client.impl.wx.WXCodeMapping.CODE_SUCCESS; -import static cn.iocoder.yudao.framework.pay.core.client.impl.wx.WXCodeMapping.MESSAGE_SUCCESS; - /** * 微信支付(公众号)的 PayClient 实现类 * @@ -50,7 +45,7 @@ public class WXPubPayClient extends AbstractPayClient { private WxPayService client; public WXPubPayClient(Long channelId, WXPayClientConfig config) { - super(channelId, PayChannelEnum.WX_PUB.getCode(), config, new WXCodeMapping()); + super(channelId, PayChannelEnum.WX_PUB.getCode(), config); } @Override @@ -75,28 +70,30 @@ public class WXPubPayClient extends AbstractPayClient { } @Override - public PayCommonResult doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { - WxPayMpOrderResult response; - try { - switch (config.getApiVersion()) { - case WXPayClientConfig.API_VERSION_V2: - response = this.unifiedOrderV2(reqDTO); - break; - case WXPayClientConfig.API_VERSION_V3: - WxPayUnifiedOrderV3Result.JsapiResult responseV3 = this.unifiedOrderV3(reqDTO); - // 将 V3 的结果,统一转换成 V2。返回的字段是一致的 - response = new WxPayMpOrderResult(); - BeanUtil.copyProperties(responseV3, response, true); - break; - default: - throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); - } - } catch (WxPayException e) { - log.error("[unifiedOrder][request({}) 发起支付失败,原因({})]", toJsonString(reqDTO), e); - return PayCommonResult.build(ObjectUtils.defaultIfNull(e.getErrCode(), e.getReturnCode(), "CustomErrorCode"), - ObjectUtils.defaultIfNull(e.getErrCodeDes(), e.getCustomErrorMsg()),null, codeMapping); - } - return PayCommonResult.build(CODE_SUCCESS, MESSAGE_SUCCESS, response, codeMapping); + public PayOrderUnifiedRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { + throw new UnsupportedOperationException(); +// +// WxPayMpOrderResult response; +// try { +// switch (config.getApiVersion()) { +// case WXPayClientConfig.API_VERSION_V2: +// response = this.unifiedOrderV2(reqDTO); +// break; +// case WXPayClientConfig.API_VERSION_V3: +// WxPayUnifiedOrderV3Result.JsapiResult responseV3 = this.unifiedOrderV3(reqDTO); +// // 将 V3 的结果,统一转换成 V2。返回的字段是一致的 +// response = new WxPayMpOrderResult(); +// BeanUtil.copyProperties(responseV3, response, true); +// break; +// default: +// throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); +// } +// } catch (WxPayException e) { +// log.error("[unifiedOrder][request({}) 发起支付失败,原因({})]", toJsonString(reqDTO), e); +// return PayCommonResult.build(ObjectUtils.defaultIfNull(e.getErrCode(), e.getReturnCode(), "CustomErrorCode"), +// ObjectUtils.defaultIfNull(e.getErrCodeDes(), e.getCustomErrorMsg()),null, codeMapping); +// } +// return PayCommonResult.build(CODE_SUCCESS, MESSAGE_SUCCESS, response, codeMapping); } @@ -196,7 +193,7 @@ public class WXPubPayClient extends AbstractPayClient { } @Override - protected PayCommonResult doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) throws Throwable { + protected PayRefundUnifiedRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) throws Throwable { // TODO 需要实现 throw new UnsupportedOperationException(); } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/enums/PayFrameworkErrorCodeConstants.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/enums/PayFrameworkErrorCodeConstants.java index 7046b4c6f..ca76059ef 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/enums/PayFrameworkErrorCodeConstants.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/enums/PayFrameworkErrorCodeConstants.java @@ -5,7 +5,7 @@ import cn.iocoder.yudao.framework.common.exception.ErrorCode; /** * 支付框架的错误码枚举 * - * 短信框架,使用 2-002-000-000 段 + * 支付框架,使用 2-002-000-000 段 * * @author 芋道源码 */ @@ -14,14 +14,16 @@ public interface PayFrameworkErrorCodeConstants { ErrorCode PAY_UNKNOWN = new ErrorCode(2002000000, "未知错误,需要解析"); // ========== 配置相关相关 2002000100 ========== + // todo 芋艿:如下的错误码,怎么处理掉 ErrorCode PAY_CONFIG_APP_ID_ERROR = new ErrorCode(2002000100, "支付渠道 AppId 不正确"); ErrorCode PAY_CONFIG_SIGN_ERROR = new ErrorCode(2002000100, "签名错误"); // 例如说,微信支付,配置错了 mchId 或者 mchKey // ========== 其它相关 2002000900 开头 ========== + // todo 芋艿:如下的错误码,怎么处理掉 ErrorCode PAY_OPENID_ERROR = new ErrorCode(2002000900, "无效的 openid"); // 例如说,微信 openid 未授权过 ErrorCode PAY_PARAM_MISSING = new ErrorCode(2002000901, "请求参数缺失"); // 例如说,支付少传了金额 - ErrorCode EXCEPTION = new ErrorCode(2002000999, "调用异常"); + ErrorCode PAY_EXCEPTION = new ErrorCode(2002000999, "调用异常"); } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn.iocoder.yudao.framework.pay.core.client.impl/PayClientFactoryImplIntegrationTest.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn.iocoder.yudao.framework.pay.core.client.impl/PayClientFactoryImplIntegrationTest.java index 0066810e2..071f53d12 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn.iocoder.yudao.framework.pay.core.client.impl/PayClientFactoryImplIntegrationTest.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn.iocoder.yudao.framework.pay.core.client.impl/PayClientFactoryImplIntegrationTest.java @@ -46,8 +46,8 @@ public class PayClientFactoryImplIntegrationTest { PayClient client = payClientFactory.getPayClient(channelId); // 发起支付 PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); - CommonResult result = client.unifiedOrder(reqDTO); - System.out.println(result); +// CommonResult result = client.unifiedOrder(reqDTO); +// System.out.println(result); } /** @@ -69,8 +69,8 @@ public class PayClientFactoryImplIntegrationTest { PayClient client = payClientFactory.getPayClient(channelId); // 发起支付 PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); - CommonResult result = client.unifiedOrder(reqDTO); - System.out.println(result); +// CommonResult result = client.unifiedOrder(reqDTO); +// System.out.println(result); } /** @@ -93,9 +93,9 @@ public class PayClientFactoryImplIntegrationTest { // 发起支付 PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); reqDTO.setNotifyUrl("http://yunai.natapp1.cc/admin-api/pay/notify/callback/18"); // TODO @tina: 这里改成你的 natapp 回调地址 - CommonResult result = (CommonResult) client.unifiedOrder(reqDTO); - System.out.println(JsonUtils.toJsonString(result)); - System.out.println(result.getData().getQrCode()); +// CommonResult result = (CommonResult) client.unifiedOrder(reqDTO); +// System.out.println(JsonUtils.toJsonString(result)); +// System.out.println(result.getData().getQrCode()); } /** @@ -116,8 +116,8 @@ public class PayClientFactoryImplIntegrationTest { PayClient client = payClientFactory.getPayClient(channelId); // 发起支付 PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); - CommonResult result = client.unifiedOrder(reqDTO); - System.out.println(JsonUtils.toJsonString(result)); +// CommonResult result = client.unifiedOrder(reqDTO); +// System.out.println(JsonUtils.toJsonString(result)); } private static PayOrderUnifiedReqDTO buildPayOrderUnifiedReqDTO() { diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn.iocoder.yudao.framework.pay.core.client.impl/alipay/AlipayQrPayClientTest.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn.iocoder.yudao.framework.pay.core.client.impl/alipay/AlipayQrPayClientTest.java index d75651488..78939aa83 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn.iocoder.yudao.framework.pay.core.client.impl/alipay/AlipayQrPayClientTest.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn.iocoder.yudao.framework.pay.core.client.impl/alipay/AlipayQrPayClientTest.java @@ -1,7 +1,6 @@ package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; import cn.hutool.core.util.ReflectUtil; import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; -import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; 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.test.core.ut.BaseMockitoUnitTest; @@ -88,13 +87,13 @@ public class AlipayQrPayClientTest extends BaseMockitoUnitTest { }))).thenReturn(response); - PayCommonResult result = client.doUnifiedOrder(reqDTO); - // 断言 - assertEquals(response.getCode(), result.getApiCode()); - assertEquals(response.getMsg(), result.getApiMsg()); - // TODO @tina:这个断言木有过? - assertEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), result.getCode()); - assertEquals(GlobalErrorCodeConstants.SUCCESS.getMsg(), result.getMsg()); +// PayCommonResult result = client.doUnifiedOrder(reqDTO); +// // 断言 +// assertEquals(response.getCode(), result.getApiCode()); +// assertEquals(response.getMsg(), result.getApiMsg()); +// // TODO @tina:这个断言木有过? +// assertEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), result.getCode()); +// assertEquals(GlobalErrorCodeConstants.SUCCESS.getMsg(), result.getMsg()); } } diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceImpl.java index a6a68390b..5df107506 100755 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceImpl.java @@ -150,14 +150,11 @@ public class PayOrderServiceImpl implements PayOrderService { .setReturnUrl(genChannelReturnUrl(channel)) // 订单相关字段 .setAmount(order.getAmount()).setExpireTime(order.getExpireTime()); - CommonResult unifiedOrderResult = client.unifiedOrder(unifiedOrderReqDTO); - unifiedOrderResult.checkError(); - - PayOrderUnifiedRespDTO xx = (PayOrderUnifiedRespDTO) unifiedOrderResult.getData(); + PayOrderUnifiedRespDTO unifiedOrderRespDTO = client.unifiedOrder(unifiedOrderReqDTO); // TODO 轮询三方接口,是否已经支付的任务 // 返回成功 - return PayOrderConvert.INSTANCE.convert(xx); + return PayOrderConvert.INSTANCE.convert(unifiedOrderRespDTO); } private PayOrderDO validatePayOrderCanSubmit(Long id) { diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundServiceImpl.java index d31b682d5..39457a015 100755 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundServiceImpl.java @@ -3,14 +3,13 @@ package cn.iocoder.yudao.module.pay.service.refund; import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; +import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.pay.config.PayProperties; import cn.iocoder.yudao.framework.pay.core.client.PayClient; import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; -import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayNotifyDataDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayRefundNotifyDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; -import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedRespDTO; import cn.iocoder.yudao.framework.pay.core.enums.PayNotifyRefundStatusEnum; import cn.iocoder.yudao.module.pay.api.refund.dto.PayRefundCreateReqDTO; import cn.iocoder.yudao.module.pay.controller.admin.refund.vo.PayRefundExportReqVO; @@ -22,7 +21,6 @@ import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderExtensionDO; import cn.iocoder.yudao.module.pay.dal.dataobject.refund.PayRefundDO; import cn.iocoder.yudao.module.pay.dal.mysql.order.PayOrderMapper; import cn.iocoder.yudao.module.pay.dal.mysql.refund.PayRefundMapper; -import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants; import cn.iocoder.yudao.module.pay.enums.notify.PayNotifyTypeEnum; import cn.iocoder.yudao.module.pay.enums.order.PayOrderNotifyStatusEnum; @@ -169,11 +167,10 @@ public class PayRefundServiceImpl implements PayRefundService { .setNotifyUrl(genChannelPayNotifyUrl(channel)) // TODO 芋艿:优化下 notifyUrl .setReason(reqDTO.getReason()); // 向渠道发起退款申请 - PayCommonResult refundUnifiedResult = client.unifiedRefund(unifiedReqDTO); + client.unifiedRefund(unifiedReqDTO); // 检查是否失败,失败抛出业务异常。 // TODO 渠道的异常记录。 // TODO @jason:可以先打个 warn log 哈; - refundUnifiedResult.checkError(); // 成功在 退款回调中处理 return payRefundDO.getId(); } diff --git a/yudao-ui-admin/src/views/pay/order/submit.vue b/yudao-ui-admin/src/views/pay/order/submit.vue index 5dcfbf783..532cc7354 100644 --- a/yudao-ui-admin/src/views/pay/order/submit.vue +++ b/yudao-ui-admin/src/views/pay/order/submit.vue @@ -56,6 +56,32 @@
+ + + + + + + + + + +
+ 或使用 + (扫码枪/扫码盒) + 扫码 +
+
+
+
+ +