diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/voucher/service/VoucherRecordService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/voucher/service/VoucherRecordService.java index 3b5ca1a4..af382f9f 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/voucher/service/VoucherRecordService.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/voucher/service/VoucherRecordService.java @@ -22,6 +22,7 @@ import org.springframework.stereotype.Service; public class VoucherRecordService { private final VoucherRecordManager manager; + /** * 分页查询 */ diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/convert/WalletConvert.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/convert/WalletConvert.java index 4e364931..7213a3f4 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/convert/WalletConvert.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/convert/WalletConvert.java @@ -2,11 +2,9 @@ package cn.bootx.platform.daxpay.service.core.channel.wallet.convert; import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.Wallet; import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.WalletConfig; -import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.WalletLog; import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.WalletRecord; import cn.bootx.platform.daxpay.service.dto.channel.wallet.WalletConfigDto; import cn.bootx.platform.daxpay.service.dto.channel.wallet.WalletDto; -import cn.bootx.platform.daxpay.service.dto.channel.wallet.WalletLogDto; import cn.bootx.platform.daxpay.service.dto.channel.wallet.WalletRecordDto; import cn.bootx.platform.daxpay.service.param.channel.wechat.WalletConfigParam; import org.mapstruct.Mapper; @@ -25,8 +23,6 @@ public interface WalletConvert { WalletDto convert(Wallet in); - WalletLogDto convert(WalletLog in); - WalletConfigDto convert(WalletConfig in); WalletConfig convert(WalletConfigParam in); diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/entity/WalletLog.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/entity/WalletLog.java deleted file mode 100644 index 25992418..00000000 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/entity/WalletLog.java +++ /dev/null @@ -1,70 +0,0 @@ -package cn.bootx.platform.daxpay.service.core.channel.wallet.entity; - - -import cn.bootx.platform.common.core.function.EntityBaseFunction; -import cn.bootx.platform.common.mybatisplus.base.MpBaseEntity; -import cn.bootx.platform.daxpay.service.core.channel.wallet.convert.WalletConvert; -import cn.bootx.platform.daxpay.service.dto.channel.wallet.WalletLogDto; -import cn.bootx.table.modify.annotation.DbColumn; -import cn.bootx.table.modify.annotation.DbTable; -import cn.bootx.table.modify.mysql.annotation.DbMySqlIndex; -import com.baomidou.mybatisplus.annotation.TableName; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.experimental.Accessors; - -/** - * 钱包日志 - * - * @author xxm - * @since 2020/12/8 - */ -@EqualsAndHashCode(callSuper = true) -@Data -@DbTable(comment = "钱包日志") -@Accessors(chain = true) -@TableName("pay_wallet_log") -public class WalletLog extends MpBaseEntity implements EntityBaseFunction { - - /** 钱包id */ - @DbMySqlIndex(comment = "钱包索引ID") - @DbColumn(comment = "钱包id") - private Long walletId; - - /** 用户id */ - @DbColumn(comment = "用户id") - private Long userId; - - /** 类型 */ - @DbColumn(comment = "类型") - private String type; - - /** 交易记录ID */ - @DbColumn(comment = "交易记录ID") - private Long paymentId; - - /** 业务ID */ - @DbColumn(comment = "业务ID") - private String businessId; - - /** - * 操作类型 - */ - @DbColumn(comment = "操作类型") - private String operationSource; - - /** 金额 */ - @DbColumn(comment = "金额") - private Integer amount; - - /** 备注 */ - @DbColumn(comment = "备注") - private String remark; - - - @Override - public WalletLogDto toDto() { - return WalletConvert.CONVERT.convert(this); - } - -} diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/service/WalletConfigService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/service/WalletConfigService.java index 029a3dc5..b54b34c7 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/service/WalletConfigService.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/service/WalletConfigService.java @@ -39,6 +39,17 @@ public class WalletConfigService { return walletConfigManager.findById(ID).orElseThrow(() -> new DataNotExistException("钱包配置不存在")); } + /** + * 获取并校验配置 + */ + public WalletConfig getAndCheckConfig(){ + WalletConfig config = this.getConfig(); + if (!config.getEnable()){ + throw new DataNotExistException("钱包配置未启用"); + } + return config; + } + /** * 更新 */ diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/service/WalletPayService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/service/WalletPayService.java index 35026a2c..8701e2d5 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/service/WalletPayService.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/service/WalletPayService.java @@ -39,20 +39,7 @@ public class WalletPayService { /** * 关闭支付, 将支付成功的金额进行返还 */ - public void close(PayChannelOrder channelOrder) { - // 从通道扩展参数中取出钱包参数 - String channelExtra = channelOrder.getChannelExtra(); - WalletPayParam walletPayParam = JSONUtil.toBean(channelExtra, WalletPayParam.class); - - // 获取钱包 - Wallet wallet = null; - if (Objects.nonNull(walletPayParam.getWalletId())){ - wallet = walletManager.findById(walletPayParam.getWalletId()).orElseThrow(DataNotExistException::new); - } - if (Objects.nonNull(walletPayParam.getUserId())){ - wallet = walletManager.findByUser(walletPayParam.getUserId()).orElseThrow(DataNotExistException::new); - } - + public void close(PayChannelOrder channelOrder, Wallet wallet) { // 将订单的金额退款到钱包 wallet.setBalance(wallet.getBalance() + channelOrder.getAmount()); walletManager.updateById(wallet); diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/service/WalletRecordService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/service/WalletRecordService.java index 13a57c1f..02c84a47 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/service/WalletRecordService.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wallet/service/WalletRecordService.java @@ -5,7 +5,10 @@ import cn.bootx.platform.common.core.rest.PageResult; import cn.bootx.platform.common.core.rest.param.PageParam; import cn.bootx.platform.common.mybatisplus.util.MpUtil; import cn.bootx.platform.daxpay.service.core.channel.wallet.dao.WalletRecordManager; +import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.Wallet; import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.WalletRecord; +import cn.bootx.platform.daxpay.service.core.order.pay.entity.PayChannelOrder; +import cn.bootx.platform.daxpay.service.core.order.refund.entity.PayRefundChannelOrder; import cn.bootx.platform.daxpay.service.dto.channel.wallet.WalletRecordDto; import cn.bootx.platform.daxpay.service.param.channel.wallet.WalletRecordQuery; import lombok.RequiredArgsConstructor; @@ -22,6 +25,71 @@ import org.springframework.stereotype.Service; @RequiredArgsConstructor public class WalletRecordService { private final WalletRecordManager walletRecordManager; + private final WalletQueryService walletQueryService; + + /** + * 创建保存 + */ + public void create(Wallet wallet){ + WalletRecord walletRecord = new WalletRecord() + .setAmount(wallet.getBalance()) + .setNewAmount(wallet.getBalance()) + .setOldAmount(0) + .setWalletId(wallet.getId()); + walletRecordManager.save(walletRecord); + } + + /** + * 支付保存 + */ + public void pay(PayChannelOrder channelOrder, Wallet wallet){ + WalletRecord walletRecord = new WalletRecord() + .setAmount(channelOrder.getAmount()) + .setNewAmount(wallet.getBalance()) + .setOldAmount(wallet.getBalance() - channelOrder.getAmount()) + .setOrderId(String.valueOf(channelOrder.getPaymentId())) + .setWalletId(wallet.getId()); + walletRecordManager.save(walletRecord); + } + + /** + * 退款保存 + */ + public void refund(PayRefundChannelOrder channelOrder, Wallet wallet){ + WalletRecord walletRecord = new WalletRecord() + .setAmount(channelOrder.getAmount()) + .setNewAmount(wallet.getBalance()) + .setOldAmount(wallet.getBalance() + channelOrder.getAmount()) + .setOrderId(String.valueOf(channelOrder.getRefundId())) + .setWalletId(wallet.getId()); + walletRecordManager.save(walletRecord); + } + + /** + * 支付关闭 + */ + public void payClose(PayChannelOrder channelOrder, Wallet wallet){ + WalletRecord walletRecord = new WalletRecord() + .setAmount(channelOrder.getAmount()) + .setNewAmount(wallet.getBalance()) + .setOldAmount(wallet.getBalance() - channelOrder.getAmount()) + .setOrderId(String.valueOf(channelOrder.getPaymentId())) + .setWalletId(wallet.getId()); + walletRecordManager.save(walletRecord); + } + + /** + * 退款关闭 + */ + public void refundClose(PayRefundChannelOrder channelOrder, Wallet wallet){ + WalletRecord walletRecord = new WalletRecord() + .setAmount(channelOrder.getAmount()) + .setNewAmount(wallet.getBalance()) + .setOldAmount(wallet.getBalance() - channelOrder.getAmount()) + .setOrderId(String.valueOf(channelOrder.getRefundId())) + .setWalletId(wallet.getId()); + walletRecordManager.save(walletRecord); + } /** * 分页 diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/close/strategy/WalletPayCloseStrategy.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/close/strategy/WalletPayCloseStrategy.java index 492ecbc9..b6d4fd24 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/close/strategy/WalletPayCloseStrategy.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/close/strategy/WalletPayCloseStrategy.java @@ -1,8 +1,13 @@ package cn.bootx.platform.daxpay.service.core.payment.close.strategy; import cn.bootx.platform.daxpay.code.PayChannelEnum; +import cn.bootx.platform.daxpay.param.channel.WalletPayParam; +import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.Wallet; import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletPayService; +import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletQueryService; +import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletRecordService; import cn.bootx.platform.daxpay.service.func.AbsPayCloseStrategy; +import cn.hutool.json.JSONUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Scope; @@ -21,17 +26,34 @@ import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROT @RequiredArgsConstructor public class WalletPayCloseStrategy extends AbsPayCloseStrategy { private final WalletPayService walletPayService; + private final WalletQueryService walletQueryService; + private final WalletRecordService walletRecordService; + + private Wallet wallet; + @Override public PayChannelEnum getChannel() { return PayChannelEnum.WALLET; } + /** + * 关闭前的处理方式 + */ + @Override + public void doBeforeCloseHandler() { + // 从通道扩展参数中取出钱包参数 + String channelExtra = this.getChannelOrder().getChannelExtra(); + WalletPayParam walletPayParam = JSONUtil.toBean(channelExtra, WalletPayParam.class); + this.wallet = walletQueryService.getWallet(walletPayParam); + } + /** * 关闭操作 */ @Override public void doCloseHandler() { - walletPayService.close(this.getChannelOrder()); + walletPayService.close(this.getChannelOrder(),this.wallet); + walletRecordService.payClose(this.getChannelOrder(),this.wallet); } } diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/pay/strategy/WalletPayStrategy.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/pay/strategy/WalletPayStrategy.java index 6f969939..247e3bfc 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/pay/strategy/WalletPayStrategy.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/pay/strategy/WalletPayStrategy.java @@ -11,6 +11,7 @@ import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.WalletConfig; import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletConfigService; import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletPayService; import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletQueryService; +import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletRecordService; import cn.bootx.platform.daxpay.service.func.AbsPayStrategy; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; @@ -41,6 +42,8 @@ public class WalletPayStrategy extends AbsPayStrategy { private final WalletQueryService walletQueryService; + private final WalletRecordService walletRecordService; + private Wallet wallet; private WalletConfig walletConfig; @@ -66,7 +69,7 @@ public class WalletPayStrategy extends AbsPayStrategy { throw new PayFailureException("支付参数错误"); } - this.walletConfig = walletConfigService.getConfig(); + this.walletConfig = walletConfigService.getAndCheckConfig(); // 获取钱包 this.wallet = walletQueryService.getWallet(walletPayParam); @@ -78,6 +81,9 @@ public class WalletPayStrategy extends AbsPayStrategy { throw new WalletBannedException(); } // 判断是否超过限额 + if (getPayChannelParam().getAmount() > this.walletConfig.getSingleLimit()){ + throw new PayFailureException("钱包单次支付金额超过限额"); + } // 判断余额 if (this.wallet.getBalance() < getPayChannelParam().getAmount()) { @@ -90,7 +96,7 @@ public class WalletPayStrategy extends AbsPayStrategy { */ @Override public void doPayHandler() { - // 异步支付方式时使用冻结方式 walletPayService.pay(getPayChannelParam().getAmount(), this.wallet); + walletRecordService.pay(this.getChannelOrder(), this.wallet); } } diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/dto/channel/voucher/VoucherLogDto.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/dto/channel/voucher/VoucherLogDto.java deleted file mode 100644 index f36464a5..00000000 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/dto/channel/voucher/VoucherLogDto.java +++ /dev/null @@ -1,61 +0,0 @@ -package cn.bootx.platform.daxpay.service.dto.channel.voucher; - -import cn.bootx.platform.common.core.rest.dto.BaseDto; -import cn.bootx.platform.daxpay.service.code.VoucherCode; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.experimental.Accessors; - -import java.math.BigDecimal; - -/** - * 储值卡日志 - * - * @author xxm - * @since 2022/3/17 - */ -@Schema(title = "储值卡日志") -@EqualsAndHashCode(callSuper = true) -@Data -@Accessors(chain = true) -public class VoucherLogDto extends BaseDto { - - /** 储值卡id */ - @Schema(description = "储值卡id") - private Long voucherId; - - /** 储值卡号 */ - @Schema(description = "储值卡号") - private String voucherNo; - - @Schema(description = "商户编码") - private String mchCode; - - @Schema(description = "商户应用编码") - private String mchAppCode; - - /** 金额 */ - @Schema(description = "金额") - private BigDecimal amount; - - /** - * 类型 - * @see VoucherCode#LOG_PAY - */ - @Schema(description = "类型") - private String type; - - /** 交易记录ID */ - @Schema(description = "交易记录ID") - private Long paymentId; - - /** 业务ID */ - @Schema(description = "业务ID") - private String businessId; - - /** 备注 */ - @Schema(description = "备注") - private String remark; - -} diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/dto/channel/wallet/WalletLogDto.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/dto/channel/wallet/WalletLogDto.java deleted file mode 100644 index d1c4c7a0..00000000 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/dto/channel/wallet/WalletLogDto.java +++ /dev/null @@ -1,59 +0,0 @@ -package cn.bootx.platform.daxpay.service.dto.channel.wallet; - -import cn.bootx.platform.common.core.rest.dto.BaseDto; -import cn.bootx.platform.daxpay.service.code.WalletCode; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.experimental.Accessors; - -import java.io.Serializable; -import java.math.BigDecimal; -import java.time.LocalDateTime; - -/** - * @author xxm - * @since 2020/12/8 - */ -@EqualsAndHashCode(callSuper = true) -@Data -@Accessors(chain = true) -@Schema(title = "钱包日志") -public class WalletLogDto extends BaseDto implements Serializable { - - private static final long serialVersionUID = -2553004953931903738L; - - @Schema(description = "钱包ID") - private Long walletId; - - @Schema(description = "用户ID") - private Long userId; - - /** - * @see WalletCode - */ - @Schema(description = "类型") - private String type; - - @Schema(description = "交易记录ID") - private Long paymentId; - - @Schema(description = "备注") - private String remark; - - @Schema(description = "业务ID") - private String businessId; - - /** - * @see WalletCode#OPERATION_SOURCE_SYSTEM - */ - @Schema(description = " 1 系统操作 2管理员操作 3用户操作") - private String operationSource; - - @Schema(description = "金额") - private BigDecimal amount; - - @Schema(description = "创建时间") - private LocalDateTime createTime; - -}