From d06f021d8713548209d066d03966f7437fda086e Mon Sep 17 00:00:00 2001 From: xxm1995 Date: Thu, 13 Jul 2023 11:21:15 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E5=BE=AE=E4=BF=A1=E9=80=80=E6=AC=BE?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wechat/service/WeChatPayCancelService.java | 14 ++++++++++---- .../wechat/service/WeChatPaymentService.java | 16 +++------------- .../strategy/WeChatPayRefundStrategy.java | 17 ++++------------- .../daxpay/dto/payment/RefundableInfo.java | 3 +++ 4 files changed, 20 insertions(+), 30 deletions(-) diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/wechat/service/WeChatPayCancelService.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/wechat/service/WeChatPayCancelService.java index 48e3baf76..004377db3 100644 --- a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/wechat/service/WeChatPayCancelService.java +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/wechat/service/WeChatPayCancelService.java @@ -1,11 +1,12 @@ package cn.bootx.platform.daxpay.core.channel.wechat.service; import cn.bootx.platform.common.spring.exception.RetryableException; +import cn.bootx.platform.daxpay.code.pay.PayChannelEnum; import cn.bootx.platform.daxpay.code.paymodel.WeChatPayCode; import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayConfig; -import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayment; import cn.bootx.platform.daxpay.core.payment.entity.Payment; import cn.bootx.platform.daxpay.core.refund.local.AsyncRefundLocal; +import cn.bootx.platform.daxpay.dto.payment.RefundableInfo; import cn.bootx.platform.daxpay.exception.payment.PayFailureException; import cn.bootx.platform.starter.file.service.FileUploadService; import cn.hutool.core.codec.Base64; @@ -24,6 +25,7 @@ import org.springframework.stereotype.Service; import java.io.ByteArrayInputStream; import java.math.BigDecimal; import java.util.Map; +import java.util.Objects; import java.util.Optional; import static cn.bootx.platform.daxpay.code.pay.PayStatusCode.REFUND_PROCESS_FAIL; @@ -63,10 +65,14 @@ public class WeChatPayCancelService { /** * 退款 */ - public void refund(Payment payment, WeChatPayment weChatPayment, BigDecimal amount, + public void refund(Payment payment, BigDecimal amount, WeChatPayConfig weChatPayConfig) { - String totalFee = weChatPayment.getAmount().multiply(BigDecimal.valueOf(100)).toBigInteger().toString(); + RefundableInfo refundableInfo = payment.getRefundableInfo().stream() + .filter(o -> Objects.equals(o.getPayChannel(), PayChannelEnum.WECHAT.getCode())) + .findFirst() + .orElseThrow(() -> new PayFailureException("未找到微信支付的详细信息")); String refundFee = amount.multiply(BigDecimal.valueOf(100)).toBigInteger().toString(); + String totalFee = refundableInfo.getAmount().multiply(BigDecimal.valueOf(100)).toBigInteger().toString(); // 设置退款号 AsyncRefundLocal.set(IdUtil.getSnowflakeNextIdStr()); Map params = RefundModel.builder() @@ -79,7 +85,7 @@ public class WeChatPayCancelService { .nonce_str(WxPayKit.generateStr()) .build() .createSign(weChatPayConfig.getApiKeyV2(), SignType.HMACSHA256); - // 获取证书文件流 + // 获取证书文件 if (StrUtil.isNotBlank(weChatPayConfig.getP12())){ String errorMsg = "微信p.12证书未配置,无法进行退款"; AsyncRefundLocal.setErrorMsg(errorMsg); diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/wechat/service/WeChatPaymentService.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/wechat/service/WeChatPaymentService.java index 37d8db7fe..0ec224152 100644 --- a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/wechat/service/WeChatPaymentService.java +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/wechat/service/WeChatPaymentService.java @@ -1,14 +1,13 @@ package cn.bootx.platform.daxpay.core.channel.wechat.service; import cn.bootx.platform.common.core.exception.BizException; -import cn.bootx.platform.common.core.util.BigDecimalUtil; import cn.bootx.platform.daxpay.code.pay.PayChannelEnum; import cn.bootx.platform.daxpay.code.pay.PayStatusCode; +import cn.bootx.platform.daxpay.core.channel.wechat.dao.WeChatPaymentManager; +import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayment; import cn.bootx.platform.daxpay.core.pay.local.AsyncPayInfoLocal; import cn.bootx.platform.daxpay.core.payment.entity.Payment; import cn.bootx.platform.daxpay.core.payment.service.PaymentService; -import cn.bootx.platform.daxpay.core.channel.wechat.dao.WeChatPaymentManager; -import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayment; import cn.bootx.platform.daxpay.dto.pay.AsyncPayInfo; import cn.bootx.platform.daxpay.dto.payment.PayChannelInfo; import cn.bootx.platform.daxpay.dto.payment.RefundableInfo; @@ -105,16 +104,7 @@ public class WeChatPaymentService { /** * 更新退款 */ - public void updatePayRefund(WeChatPayment weChatPayment, BigDecimal amount) { - BigDecimal refundableBalance = weChatPayment.getRefundableBalance().subtract(amount); - weChatPayment.setRefundableBalance(refundableBalance); - if (BigDecimalUtil.compareTo(refundableBalance, BigDecimal.ZERO) == 0) { - weChatPayment.setPayStatus(PayStatusCode.TRADE_REFUNDED); - } - else { - weChatPayment.setPayStatus(PayStatusCode.TRADE_REFUNDING); - } - weChatPaymentManager.updateById(weChatPayment); + public void updatePayRefund(Long paymentId, BigDecimal amount) { } } diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/refund/strategy/WeChatPayRefundStrategy.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/refund/strategy/WeChatPayRefundStrategy.java index 5d7d1b9f4..a0de464ef 100644 --- a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/refund/strategy/WeChatPayRefundStrategy.java +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/refund/strategy/WeChatPayRefundStrategy.java @@ -4,7 +4,6 @@ import cn.bootx.platform.daxpay.code.pay.PayChannelEnum; import cn.bootx.platform.daxpay.core.channel.wechat.dao.WeChatPayConfigManager; import cn.bootx.platform.daxpay.core.channel.wechat.dao.WeChatPaymentManager; import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayConfig; -import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayment; import cn.bootx.platform.daxpay.core.channel.wechat.service.WeChatPayCancelService; import cn.bootx.platform.daxpay.core.channel.wechat.service.WeChatPaymentService; import cn.bootx.platform.daxpay.core.payment.service.PaymentService; @@ -47,7 +46,7 @@ public class WeChatPayRefundStrategy extends AbsPayRefundStrategy { } /** - * 退款前对处理 包含必要的校验以及对Payment对象的创建和保存操作 + * 退款前对处理, 初始化微信支付配置 */ @Override public void doBeforeRefundHandler() { @@ -60,18 +59,10 @@ public class WeChatPayRefundStrategy extends AbsPayRefundStrategy { */ @Override public void doRefundHandler() { - WeChatPayment weChatPayment = weChatPaymentManager.findByPaymentId(this.getPayment().getId()) - .orElseThrow(() -> new PayFailureException("微信支付记录不存在")); - weChatPayCancelService.refund(this.getPayment(), weChatPayment, this.getRefundModeParam().getAmount(), - this.weChatPayConfig); - weChatPaymentService.updatePayRefund(weChatPayment, this.getRefundModeParam().getAmount()); + weChatPayCancelService.refund(this.getPayment(), this.getRefundModeParam().getAmount(), this.weChatPayConfig); + weChatPaymentService.updatePayRefund(this.getPayment().getId(), this.getRefundModeParam().getAmount()); paymentService.updateRefundSuccess(this.getPayment(), this.getRefundModeParam().getAmount(), PayChannelEnum.WECHAT); + } - /** - * 初始化微信支付 - */ - private void initWeChatPayConfig(String appCode) { - // 检查并获取微信支付配置 - } } diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/dto/payment/RefundableInfo.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/dto/payment/RefundableInfo.java index e1137d1e5..478b2f9ba 100644 --- a/dax-pay/src/main/java/cn/bootx/platform/daxpay/dto/payment/RefundableInfo.java +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/dto/payment/RefundableInfo.java @@ -17,6 +17,9 @@ import java.math.BigDecimal; @Schema(title = "可退款信息") public class RefundableInfo { + /** + * @see cn.bootx.platform.daxpay.code.pay.PayChannelEnum + */ @Schema(description = "支付通道") private String payChannel;