From fcd16c019aa1e93f4b00385817e3a9552f62d154 Mon Sep 17 00:00:00 2001 From: xxm1995 Date: Fri, 30 Jun 2023 15:57:11 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E5=82=A8=E5=80=BC=E5=8D=A1=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=86=BB=E7=BB=93,=20=E5=A4=9A=E5=8D=A1=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E6=94=AF=E4=BB=98=E6=89=A3=E6=AC=BE=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BC=98=E5=85=88=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../daxpay/code/paymodel/VoucherCode.java | 30 ++- .../core/channel/base/entity/BasePayment.java | 3 + .../channel/voucher/dao/VoucherManager.java | 2 +- .../voucher/entity/VoucherPayment.java | 17 +- .../channel/voucher/entity/VoucherRecord.java | 22 ++ .../voucher/service/VoucherPayService.java | 238 ++++++++++++++---- .../service/VoucherPaymentService.java | 21 +- .../core/pay/strategy/VoucherStrategy.java | 10 +- .../channel/voucher/VoucherRefundParam.java | 23 ++ .../param/channel/wallet/WalletPayParam.java | 1 - .../channel/wallet/WalletRefundParam.java | 16 ++ .../daxpay/param/refund/RefundModeParam.java | 9 + 12 files changed, 320 insertions(+), 72 deletions(-) create mode 100644 dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/entity/VoucherRecord.java create mode 100644 dax-pay/src/main/java/cn/bootx/platform/daxpay/param/channel/voucher/VoucherRefundParam.java create mode 100644 dax-pay/src/main/java/cn/bootx/platform/daxpay/param/channel/wallet/WalletRefundParam.java diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/code/paymodel/VoucherCode.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/code/paymodel/VoucherCode.java index 9bdf4e5d7..fe92aa3e5 100644 --- a/dax-pay/src/main/java/cn/bootx/platform/daxpay/code/paymodel/VoucherCode.java +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/code/paymodel/VoucherCode.java @@ -24,19 +24,37 @@ public interface VoucherCode { String LOG_ACTIVE = "active"; /** - * 储值卡日志-支付 + * 钱包日志-预冻结额度 + */ + String LOG_FREEZE_BALANCE = "freeze"; + + /** + * 钱包日志-解冻额度 + */ + String LOG_UNFREEZE_BALANCE = "unfreeze"; + + /** + * 钱包日志-解冻并扣减余额 + */ + String LOG_REDUCE_AND_UNFREEZE_BALANCE = "reduceAndUnfreeze"; + + /** + * 钱包日志-直接支付 */ String LOG_PAY = "pay"; + /** + * 钱包日志-取消支付 + */ + String LOG_CLOSE_PAY = "closePay"; /** - * 储值卡日志-退款 + * 钱包日志-退款到本卡中 */ - String LOG_CLOSE = "close"; - + String LOG_REFUND_SELF = "refundSelf"; /** - * 储值卡日志-退款 + * 钱包日志-统一退款到指定卡中 */ - String LOG_REFUND = "refund"; + String LOG_REFUND_ONE = "refundOne"; /** * 储值卡日志-Admin余额变动 diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/base/entity/BasePayment.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/base/entity/BasePayment.java index d10282e29..ce16a792d 100644 --- a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/base/entity/BasePayment.java +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/base/entity/BasePayment.java @@ -1,5 +1,6 @@ package cn.bootx.platform.daxpay.core.channel.base.entity; +import cn.bootx.mybatis.table.modify.mybatis.mysq.annotation.DbMySqlIndex; import cn.bootx.platform.common.mybatisplus.base.MpBaseEntity; import cn.bootx.platform.daxpay.code.pay.PayStatusCode; import lombok.Data; @@ -21,6 +22,7 @@ import java.time.LocalDateTime; public class BasePayment extends MpBaseEntity { /** 交易记录ID */ + @DbMySqlIndex(comment = "交易记录索引") private Long paymentId; /** 交易金额 */ @@ -30,6 +32,7 @@ public class BasePayment extends MpBaseEntity { private BigDecimal refundableBalance; /** 关联的业务id */ + @DbMySqlIndex(comment = "业务id索引") private String businessId; /** diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/dao/VoucherManager.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/dao/VoucherManager.java index 4ecdadb2a..d2330fc99 100644 --- a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/dao/VoucherManager.java +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/dao/VoucherManager.java @@ -33,7 +33,7 @@ public class VoucherManager extends BaseManager { return this.lambdaQuery() .ge(Objects.nonNull(param.getStartTime()), Voucher::getStartTime, param.getStartTime()) .le(Objects.nonNull(param.getEndTime()), Voucher::getEndTime, param.getEndTime()) - .eq(Objects.nonNull(param.getEnduring()), Voucher::getEnduring, param.getEnduring()) + .eq(Objects.nonNull(param.getEnduring()), Voucher::isEnduring, param.getEnduring()) .like(StrUtil.isNotBlank(param.getCardNo()), Voucher::getCardNo, param.getCardNo()) .like(Objects.nonNull(param.getBatchNo()), Voucher::getBatchNo, param.getBatchNo()) .orderByDesc(MpIdEntity::getId) diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/entity/VoucherPayment.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/entity/VoucherPayment.java index f45c8e937..c4a820dff 100644 --- a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/entity/VoucherPayment.java +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/entity/VoucherPayment.java @@ -1,14 +1,21 @@ package cn.bootx.platform.daxpay.core.channel.voucher.entity; +import cn.bootx.mybatis.table.modify.annotation.DbColumn; +import cn.bootx.mybatis.table.modify.mybatis.mysq.annotation.DbMySqlFieldType; +import cn.bootx.mybatis.table.modify.mybatis.mysq.constants.MySqlFieldTypeEnum; import cn.bootx.platform.common.core.function.EntityBaseFunction; +import cn.bootx.platform.common.mybatisplus.handler.JacksonRawTypeHandler; import cn.bootx.platform.daxpay.core.channel.base.entity.BasePayment; import cn.bootx.platform.daxpay.core.channel.voucher.convert.VoucherConvert; import cn.bootx.platform.daxpay.dto.channel.voucher.VoucherPaymentDto; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; +import java.util.List; + /** * 储值卡支付记录 * @@ -18,11 +25,15 @@ import lombok.experimental.Accessors; @EqualsAndHashCode(callSuper = true) @Data @Accessors(chain = true) -@TableName("pay_voucher_payment") +@TableName(value = "pay_voucher_payment",autoResultMap = true) public class VoucherPayment extends BasePayment implements EntityBaseFunction { - /** 储值卡id列表 */ - private String voucherIds; + /** 储值卡扣款记录列表 */ + @DbColumn(comment = "储值卡扣款列表") + @DbMySqlFieldType(MySqlFieldTypeEnum.LONGTEXT) + @TableField(typeHandler = JacksonRawTypeHandler.class) + private List voucherRecords; + @Override public VoucherPaymentDto toDto() { diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/entity/VoucherRecord.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/entity/VoucherRecord.java new file mode 100644 index 000000000..fec3c3c53 --- /dev/null +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/entity/VoucherRecord.java @@ -0,0 +1,22 @@ +package cn.bootx.platform.daxpay.core.channel.voucher.entity; + +import lombok.Data; +import lombok.experimental.Accessors; + +import java.math.BigDecimal; + +/** + * 储值卡扣款记录列表 + * @author xxm + * @since 2023/6/29 + */ +@Data +@Accessors(chain = true) +public class VoucherRecord { + + /** 卡号 */ + private String cardNo; + + /** 扣款金额 */ + private BigDecimal amount; +} diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/service/VoucherPayService.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/service/VoucherPayService.java index d42aeea9b..faeaf8cfc 100644 --- a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/service/VoucherPayService.java +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/service/VoucherPayService.java @@ -1,6 +1,7 @@ package cn.bootx.platform.daxpay.core.channel.voucher.service; import cn.bootx.platform.common.core.exception.DataNotExistException; +import cn.bootx.platform.common.core.function.CollectorsFunction; import cn.bootx.platform.common.core.util.BigDecimalUtil; import cn.bootx.platform.common.core.util.LocalDateTimeUtil; import cn.bootx.platform.daxpay.code.paymodel.VoucherCode; @@ -10,9 +11,11 @@ import cn.bootx.platform.daxpay.core.channel.voucher.dao.VoucherPaymentManager; import cn.bootx.platform.daxpay.core.channel.voucher.entity.Voucher; import cn.bootx.platform.daxpay.core.channel.voucher.entity.VoucherLog; import cn.bootx.platform.daxpay.core.channel.voucher.entity.VoucherPayment; +import cn.bootx.platform.daxpay.core.channel.voucher.entity.VoucherRecord; import cn.bootx.platform.daxpay.core.payment.entity.Payment; import cn.bootx.platform.daxpay.exception.payment.PayFailureException; import cn.bootx.platform.daxpay.param.channel.voucher.VoucherPayParam; +import cn.bootx.platform.daxpay.param.channel.voucher.VoucherRefundParam; import cn.bootx.platform.daxpay.param.pay.PayWayParam; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONException; @@ -20,6 +23,7 @@ import cn.hutool.json.JSONUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.time.LocalDateTime; @@ -88,52 +92,121 @@ public class VoucherPayService { /** * 支付前冻结余额 */ - public void freezeBalance(BigDecimal amount, Payment payment, List vouchers){ - - } - - /** - * 支付成功 - */ - public void paySuccess(Long paymentId){ - - } - - /** - * 直接支付 - */ - public void pay(BigDecimal amount, Payment payment, List vouchers) { - vouchers.sort((o1, o2) -> BigDecimalUtil.compareTo(o1.getBalance(), o2.getBalance())); + public List freezeBalance(BigDecimal amount, Payment payment, List vouchers){ List voucherLogs = new ArrayList<>(); - + List voucherRecords = new ArrayList<>(); for (Voucher voucher : vouchers) { // 待支付余额为零, 不在处理后面的储值卡 if (BigDecimalUtil.compareTo(amount, BigDecimal.ZERO) < 1) { break; } - BigDecimal balance = voucher.getBalance(); // 日志 - VoucherLog voucherLog = new VoucherLog().setPaymentId(payment.getId()) - .setBusinessId(payment.getBusinessId()) - .setType(VoucherCode.LOG_PAY) - .setVoucherId(voucher.getId()) - .setVoucherNo(voucher.getCardNo()); + VoucherLog voucherLog = new VoucherLog() + .setPaymentId(payment.getId()) + .setBusinessId(payment.getBusinessId()) + .setType(VoucherCode.LOG_FREEZE_BALANCE) + .setVoucherId(voucher.getId()) + .setVoucherNo(voucher.getCardNo()); + // 待支付额大于储值卡余额. 全冻结 + if (BigDecimalUtil.compareTo(amount, balance) == 1) { + amount = amount.subtract(balance); + voucher.setFreezeBalance(balance); + voucherLog.setAmount(balance); + } + else { + amount = BigDecimal.ZERO; + voucher.setFreezeBalance(amount); + voucherLog.setAmount(amount); + } + voucherRecords.add(new VoucherRecord().setCardNo(voucher.getCardNo()).setAmount(voucher.getFreezeBalance())); + voucherLogs.add(voucherLog); + } + voucherManager.updateAllById(vouchers); + voucherLogManager.saveAll(voucherLogs); + return voucherRecords; + } + + /** + * 支付成功, 对冻结的金额进行扣款 + */ + public void paySuccess(Long paymentId){ + voucherPaymentManager.findByPaymentId(paymentId).ifPresent(voucherPayment -> { + + List voucherLogs = new ArrayList<>(); + for (Voucher voucher : vouchers) { + // 待支付余额为零, 不在处理后面的储值卡 + if (BigDecimalUtil.compareTo(amount, BigDecimal.ZERO) < 1) { + break; + } + BigDecimal balance = voucher.getBalance(); + // 日志 + VoucherLog voucherLog = new VoucherLog() + .setPaymentId(payment.getId()) + .setBusinessId(payment.getBusinessId()) + .setType(VoucherCode.LOG_FREEZE_BALANCE) + .setVoucherId(voucher.getId()) + .setVoucherNo(voucher.getCardNo()); + + // 待支付额大于储值卡余额. 全冻结 + if (BigDecimalUtil.compareTo(amount, balance) == 1) { + amount = amount.subtract(balance); + voucher.setFreezeBalance(balance); + voucherLog.setAmount(balance); + } else { + voucher.setFreezeBalance(balance.subtract(amount)); + voucherLog.setAmount(amount); + } + voucherLogs.add(voucherLog); + } + voucherManager.updateAllById(vouchers); + voucherLogManager.saveAll(voucherLogs); + }); + } + + /** + * 直接支付 + */ + @Transactional(rollbackFor = Exception.class) + public List pay(BigDecimal amount, Payment payment, List vouchers) { + List voucherLogs = new ArrayList<>(); + List voucherRecords = new ArrayList<>(); + for (Voucher voucher : vouchers) { + // 待支付余额为零, 不在处理后面的储值卡 + if (BigDecimalUtil.compareTo(amount, BigDecimal.ZERO) < 1) { + break; + } + BigDecimal balance = voucher.getBalance(); + // 日志 + VoucherLog voucherLog = new VoucherLog() + .setPaymentId(payment.getId()) + .setBusinessId(payment.getBusinessId()) + .setType(VoucherCode.LOG_PAY) + .setVoucherId(voucher.getId()) + .setVoucherNo(voucher.getCardNo()); + + // 记录当前卡扣了多少钱 + BigDecimal amountRecord; // 待支付额大于储值卡余额. 全扣光 if (BigDecimalUtil.compareTo(amount, balance) == 1) { + amountRecord = voucher.getBalance(); amount = amount.subtract(balance); voucher.setBalance(BigDecimal.ZERO); voucherLog.setAmount(balance); } else { + amountRecord = balance.subtract(amount); + amount = BigDecimal.ZERO; voucher.setBalance(balance.subtract(amount)); voucherLog.setAmount(amount); } voucherLogs.add(voucherLog); + voucherRecords.add(new VoucherRecord().setCardNo(voucher.getCardNo()).setAmount(amountRecord)); } voucherManager.updateAllById(vouchers); voucherLogManager.saveAll(voucherLogs); + return voucherRecords; } @@ -141,7 +214,11 @@ public class VoucherPayService { * 取消支付, 可配置解除冻结金额 * @param freeze 是否是冻结模式, 是的话对冻结金额进行解冻, 不是的话返还扣减的金额 */ - public void close(Long paymentId,boolean freeze) { + public void close(Long paymentId, boolean freeze) { + + VoucherPayment voucherPayment = voucherPaymentManager.findByPaymentId(paymentId).orElseThrow(DataNotExistException::new); + + // 查找支付记录日志 List voucherLogs = voucherLogManager.findByPaymentIdAndType(paymentId, VoucherCode.LOG_PAY); // 查出关联的储值卡 @@ -158,7 +235,7 @@ public class VoucherPayService { .setBusinessId(voucherLog.getBusinessId()) .setVoucherId(voucher.getId()) .setVoucherNo(voucher.getCardNo()) - .setType(VoucherCode.LOG_CLOSE); + .setType(VoucherCode.LOG_CLOSE_PAY); logs.add(log); } voucherManager.updateAllById(vouchers); @@ -166,25 +243,100 @@ public class VoucherPayService { } /** - * 退款 TODO 延长卡的有效期, + * 退款 + * 全额退款支持分别退回到原有卡和统一退回到某张卡中, 默认退回到原有卡中 + * 部分退款, 会退到指定的一张卡上, 如果不指定, 则自动退到有效期最久的卡上 */ - public void refund(Long paymentId, BigDecimal amount) { + @Transactional(rollbackFor = Exception.class) + public void refund(Long paymentId, BigDecimal amount, VoucherRefundParam voucherRefundParam) { VoucherPayment voucherPayment = voucherPaymentManager.findByPaymentId(paymentId) - .orElseThrow(() -> new PayFailureException("储值卡支付记录不存在")); + .orElseThrow(() -> new PayFailureException("储值卡支付记录不存在")); + // 全部退款还是部分退款 + if (BigDecimalUtil.compareTo(amount,voucherPayment.getAmount())==0){ + // 是否全部退到一张卡中 + if (voucherRefundParam.isRefundToOne()){ + this.refundToOne(voucherPayment, voucherRefundParam.getRefundVoucherNo()); + } else { + // 退回到原有卡中 + this.refundToRaw(voucherPayment); + } + } else { + this.refundToOne(voucherPayment, voucherRefundParam.getRefundVoucherNo()); + } + } - Long voucherId = Long.valueOf(voucherPayment.getVoucherIds().split(",")[0]); - Voucher voucher = voucherManager.findById(voucherId).orElseThrow(DataNotExistException::new); - - voucher.setBalance(voucher.getBalance().add(amount)); - - VoucherLog log = new VoucherLog().setAmount(amount) - .setPaymentId(paymentId) - .setBusinessId(voucherPayment.getBusinessId()) - .setVoucherId(voucher.getId()) - .setVoucherNo(voucher.getCardNo()) - .setType(VoucherCode.LOG_REFUND); + /** + * 全部退款到一张卡中 + */ + private void refundToOne(VoucherPayment voucherPayment, String refundVoucherNo){ + // 获取储值卡扣款信息 + List voucherRecords = voucherPayment.getVoucherRecords(); + Map voucherRecordMap = voucherRecords.stream() + .collect(Collectors.toMap(VoucherRecord::getCardNo, VoucherRecord::getAmount, CollectorsFunction::retainLatest)); + List cardNoList = voucherRecords.stream() + .map(VoucherRecord::getCardNo) + .collect(Collectors.toList()); + List vouchers = voucherManager.findByCardNoList(cardNoList); + // 筛选出来要进行退款的卡 + Voucher voucher = vouchers.stream() + .filter(vr -> Objects.equals(vr.getCardNo(), refundVoucherNo)) + .findFirst() + .orElseThrow(() -> new PayFailureException("退款卡号不存在")); + // 将金额全部推到指定的卡上 + voucher.setBalance(voucher.getBalance().add(voucherPayment.getAmount())); + // 记录日志 + List voucherLogs = new ArrayList<>(); + for (Voucher v : vouchers) { + BigDecimal voucherAmount = voucherRecordMap.get(v.getCardNo()); + VoucherLog voucherLog = new VoucherLog() + .setType(VoucherCode.LOG_REFUND_SELF) + .setVoucherId(voucher.getId()) + .setVoucherNo(voucher.getCardNo()) + .setAmount(voucherAmount) + .setPaymentId(voucherPayment.getPaymentId()) + .setBusinessId(voucherPayment.getBusinessId()) + .setRemark(String.format("退款金额 %.2f, 退款到卡号: %s 储值卡中", voucherAmount, voucher.getCardNo())); + // 接收退款的卡 + if (Objects.equals(v,voucher)){ + voucherLog.setAmount(voucherPayment.getAmount()) + .setType(VoucherCode.LOG_REFUND_SELF) + .setRemark(String.format("退款金额 %.2f, 退款到卡号: %s 储值卡中", voucherAmount, voucher.getCardNo())) + } + } voucherManager.updateById(voucher); - voucherLogManager.save(log); + voucherLogManager.saveAll(voucherLogs); + } + + /** + * 退款到原有的卡中 + */ + private void refundToRaw(VoucherPayment voucherPayment){ + // 获取储值卡扣款信息 + List voucherRecords = voucherPayment.getVoucherRecords(); + List cardNoList = voucherRecords.stream() + .map(VoucherRecord::getCardNo) + .collect(Collectors.toList()); + Map voucherRecordMap = voucherRecords.stream() + .collect(Collectors.toMap(VoucherRecord::getCardNo, VoucherRecord::getAmount, CollectorsFunction::retainLatest)); + List vouchers = voucherManager.findByCardNoList(cardNoList); + + // 退款 和 记录日志 + List voucherLogs = new ArrayList<>(); + for (Voucher voucher : vouchers) { + BigDecimal voucherAmount = voucherRecordMap.get(voucher.getCardNo()); + voucher.setBalance(voucherAmount.add(voucher.getBalance())); + + voucherLogs.add(new VoucherLog() + .setType(VoucherCode.LOG_REFUND_SELF) + .setVoucherId(voucher.getId()) + .setVoucherNo(voucher.getCardNo()) + .setAmount(voucherAmount) + .setPaymentId(voucherPayment.getPaymentId()) + .setBusinessId(voucherPayment.getBusinessId()) + .setRemark(String.format("退款金额 %.2f ", voucherAmount))); + } + voucherManager.updateAllById(vouchers); + voucherLogManager.saveAll(voucherLogs); } /** @@ -193,9 +345,9 @@ public class VoucherPayService { private boolean check(List vouchers) { // 判断有效期 return vouchers.stream() - .filter(voucher -> !Objects.equals(voucher.isEnduring(), true)) - .allMatch(voucher -> LocalDateTimeUtil.between(LocalDateTime.now(), voucher.getStartTime(), - voucher.getEndTime())); + .filter(voucher -> !Objects.equals(voucher.isEnduring(), true)) + .allMatch(voucher -> LocalDateTimeUtil.between(LocalDateTime.now(), voucher.getStartTime(), + voucher.getEndTime())); } /** diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/service/VoucherPaymentService.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/service/VoucherPaymentService.java index bffe467f7..6db4bbb40 100644 --- a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/service/VoucherPaymentService.java +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/channel/voucher/service/VoucherPaymentService.java @@ -2,14 +2,13 @@ package cn.bootx.platform.daxpay.core.channel.voucher.service; import cn.bootx.platform.common.core.exception.BizException; import cn.bootx.platform.common.core.util.BigDecimalUtil; -import cn.bootx.platform.common.mybatisplus.base.MpIdEntity; import cn.bootx.platform.daxpay.code.pay.PayStatusCode; -import cn.bootx.platform.daxpay.core.payment.entity.Payment; import cn.bootx.platform.daxpay.core.channel.voucher.dao.VoucherPaymentManager; -import cn.bootx.platform.daxpay.core.channel.voucher.entity.Voucher; import cn.bootx.platform.daxpay.core.channel.voucher.entity.VoucherPayment; -import cn.bootx.platform.daxpay.param.pay.PayWayParam; +import cn.bootx.platform.daxpay.core.channel.voucher.entity.VoucherRecord; +import cn.bootx.platform.daxpay.core.payment.entity.Payment; import cn.bootx.platform.daxpay.param.pay.PayParam; +import cn.bootx.platform.daxpay.param.pay.PayWayParam; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -18,7 +17,6 @@ import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; /** * 储值卡支付记录 @@ -36,19 +34,14 @@ public class VoucherPaymentService { /** * 添加支付记录 */ - public void savePayment(Payment payment, PayParam payParam, PayWayParam payMode, List vouchers) { - String voucherIds = vouchers.stream() - .map(MpIdEntity::getId) - .map(String::valueOf) - .collect(Collectors.joining(",")); - - VoucherPayment walletPayment = new VoucherPayment().setVoucherIds(voucherIds); - walletPayment.setPaymentId(payment.getId()) + public void savePayment(Payment payment, PayParam payParam, PayWayParam payMode, List voucherRecords) { + VoucherPayment voucherPayment = new VoucherPayment().setVoucherRecords(voucherRecords); + voucherPayment.setPaymentId(payment.getId()) .setBusinessId(payParam.getBusinessId()) .setAmount(payMode.getAmount()) .setRefundableBalance(payMode.getAmount()) .setPayStatus(payment.getPayStatus()); - voucherPaymentManager.save(walletPayment); + voucherPaymentManager.save(voucherPayment); } /** diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/pay/strategy/VoucherStrategy.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/pay/strategy/VoucherStrategy.java index ece20a97d..ffed6ec2d 100644 --- a/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/pay/strategy/VoucherStrategy.java +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/core/pay/strategy/VoucherStrategy.java @@ -1,6 +1,7 @@ package cn.bootx.platform.daxpay.core.pay.strategy; import cn.bootx.platform.daxpay.code.pay.PayChannelEnum; +import cn.bootx.platform.daxpay.core.channel.voucher.entity.VoucherRecord; import cn.bootx.platform.daxpay.core.pay.func.AbsPayStrategy; import cn.bootx.platform.daxpay.core.payment.service.PaymentService; import cn.bootx.platform.daxpay.core.channel.voucher.entity.Voucher; @@ -54,12 +55,13 @@ public class VoucherStrategy extends AbsPayStrategy { */ @Override public void doPayHandler() { + List voucherRecords; if (this.getPayment().isAsyncPayMode()){ - voucherPayService.freezeBalance(this.getPayWayParam().getAmount(), this.getPayment(), this.vouchers); + voucherRecords = voucherPayService.freezeBalance(this.getPayWayParam().getAmount(), this.getPayment(), this.vouchers); } else { - voucherPayService.pay(this.getPayWayParam().getAmount(), this.getPayment(), this.vouchers); + voucherRecords = voucherPayService.pay(this.getPayWayParam().getAmount(), this.getPayment(), this.vouchers); } - voucherPaymentService.savePayment(this.getPayment(), getPayParam(), getPayWayParam(), vouchers); + voucherPaymentService.savePayment(this.getPayment(), getPayParam(), getPayWayParam(), voucherRecords); } /** @@ -87,7 +89,7 @@ public class VoucherStrategy extends AbsPayStrategy { */ @Override public void doRefundHandler() { - voucherPayService.refund(this.getPayment().getId(), this.getPayWayParam().getAmount()); + voucherPayService.refund(this.getPayment().getId(), this.getPayWayParam().getAmount(),null); voucherPaymentService.updateRefund(this.getPayment().getId(), this.getPayWayParam().getAmount()); paymentService.updateRefundSuccess(this.getPayment(), this.getPayWayParam().getAmount(), PayChannelEnum.VOUCHER); diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/channel/voucher/VoucherRefundParam.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/channel/voucher/VoucherRefundParam.java new file mode 100644 index 000000000..3ef835c58 --- /dev/null +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/channel/voucher/VoucherRefundParam.java @@ -0,0 +1,23 @@ +package cn.bootx.platform.daxpay.param.channel.voucher; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 储值卡退款参数 + * @author xxm + * @since 2023/6/29 + */ +@Data +@Accessors(chain = true) +@Schema(title = "储值卡退款参数") +public class VoucherRefundParam { + + @Schema(description = "是否统一退款到指定卡中") + private boolean refundToOne; + + @Schema(description = "统一退款到的卡号") + private String refundVoucherNo; + +} diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/channel/wallet/WalletPayParam.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/channel/wallet/WalletPayParam.java index 277ddc839..8b86502ac 100644 --- a/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/channel/wallet/WalletPayParam.java +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/channel/wallet/WalletPayParam.java @@ -25,5 +25,4 @@ public class WalletPayParam implements Serializable { @Schema(description = "用户ID") private Long userId; - } diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/channel/wallet/WalletRefundParam.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/channel/wallet/WalletRefundParam.java new file mode 100644 index 000000000..02ab736a2 --- /dev/null +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/channel/wallet/WalletRefundParam.java @@ -0,0 +1,16 @@ +package cn.bootx.platform.daxpay.param.channel.wallet; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 钱包退款参数 + * @author xxm + * @since 2023/6/29 + */ +@Data +@Accessors(chain = true) +@Schema(title = "钱包退款参数") +public class WalletRefundParam { +} diff --git a/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/refund/RefundModeParam.java b/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/refund/RefundModeParam.java index 9db85e075..d5c488eb5 100644 --- a/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/refund/RefundModeParam.java +++ b/dax-pay/src/main/java/cn/bootx/platform/daxpay/param/refund/RefundModeParam.java @@ -2,6 +2,8 @@ package cn.bootx.platform.daxpay.param.refund; import cn.bootx.platform.daxpay.code.pay.PayChannelEnum; import cn.bootx.platform.daxpay.dto.payment.RefundableInfo; +import cn.bootx.platform.daxpay.param.channel.voucher.VoucherRefundParam; +import cn.bootx.platform.daxpay.param.channel.wallet.WalletRefundParam; import cn.bootx.platform.daxpay.param.pay.PayWayParam; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; @@ -29,6 +31,13 @@ public class RefundModeParam { @Schema(description = "支付金额") private BigDecimal amount; + /** + * @see VoucherRefundParam + * @see WalletRefundParam + */ + @Schema(description = "扩展参数的json字符串") + private String extraParamsJson; + /** * 转换成支付方式参数 */