From 3875c80471a285380392581c724a9085382c292a Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 30 Sep 2023 22:08:15 +0800 Subject: [PATCH] wallet:钱包支付,接入 user 付款界面 --- yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/order/PayOrderController.java | 2 +- yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/app/order/AppPayOrderController.java | 18 ++++++++++++++++++ yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/core/WalletPayClient.java | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/core/package-info.java | 4 ---- yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java | 183 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java | 10 ++++++++++ yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/util/PaySeqUtils.java | 54 ------------------------------------------------------ 7 files changed, 212 insertions(+), 242 deletions(-) create mode 100644 yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/core/WalletPayClient.java delete mode 100644 yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/core/package-info.java delete mode 100644 yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java delete mode 100644 yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/util/PaySeqUtils.java diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/order/PayOrderController.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/order/PayOrderController.java index 8967426..c6dd9c1 100755 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/order/PayOrderController.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/order/PayOrderController.java @@ -11,7 +11,7 @@ import cn.iocoder.yudao.module.pay.convert.order.PayOrderConvert; import cn.iocoder.yudao.module.pay.dal.dataobject.app.PayAppDO; import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO; import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderExtensionDO; -import cn.iocoder.yudao.module.pay.framework.pay.wallet.WalletPayClient; +import cn.iocoder.yudao.module.pay.framework.pay.core.WalletPayClient; import cn.iocoder.yudao.module.pay.service.app.PayAppService; import cn.iocoder.yudao.module.pay.service.order.PayOrderService; import com.google.common.collect.Maps; diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/app/order/AppPayOrderController.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/app/order/AppPayOrderController.java index 9100d34..8033b26 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/app/order/AppPayOrderController.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/app/order/AppPayOrderController.java @@ -1,12 +1,15 @@ package cn.iocoder.yudao.module.pay.controller.app.order; import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderRespVO; import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderSubmitRespVO; import cn.iocoder.yudao.module.pay.controller.app.order.vo.AppPayOrderSubmitReqVO; import cn.iocoder.yudao.module.pay.controller.app.order.vo.AppPayOrderSubmitRespVO; import cn.iocoder.yudao.module.pay.convert.order.PayOrderConvert; +import cn.iocoder.yudao.module.pay.framework.pay.core.WalletPayClient; import cn.iocoder.yudao.module.pay.service.order.PayOrderService; +import com.google.common.collect.Maps; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; @@ -16,8 +19,13 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import java.util.Map; +import java.util.Objects; + import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP; +import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId; +import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserType; @Tag(name = "用户 APP - 支付订单") @RestController @@ -40,6 +48,16 @@ public class AppPayOrderController { @PostMapping("/submit") @Operation(summary = "提交支付订单") public CommonResult submitPayOrder(@RequestBody AppPayOrderSubmitReqVO reqVO) { + // 1. 钱包支付事,需要额外传 user_id 和 user_type + if (Objects.equals(reqVO.getChannelCode(), PayChannelEnum.WALLET.getCode())) { + Map channelExtras = reqVO.getChannelExtras() == null ? + Maps.newHashMapWithExpectedSize(2) : reqVO.getChannelExtras(); + channelExtras.put(WalletPayClient.USER_ID_KEY, String.valueOf(getLoginUserId())); + channelExtras.put(WalletPayClient.USER_TYPE_KEY, String.valueOf(getLoginUserType())); + reqVO.setChannelExtras(channelExtras); + } + + // 2. 提交支付 PayOrderSubmitRespVO respVO = payOrderService.submitOrder(reqVO, getClientIP()); return success(PayOrderConvert.INSTANCE.convert3(respVO)); } diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/core/WalletPayClient.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/core/WalletPayClient.java new file mode 100644 index 0000000..2bc6870 --- /dev/null +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/core/WalletPayClient.java @@ -0,0 +1,183 @@ +package cn.iocoder.yudao.module.pay.framework.pay.core; + +import cn.hutool.core.lang.Assert; +import cn.hutool.core.map.MapUtil; +import cn.hutool.extra.spring.SpringUtil; +import cn.iocoder.yudao.framework.common.exception.ServiceException; +import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO; +import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; +import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO; +import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; +import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferRespDTO; +import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferUnifiedReqDTO; +import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; +import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig; +import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; +import cn.iocoder.yudao.framework.pay.core.enums.refund.PayRefundStatusRespEnum; +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.dataobject.wallet.PayWalletTransactionDO; +import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum; +import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum; +import cn.iocoder.yudao.module.pay.service.order.PayOrderService; +import cn.iocoder.yudao.module.pay.service.refund.PayRefundService; +import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService; +import cn.iocoder.yudao.module.pay.service.wallet.PayWalletTransactionService; +import lombok.extern.slf4j.Slf4j; + +import java.util.Map; + +import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR; +import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.PAY_ORDER_EXTENSION_NOT_FOUND; +import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.REFUND_NOT_FOUND; + +/** + * 钱包支付的 PayClient 实现类 + * + * @author jason + */ +@Slf4j +public class WalletPayClient extends AbstractPayClient { + + public static final String USER_ID_KEY = "user_id"; + public static final String USER_TYPE_KEY = "user_type"; + + private PayWalletService wallService; + private PayWalletTransactionService walletTransactionService; + private PayOrderService orderService; + private PayRefundService refundService; + + public WalletPayClient(Long channelId, NonePayClientConfig config) { + super(channelId, PayChannelEnum.WALLET.getCode(), config); + } + + @Override + protected void doInit() { + if (wallService == null) { + wallService = SpringUtil.getBean(PayWalletService.class); + } + if (walletTransactionService == null) { + walletTransactionService = SpringUtil.getBean(PayWalletTransactionService.class); + } + } + + @Override + protected PayOrderRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { + try { + Long userId = MapUtil.getLong(reqDTO.getChannelExtras(), USER_ID_KEY); + Integer userType = MapUtil.getInt(reqDTO.getChannelExtras(), USER_TYPE_KEY); + Assert.notNull(userId, "用户 id 不能为空"); + Assert.notNull(userType, "用户类型不能为空"); + PayWalletTransactionDO transaction = wallService.orderPay(userId, userType, reqDTO.getOutTradeNo(), + reqDTO.getPrice()); + return PayOrderRespDTO.successOf(transaction.getNo(), transaction.getCreator(), + transaction.getCreateTime(), + reqDTO.getOutTradeNo(), transaction); + } catch (Throwable ex) { + log.error("[doUnifiedOrder] 失败", ex); + Integer errorCode = INTERNAL_SERVER_ERROR.getCode(); + String errorMsg = INTERNAL_SERVER_ERROR.getMsg(); + if (ex instanceof ServiceException) { + ServiceException serviceException = (ServiceException) ex; + errorCode = serviceException.getCode(); + errorMsg = serviceException.getMessage(); + } + return PayOrderRespDTO.closedOf(String.valueOf(errorCode), errorMsg, + reqDTO.getOutTradeNo(), ""); + } + } + + @Override + protected PayOrderRespDTO doParseOrderNotify(Map params, String body) { + throw new UnsupportedOperationException("钱包支付无支付回调"); + } + + @Override + protected PayOrderRespDTO doGetOrder(String outTradeNo) { + if (orderService == null) { + orderService = SpringUtil.getBean(PayOrderService.class); + } + PayOrderExtensionDO orderExtension = orderService.getOrderExtensionByNo(outTradeNo); + // 支付交易拓展单不存在, 返回关闭状态 + if (orderExtension == null) { + return PayOrderRespDTO.closedOf(String.valueOf(PAY_ORDER_EXTENSION_NOT_FOUND.getCode()), + PAY_ORDER_EXTENSION_NOT_FOUND.getMsg(), outTradeNo, ""); + } + // 关闭状态 + if (PayOrderStatusEnum.isClosed(orderExtension.getStatus())) { + return PayOrderRespDTO.closedOf(orderExtension.getChannelErrorCode(), + orderExtension.getChannelErrorMsg(), outTradeNo, ""); + } + // 成功状态 + if (PayOrderStatusEnum.isSuccess(orderExtension.getStatus())) { + PayWalletTransactionDO walletTransaction = walletTransactionService.getWalletTransaction( + String.valueOf(orderExtension.getOrderId()), PayWalletBizTypeEnum.PAYMENT); + Assert.notNull(walletTransaction, "支付单 {} 钱包流水不能为空", outTradeNo); + return PayOrderRespDTO.successOf(walletTransaction.getNo(), walletTransaction.getCreator(), + walletTransaction.getCreateTime(), outTradeNo, walletTransaction); + } + // 其它状态为无效状态 + log.error("[doGetOrder] 支付单 {} 的状态不正确", outTradeNo); + throw new IllegalStateException(String.format("支付单[%s] 状态不正确", outTradeNo)); + } + + @Override + protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) { + try { + PayWalletTransactionDO payWalletTransaction = wallService.orderRefund(reqDTO.getOutRefundNo(), + reqDTO.getRefundPrice(), reqDTO.getReason()); + return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getCreateTime(), + reqDTO.getOutRefundNo(), payWalletTransaction); + } catch (Throwable ex) { + log.error("[doUnifiedRefund] 失败", ex); + Integer errorCode = INTERNAL_SERVER_ERROR.getCode(); + String errorMsg = INTERNAL_SERVER_ERROR.getMsg(); + if (ex instanceof ServiceException) { + ServiceException serviceException = (ServiceException) ex; + errorCode = serviceException.getCode(); + errorMsg = serviceException.getMessage(); + } + return PayRefundRespDTO.failureOf(String.valueOf(errorCode), errorMsg, + reqDTO.getOutRefundNo(), ""); + } + } + + @Override + protected PayRefundRespDTO doParseRefundNotify(Map params, String body) { + throw new UnsupportedOperationException("钱包支付无退款回调"); + } + + @Override + protected PayRefundRespDTO doGetRefund(String outTradeNo, String outRefundNo) { + if (refundService == null) { + refundService = SpringUtil.getBean(PayRefundService.class); + } + PayRefundDO payRefund = refundService.getRefundByNo(outRefundNo); + // 支付退款单不存在, 返回退款失败状态 + if (payRefund == null) { + return PayRefundRespDTO.failureOf(String.valueOf(REFUND_NOT_FOUND), REFUND_NOT_FOUND.getMsg(), + outRefundNo, ""); + } + // 退款失败 + if (PayRefundStatusRespEnum.isFailure(payRefund.getStatus())) { + return PayRefundRespDTO.failureOf(payRefund.getChannelErrorCode(), payRefund.getChannelErrorMsg(), + outRefundNo, ""); + } + // 退款成功 + if (PayRefundStatusRespEnum.isSuccess(payRefund.getStatus())) { + PayWalletTransactionDO walletTransaction = walletTransactionService.getWalletTransaction( + String.valueOf(payRefund.getId()), PayWalletBizTypeEnum.PAYMENT_REFUND); + Assert.notNull(walletTransaction, "支付退款单 {} 钱包流水不能为空", outRefundNo); + return PayRefundRespDTO.successOf(walletTransaction.getNo(), walletTransaction.getCreateTime(), + outRefundNo, walletTransaction); + } + // 其它状态为无效状态 + log.error("[doGetRefund] 支付退款单 {} 的状态不正确", outRefundNo); + throw new IllegalStateException(String.format("支付退款单[%s] 状态不正确", outRefundNo)); + } + + @Override + public PayTransferRespDTO doUnifiedTransfer(PayTransferUnifiedReqDTO reqDTO) { + throw new UnsupportedOperationException("待实现"); + } +} diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/core/package-info.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/core/package-info.java deleted file mode 100644 index 07f837f..0000000 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/core/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * 占位,无实际作用 - */ -package cn.iocoder.yudao.module.pay.framework.pay.core; \ No newline at end of file diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java deleted file mode 100644 index 9bd8a6e..0000000 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java +++ /dev/null @@ -1,183 +0,0 @@ -package cn.iocoder.yudao.module.pay.framework.pay.wallet; - -import cn.hutool.core.lang.Assert; -import cn.hutool.core.map.MapUtil; -import cn.hutool.extra.spring.SpringUtil; -import cn.iocoder.yudao.framework.common.exception.ServiceException; -import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO; -import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; -import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO; -import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; -import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferRespDTO; -import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferUnifiedReqDTO; -import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; -import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig; -import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; -import cn.iocoder.yudao.framework.pay.core.enums.refund.PayRefundStatusRespEnum; -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.dataobject.wallet.PayWalletTransactionDO; -import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum; -import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum; -import cn.iocoder.yudao.module.pay.service.order.PayOrderService; -import cn.iocoder.yudao.module.pay.service.refund.PayRefundService; -import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService; -import cn.iocoder.yudao.module.pay.service.wallet.PayWalletTransactionService; -import lombok.extern.slf4j.Slf4j; - -import java.util.Map; - -import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR; -import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.PAY_ORDER_EXTENSION_NOT_FOUND; -import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.REFUND_NOT_FOUND; - -/** - * 钱包支付的 PayClient 实现类 - * - * @author jason - */ -@Slf4j -public class WalletPayClient extends AbstractPayClient { - - public static final String USER_ID_KEY = "user_id"; - public static final String USER_TYPE_KEY = "user_type"; - - private PayWalletService wallService; - private PayWalletTransactionService walletTransactionService; - private PayOrderService orderService; - private PayRefundService refundService; - - public WalletPayClient(Long channelId, NonePayClientConfig config) { - super(channelId, PayChannelEnum.WALLET.getCode(), config); - } - - @Override - protected void doInit() { - if (wallService == null) { - wallService = SpringUtil.getBean(PayWalletService.class); - } - if (walletTransactionService == null) { - walletTransactionService = SpringUtil.getBean(PayWalletTransactionService.class); - } - } - - @Override - protected PayOrderRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { - try { - Long userId = MapUtil.getLong(reqDTO.getChannelExtras(), USER_ID_KEY); - Integer userType = MapUtil.getInt(reqDTO.getChannelExtras(), USER_TYPE_KEY); - Assert.notNull(userId, "用户 id 不能为空"); - Assert.notNull(userType, "用户类型不能为空"); - PayWalletTransactionDO transaction = wallService.orderPay(userId, userType, reqDTO.getOutTradeNo(), - reqDTO.getPrice()); - return PayOrderRespDTO.successOf(transaction.getNo(), transaction.getCreator(), - transaction.getCreateTime(), - reqDTO.getOutTradeNo(), transaction); - } catch (Throwable ex) { - log.error("[doUnifiedOrder] 失败", ex); - Integer errorCode = INTERNAL_SERVER_ERROR.getCode(); - String errorMsg = INTERNAL_SERVER_ERROR.getMsg(); - if (ex instanceof ServiceException) { - ServiceException serviceException = (ServiceException) ex; - errorCode = serviceException.getCode(); - errorMsg = serviceException.getMessage(); - } - return PayOrderRespDTO.closedOf(String.valueOf(errorCode), errorMsg, - reqDTO.getOutTradeNo(), ""); - } - } - - @Override - protected PayOrderRespDTO doParseOrderNotify(Map params, String body) { - throw new UnsupportedOperationException("钱包支付无支付回调"); - } - - @Override - protected PayOrderRespDTO doGetOrder(String outTradeNo) { - if (orderService == null) { - orderService = SpringUtil.getBean(PayOrderService.class); - } - PayOrderExtensionDO orderExtension = orderService.getOrderExtensionByNo(outTradeNo); - // 支付交易拓展单不存在, 返回关闭状态 - if (orderExtension == null) { - return PayOrderRespDTO.closedOf(String.valueOf(PAY_ORDER_EXTENSION_NOT_FOUND.getCode()), - PAY_ORDER_EXTENSION_NOT_FOUND.getMsg(), outTradeNo, ""); - } - // 关闭状态 - if (PayOrderStatusEnum.isClosed(orderExtension.getStatus())) { - return PayOrderRespDTO.closedOf(orderExtension.getChannelErrorCode(), - orderExtension.getChannelErrorMsg(), outTradeNo, ""); - } - // 成功状态 - if (PayOrderStatusEnum.isSuccess(orderExtension.getStatus())) { - PayWalletTransactionDO walletTransaction = walletTransactionService.getWalletTransaction( - String.valueOf(orderExtension.getOrderId()), PayWalletBizTypeEnum.PAYMENT); - Assert.notNull(walletTransaction, "支付单 {} 钱包流水不能为空", outTradeNo); - return PayOrderRespDTO.successOf(walletTransaction.getNo(), walletTransaction.getCreator(), - walletTransaction.getCreateTime(), outTradeNo, walletTransaction); - } - // 其它状态为无效状态 - log.error("[doGetOrder] 支付单 {} 的状态不正确", outTradeNo); - throw new IllegalStateException(String.format("支付单[%s] 状态不正确", outTradeNo)); - } - - @Override - protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) { - try { - PayWalletTransactionDO payWalletTransaction = wallService.orderRefund(reqDTO.getOutRefundNo(), - reqDTO.getRefundPrice(), reqDTO.getReason()); - return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getCreateTime(), - reqDTO.getOutRefundNo(), payWalletTransaction); - } catch (Throwable ex) { - log.error("[doUnifiedRefund] 失败", ex); - Integer errorCode = INTERNAL_SERVER_ERROR.getCode(); - String errorMsg = INTERNAL_SERVER_ERROR.getMsg(); - if (ex instanceof ServiceException) { - ServiceException serviceException = (ServiceException) ex; - errorCode = serviceException.getCode(); - errorMsg = serviceException.getMessage(); - } - return PayRefundRespDTO.failureOf(String.valueOf(errorCode), errorMsg, - reqDTO.getOutRefundNo(), ""); - } - } - - @Override - protected PayRefundRespDTO doParseRefundNotify(Map params, String body) { - throw new UnsupportedOperationException("钱包支付无退款回调"); - } - - @Override - protected PayRefundRespDTO doGetRefund(String outTradeNo, String outRefundNo) { - if (refundService == null) { - refundService = SpringUtil.getBean(PayRefundService.class); - } - PayRefundDO payRefund = refundService.getRefundByNo(outRefundNo); - // 支付退款单不存在, 返回退款失败状态 - if (payRefund == null) { - return PayRefundRespDTO.failureOf(String.valueOf(REFUND_NOT_FOUND), REFUND_NOT_FOUND.getMsg(), - outRefundNo, ""); - } - // 退款失败 - if (PayRefundStatusRespEnum.isFailure(payRefund.getStatus())) { - return PayRefundRespDTO.failureOf(payRefund.getChannelErrorCode(), payRefund.getChannelErrorMsg(), - outRefundNo, ""); - } - // 退款成功 - if (PayRefundStatusRespEnum.isSuccess(payRefund.getStatus())) { - PayWalletTransactionDO walletTransaction = walletTransactionService.getWalletTransaction( - String.valueOf(payRefund.getId()), PayWalletBizTypeEnum.PAYMENT_REFUND); - Assert.notNull(walletTransaction, "支付退款单 {} 钱包流水不能为空", outRefundNo); - return PayRefundRespDTO.successOf(walletTransaction.getNo(), walletTransaction.getCreateTime(), - outRefundNo, walletTransaction); - } - // 其它状态为无效状态 - log.error("[doGetRefund] 支付退款单 {} 的状态不正确", outRefundNo); - throw new IllegalStateException(String.format("支付退款单[%s] 状态不正确", outRefundNo)); - } - - @Override - public PayTransferRespDTO doUnifiedTransfer(PayTransferUnifiedReqDTO reqDTO) { - throw new UnsupportedOperationException("待实现"); - } -} diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java index 293d6c5..2fe495a 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java @@ -13,6 +13,7 @@ import cn.iocoder.yudao.module.pay.controller.admin.channel.vo.PayChannelUpdateR import cn.iocoder.yudao.module.pay.convert.channel.PayChannelConvert; import cn.iocoder.yudao.module.pay.dal.dataobject.channel.PayChannelDO; import cn.iocoder.yudao.module.pay.dal.mysql.channel.PayChannelMapper; +import cn.iocoder.yudao.module.pay.framework.pay.core.WalletPayClient; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import lombok.Getter; @@ -20,6 +21,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; +import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.validation.Validator; import java.time.Duration; @@ -68,6 +70,14 @@ public class PayChannelServiceImpl implements PayChannelService { @Resource private Validator validator; + /** + * 初始化,为了注册钱包 + */ + @PostConstruct + public void init() { + payClientFactory.registerPayClientClass(PayChannelEnum.WALLET, WalletPayClient.class); + } + @Override public Long createChannel(PayChannelCreateReqVO reqVO) { // 断言是否有重复的 diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/util/PaySeqUtils.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/util/PaySeqUtils.java deleted file mode 100644 index 3882a2f..0000000 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/util/PaySeqUtils.java +++ /dev/null @@ -1,54 +0,0 @@ -package cn.iocoder.yudao.module.pay.util; - -import cn.hutool.core.date.DatePattern; -import cn.hutool.core.date.DateUtil; - -import java.time.LocalDateTime; -import java.util.concurrent.atomic.AtomicLong; - -/** - * 支付相关编号的生产 - */ -// TODO @jason:需要改造,基于 db; -public class PaySeqUtils { - - private static final AtomicLong REFUND_REQ_NO_SEQ = new AtomicLong(0L); - - private static final AtomicLong MER_REFUND_NO_SEQ = new AtomicLong(0L); - - private static final AtomicLong MER_ORDER_NO_SEQ = new AtomicLong(0L); - - // TODO 芋艿:需要看看 - /** - * 生成商户退款单号,用于测试,应该由商户系统生成 - * @return 商户退款单 - */ - public static String genMerchantRefundNo() { - return String.format("%s%s%04d", "MR", - DateUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_MS_PATTERN), - (int) MER_REFUND_NO_SEQ.getAndIncrement() % 10000); - } - - // TODO 芋艿:需要看看 - - /** - * 生成退款请求号 - * @return 退款请求号 - */ - public static String genRefundReqNo() { - return String.format("%s%s%04d", "RR", - DateUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_MS_PATTERN), - (int) REFUND_REQ_NO_SEQ.getAndIncrement() % 10000); - } - - /** - * 生成商户订单编号号 用于测试,应该由商户系统生成 - * @return 商户订单编号 - */ - public static String genMerchantOrderNo() { - return String.format("%s%s%04d", "MO", - DateUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_MS_PATTERN), - (int) MER_ORDER_NO_SEQ.getAndIncrement() % 10000); - } - -} -- libgit2 0.21.4