feat 钱包管理调试

This commit is contained in:
xxm1995
2023-06-25 17:27:46 +08:00
parent beadbb685f
commit 16bcbc0d52
225 changed files with 560 additions and 398 deletions

View File

@@ -18,7 +18,7 @@ import java.net.UnknownHostException;
* DaxPay 支付开发平台
*
* @author xxm
* @date 2023/4/20
* @since 2023/4/20
*/
@Slf4j
@ConfigurationPropertiesScan

View File

@@ -4,7 +4,7 @@ package cn.bootx.platform.daxpay.code;
* 支付服务缓存
*
* @author xxm
* @date 2022/7/11
* @since 2022/7/11
*/
public interface CachingCode {

View File

@@ -4,7 +4,7 @@ package cn.bootx.platform.daxpay.code;
* 商户和应用相关编码
*
* @author xxm
* @date 2023/6/12
* @since 2023/6/12
*/
public interface MchAndAppCode {

View File

@@ -4,7 +4,7 @@ package cn.bootx.platform.daxpay.code;
* 错误码
*
* @author xxm
* @date 2020/12/7
* @since 2020/12/7
*/
public interface PaymentErrorCode {

View File

@@ -12,7 +12,7 @@ import java.util.Objects;
* 支付通道枚举
*
* @author xxm
* @date 2021/7/26
* @since 2021/7/26
*/
@Getter
@RequiredArgsConstructor

View File

@@ -4,7 +4,7 @@ package cn.bootx.platform.daxpay.code.pay;
* 支付状态
*
* @author xxm
* @date 2021/3/1
* @since 2021/3/1
*/
public interface PayStatusCode {

View File

@@ -4,7 +4,7 @@ package cn.bootx.platform.daxpay.code.pay;
* 支付网关同步状态
*
* @author xxm
* @date 2021/4/21
* @since 2021/4/21
*/
public interface PaySyncStatus {

View File

@@ -11,7 +11,7 @@ import java.util.Objects;
* 支付方式
*
* @author xxm
* @date 2021/7/26
* @since 2021/7/26
*/
@Getter
@AllArgsConstructor

View File

@@ -4,7 +4,7 @@ package cn.bootx.platform.daxpay.code.pay;
* 支付方式扩展字段
*
* @author xxm
* @date 2022/2/27
* @since 2022/2/27
*/
public interface PayWayExtraCode {
@@ -17,6 +17,9 @@ public interface PayWayExtraCode {
/** 单张储值卡 */
String VOUCHER_NO = "voucher_no";
/** 钱包ID */
String WALLET_ID = "wallet_id";
/** 同步通知路径 支付完成跳转的页面地址 */
String RETURN_URL = "return_url";

View File

@@ -4,7 +4,7 @@ package cn.bootx.platform.daxpay.code.paymodel;
* 支付宝支付参数
*
* @author xxm
* @date 2021/2/27
* @since 2021/2/27
*/
public interface AliPayCode {

View File

@@ -12,7 +12,7 @@ import java.util.Objects;
* 支付宝支付方式
*
* @author xxm
* @date 2021/7/2
* @since 2021/7/2
*/
@UtilityClass
public class AliPayWay {

View File

@@ -4,7 +4,7 @@ package cn.bootx.platform.daxpay.code.paymodel;
* 储值卡常量
*
* @author xxm
* @date 2022/3/14
* @since 2022/3/14
*/
public interface VoucherCode {

View File

@@ -4,66 +4,75 @@ package cn.bootx.platform.daxpay.code.paymodel;
* 钱包涉及到的常量
*
* @author xxm
* @date 2020/12/8
* @since 2020/12/8
*/
public interface WalletCode {
/* 操作类型 */
/** 系统操作 */
String OPERATION_SOURCE_SYSTEM = "1";
String OPERATION_SOURCE_SYSTEM = "system";
/** 管理员操作 */
String OPERATION_SOURCE_ADMIN = "";
String OPERATION_SOURCE_ADMIN = "admin";
/** 用户操作 */
String OPERATION_SOURCE_USER = "3";
String OPERATION_SOURCE_USER = "user";
/* 钱包状态 */
/** 钱包状态-正常 */
String STATUS_NORMAL = "";
String STATUS_NORMAL = "normal";
/** 钱包状态-禁用 */
String STATUS_FORBIDDEN = "";
String STATUS_FORBIDDEN = "forbidden";
/* 日志类型 */
/**
* 钱包日志-开通
*/
String LOG_ACTIVE = "";
String LOG_ACTIVE = "active";
/**
* 钱包日志-主动充值
* 钱包日志-Admin操作余额变动
*/
String LOG_RECHARGE = "";
String LOG_ADMIN_CHANGER = "adminChanger";
/**
* 钱包日志-自动充值
* 钱包日志-预冻结额度
*/
String LOG_AUTO_RECHARGE = "";
String LOG_FREEZE_BALANCE = "freeze";
/**
* 钱包日志-Admin余额变动
* 钱包日志-解冻额度
*/
String LOG_ADMIN_CHANGER = "";
String LOG_UNFREEZE_BALANCE = "unfreeze";
/**
* 钱包日志-支付
* 钱包日志-解冻并扣减余额
*/
String LOG_PAY = "";
String LOG_REDUCE_AND_UNFREEZE_BALANCE = "reduceAndUnfreeze";
/**
* 钱包日志-系统扣除余额的日志
* 钱包日志-直接支付
*/
String LOG_SYSTEM_REDUCE_BALANCE = "";
String LOG_PAY = "pay";
/**
* 钱包日志-取消支付
*/
String LOG_CLOSE_PAY = "closePay";
/**
* 钱包日志-退款
*/
String LOG_REFUND = "";
String LOG_REFUND = "refund";
/**
* 钱包日志-取消支付返还
* 锁定钱包
*/
String LOG_PAY_CLOSE = "";
String LOG_LOCK = "lock";
/**
* 解锁钱包
*/
String LOG_UNLOCK = "unlock";
}

View File

@@ -4,7 +4,7 @@ package cn.bootx.platform.daxpay.code.paymodel;
* 微信参数
*
* @author xxm
* @date 2021/6/21
* @since 2021/6/21
*/
public interface WeChatPayCode {

View File

@@ -12,7 +12,7 @@ import java.util.Objects;
* 微信支付方式
*
* @author xxm
* @date 2021/7/2
* @since 2021/7/2
*/
@UtilityClass
public class WeChatPayWay {

View File

@@ -12,7 +12,7 @@ import org.springframework.context.annotation.Configuration;
* 消息队列配置
*
* @author xxm
* @date 2021/6/25
* @since 2021/6/25
*/
@Configuration
public class PaymentRabbitMqConfiguration {

View File

@@ -17,7 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
* 聚合支付
*
* @author xxm
* @date 2022/3/6
* @since 2022/3/6
*/
@IgnoreAuth
@Tag(name = "聚合支付")

View File

@@ -18,7 +18,7 @@ import java.util.List;
/**
* @author xxm
* @date 2021/2/26
* @since 2021/2/26
*/
@Tag(name = "支付宝配置")
@RestController

View File

@@ -20,7 +20,7 @@ import static org.springframework.http.HttpHeaders.USER_AGENT;
/**
* @author xxm
* @date 2022/2/23
* @since 2022/2/23
*/
@IgnoreAuth
@Tag(name = "结算台")

View File

@@ -20,7 +20,7 @@ import java.util.List;
* 商户应用
*
* @author xxm
* @date 2023-05-23
* @since 2023-05-23
*/
@Tag(name = "商户应用")
@RestController

View File

@@ -19,7 +19,7 @@ import java.util.List;
* 商户
*
* @author xxm
* @date 2023-05-17
* @since 2023-05-17
*/
@Tag(name = "商户")
@RestController

View File

@@ -21,7 +21,7 @@ import java.util.Map;
/**
* @author xxm
* @date 2021/2/27
* @since 2021/2/27
*/
@IgnoreAuth
@Slf4j

View File

@@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.*;
* 支付通道配置
*
* @author xxm
* @date 2023-05-24
* @since 2023-05-24
*/
@Tag(name = "支付通道配置")
@RestController

View File

@@ -20,7 +20,7 @@ import org.springframework.web.bind.annotation.RestController;
/**
* @author xxm
* @date 2020/12/9
* @since 2020/12/9
*/
@Tag(name = "统一支付")
@RestController

View File

@@ -17,7 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
* 回调记录
*
* @author xxm
* @date 2021/7/22
* @since 2021/7/22
*/
@Tag(name = "支付回调记录")
@RestController

View File

@@ -20,7 +20,7 @@ import java.util.List;
/**
* @author xxm
* @date 2021/6/28
* @since 2021/6/28
*/
@Tag(name = "支付记录")
@RestController

View File

@@ -17,7 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
* 退款记录
*
* @author xxm
* @date 2022/3/3
* @since 2022/3/3
*/
@Tag(name = "退款记录")
@RestController

View File

@@ -18,7 +18,7 @@ import java.util.List;
/**
* @author xxm
* @date 2022/3/14
* @since 2022/3/14
*/
@Tag(name = "储值卡")
@RestController

View File

@@ -24,11 +24,11 @@ import java.util.List;
* 钱包
*
* @author xxm
* @date 2021/2/24
* @since 2021/2/24
*/
@Tag(name = "钱包相关的接口")
@RestController
@RequestMapping("wallet")
@RequestMapping("/wallet")
@AllArgsConstructor
public class WalletController {

View File

@@ -20,7 +20,7 @@ import org.springframework.web.bind.annotation.RestController;
* 钱包日志相关接口
*
* @author xxm
* @date 2020/12/8
* @since 2020/12/8
*/
@Tag(name = "钱包日志相关的接口")
@RestController

View File

@@ -17,7 +17,7 @@ import java.util.List;
/**
* @author xxm
* @date 2021/3/19
* @since 2021/3/19
*/
@Tag(name = "微信支付配置")
@RestController

View File

@@ -9,7 +9,7 @@ import java.math.BigDecimal;
* 聚合支付发起信息
*
* @author xxm
* @date 2022/3/5
* @since 2022/3/5
*/
@Data
@Accessors(chain = true)

View File

@@ -18,7 +18,7 @@ import java.util.Optional;
* 聚合支付
*
* @author xxm
* @date 2022/3/6
* @since 2022/3/6
*/
@Slf4j
@Service

View File

@@ -43,7 +43,7 @@ import java.util.*;
* 结算台
*
* @author xxm
* @date 2022/2/23
* @since 2022/2/23
*/
@Slf4j
@Service
@@ -79,6 +79,7 @@ public class CashierService {
map.put(PayWayExtraCode.AUTH_CODE, param.getAuthCode());
map.put(PayWayExtraCode.OPEN_ID, param.getOpenId());
map.put(PayWayExtraCode.VOUCHER_NO, param.getVoucherNo());
map.put(PayWayExtraCode.WALLET_ID, param.getWalletId());
String extraParamsJson = PayWaylUtil.buildExtraParamsJson(param.getPayChannel(), map);
payWayParam.setExtraParamsJson(extraParamsJson);

View File

@@ -10,7 +10,7 @@ import org.mapstruct.factory.Mappers;
* 支付宝转换
*
* @author xxm
* @date 2021/7/5
* @since 2021/7/5
*/
@Mapper
public interface AlipayConvert {

View File

@@ -12,7 +12,7 @@ import java.util.Optional;
* 支付宝
*
* @author xxm
* @date 2021/2/26
* @since 2021/2/26
*/
@Repository
@RequiredArgsConstructor

View File

@@ -8,7 +8,7 @@ import org.apache.ibatis.annotations.Mapper;
* 支付宝支付
*
* @author xxm
* @date 2021/2/26
* @since 2021/2/26
*/
@Mapper
public interface AliPaymentMapper extends BaseMapper<AliPayment> {

View File

@@ -16,7 +16,7 @@ import java.util.Optional;
* 支付宝配置
*
* @author xxm
* @date 2021/2/26
* @since 2021/2/26
*/
@Repository
@RequiredArgsConstructor

View File

@@ -13,7 +13,7 @@ import lombok.experimental.Accessors;
* 支付宝支付记录
*
* @author xxm
* @date 2021/2/26
* @since 2021/2/26
*/
@EqualsAndHashCode(callSuper = true)
@Data

View File

@@ -26,7 +26,7 @@ import lombok.experimental.Accessors;
* 支付宝支付配置
*
* @author xxm
* @date 2020/12/15
* @since 2020/12/15
*/
@EqualsAndHashCode(callSuper = true)
@Data

View File

@@ -28,7 +28,7 @@ import java.util.Objects;
* 支付宝回调处理
*
* @author xxm
* @date 2021/2/28
* @since 2021/2/28
*/
@Slf4j
@Service

View File

@@ -24,7 +24,7 @@ import java.util.Objects;
* 支付宝支付取消和退款
*
* @author xxm
* @date 2021/4/20
* @since 2021/4/20
*/
@Slf4j
@Service

View File

@@ -39,7 +39,7 @@ import static cn.bootx.platform.daxpay.code.paymodel.AliPayCode.QUICK_MSECURITY_
* 支付宝支付service
*
* @author xxm
* @date 2021/2/26
* @since 2021/2/26
*/
@Slf4j
@Service

View File

@@ -27,7 +27,7 @@ import java.util.Optional;
* 支付宝支付记录
*
* @author xxm
* @date 2021/2/26
* @since 2021/2/26
*/
@Slf4j
@Service

View File

@@ -32,7 +32,7 @@ import java.util.stream.Collectors;
* 支付宝支付
*
* @author xxm
* @date 2020/12/15
* @since 2020/12/15
*/
@Slf4j
@Service

View File

@@ -19,7 +19,7 @@ import java.util.Objects;
* 支付宝同步
*
* @author xxm
* @date 2021/5/17
* @since 2021/5/17
*/
@Slf4j
@Service

View File

@@ -13,7 +13,7 @@ import java.time.LocalDateTime;
* 基础支付记录类
*
* @author xxm
* @date 2021/2/25
* @since 2021/2/25
*/
@EqualsAndHashCode(callSuper = true)
@Data

View File

@@ -12,7 +12,7 @@ import java.util.Optional;
* 现金支付
*
* @author xxm
* @date 2021/6/23
* @since 2021/6/23
*/
@Slf4j
@Repository

View File

@@ -8,7 +8,7 @@ import org.apache.ibatis.annotations.Mapper;
* 现金支付
*
* @author xxm
* @date 2021/6/23
* @since 2021/6/23
*/
@Mapper
public interface CashPaymentMapper extends BaseMapper<CashPayment> {

View File

@@ -10,7 +10,7 @@ import lombok.experimental.Accessors;
* 现金支付记录
*
* @author xxm
* @date 2021/6/23
* @since 2021/6/23
*/
@Data
@EqualsAndHashCode(callSuper = true)

View File

@@ -18,7 +18,7 @@ import java.util.Optional;
* 现金支付
*
* @author xxm
* @date 2021/6/23
* @since 2021/6/23
*/
@Slf4j
@Service

View File

@@ -10,7 +10,7 @@ import org.mapstruct.factory.Mappers;
* 支付通道配置
*
* @author xxm
* @date 2023-05-24
* @since 2023-05-24
*/
@Mapper
public interface PayChannelConfigConvert {
@@ -21,4 +21,4 @@ public interface PayChannelConfigConvert {
PayChannelConfigDto convert(PayChannelConfig in);
}
}

View File

@@ -18,7 +18,7 @@ import java.util.List;
* 支付通道配置
*
* @author xxm
* @date 2023-05-24
* @since 2023-05-24
*/
@Repository
@RequiredArgsConstructor

View File

@@ -8,9 +8,9 @@ import org.apache.ibatis.annotations.Mapper;
* 支付通道配置
*
* @author xxm
* @date 2023-05-24
* @since 2023-05-24
*/
@Mapper
public interface PayChannelConfigMapper extends BaseMapper<PayChannelConfig> {
}
}

View File

@@ -16,7 +16,7 @@ import lombok.experimental.Accessors;
* 支付通道配置
*
* @author xxm
* @date 2023-05-24
* @since 2023-05-24
*/
@EqualsAndHashCode(callSuper = true)
@Data

View File

@@ -19,7 +19,7 @@ import org.springframework.stereotype.Service;
* 支付通道配置
*
* @author xxm
* @date 2023-05-24
* @since 2023-05-24
*/
@Slf4j
@Service

View File

@@ -7,7 +7,7 @@ import org.mapstruct.factory.Mappers;
/**
* @author xxm
* @date 2022/3/11
* @since 2022/3/11
*/
@Mapper
public interface UnionPayConvert {

View File

@@ -8,7 +8,7 @@ import org.springframework.stereotype.Repository;
/**
* @author xxm
* @date 2022/3/11
* @since 2022/3/11
*/
@Slf4j
@Repository

View File

@@ -6,7 +6,7 @@ import org.apache.ibatis.annotations.Mapper;
/**
* @author xxm
* @date 2022/3/11
* @since 2022/3/11
*/
@Mapper
public interface UnionPayConfigMapper extends BaseMapper<UnionPayConfig> {

View File

@@ -8,7 +8,7 @@ import org.springframework.stereotype.Repository;
/**
* @author xxm
* @date 2022/3/11
* @since 2022/3/11
*/
@Slf4j
@Repository

View File

@@ -6,7 +6,7 @@ import org.apache.ibatis.annotations.Mapper;
/**
* @author xxm
* @date 2022/3/11
* @since 2022/3/11
*/
@Mapper
public interface UnionPaymentMapper extends BaseMapper<UnionPayment> {

View File

@@ -13,7 +13,7 @@ import lombok.experimental.Accessors;
* 云闪付
*
* @author xxm
* @date 2022/3/11
* @since 2022/3/11
*/
@EqualsAndHashCode(callSuper = true)
@Data

View File

@@ -8,7 +8,7 @@ import lombok.experimental.Accessors;
/**
* @author xxm
* @date 2022/3/11
* @since 2022/3/11
*/
@EqualsAndHashCode(callSuper = true)
@Data

View File

@@ -7,7 +7,7 @@ import org.springframework.stereotype.Service;
/**
* @author xxm
* @date 2022/3/11
* @since 2022/3/11
*/
@Slf4j
@Service

View File

@@ -6,7 +6,7 @@ import org.springframework.stereotype.Service;
/**
* @author xxm
* @date 2022/3/11
* @since 2022/3/11
*/
@Slf4j
@Service

View File

@@ -6,7 +6,7 @@ import org.springframework.stereotype.Service;
/**
* @author xxm
* @date 2022/3/11
* @since 2022/3/11
*/
@Slf4j
@Service

View File

@@ -9,7 +9,7 @@ import org.mapstruct.factory.Mappers;
/**
* @author xxm
* @date 2022/3/14
* @since 2022/3/14
*/
@Mapper
public interface VoucherConvert {

View File

@@ -10,7 +10,7 @@ import java.util.List;
/**
* @author xxm
* @date 2022/3/19
* @since 2022/3/19
*/
@Slf4j
@Repository

View File

@@ -6,7 +6,7 @@ import org.apache.ibatis.annotations.Mapper;
/**
* @author xxm
* @date 2022/3/19
* @since 2022/3/19
*/
@Mapper
public interface VoucherLogMapper extends BaseMapper<VoucherLog> {

View File

@@ -18,7 +18,7 @@ import java.util.Optional;
/**
* @author xxm
* @date 2022/3/14
* @since 2022/3/14
*/
@Slf4j
@Repository

View File

@@ -6,7 +6,7 @@ import org.apache.ibatis.annotations.Mapper;
/**
* @author xxm
* @date 2022/3/14
* @since 2022/3/14
*/
@Mapper
public interface VoucherMapper extends BaseMapper<Voucher> {

View File

@@ -10,7 +10,7 @@ import java.util.Optional;
/**
* @author xxm
* @date 2022/3/14
* @since 2022/3/14
*/
@Slf4j
@Repository

View File

@@ -6,7 +6,7 @@ import org.apache.ibatis.annotations.Mapper;
/**
* @author xxm
* @date 2022/3/14
* @since 2022/3/14
*/
@Mapper
public interface VoucherPaymentMapper extends BaseMapper<VoucherPayment> {

View File

@@ -17,7 +17,7 @@ import java.time.LocalDateTime;
* 储值卡
*
* @author xxm
* @date 2022/3/14
* @since 2022/3/14
*/
@EqualsAndHashCode(callSuper = true)
@Data

View File

@@ -13,7 +13,7 @@ import java.math.BigDecimal;
* 储值卡日志
*
* @author xxm
* @date 2022/3/17
* @since 2022/3/17
*/
@EqualsAndHashCode(callSuper = true)
@Data

View File

@@ -13,7 +13,7 @@ import lombok.experimental.Accessors;
* 储值卡支付记录
*
* @author xxm
* @date 2022/3/14
* @since 2022/3/14
*/
@EqualsAndHashCode(callSuper = true)
@Data

View File

@@ -9,7 +9,7 @@ import org.springframework.stereotype.Service;
* 储值卡日志
*
* @author xxm
* @date 2022/3/19
* @since 2022/3/19
*/
@Slf4j
@Service

View File

@@ -34,7 +34,7 @@ import java.util.stream.Collectors;
* 储值卡支付
*
* @author xxm
* @date 2022/3/14
* @since 2022/3/14
*/
@Slf4j
@Service

View File

@@ -24,7 +24,7 @@ import java.util.stream.Collectors;
* 储值卡支付记录
*
* @author xxm
* @date 2022/3/14
* @since 2022/3/14
*/
@Slf4j
@Service

View File

@@ -16,7 +16,7 @@ import org.springframework.stereotype.Service;
* 储值卡查询
*
* @author xxm
* @date 2022/3/14
* @since 2022/3/14
*/
@Slf4j
@Service

View File

@@ -23,7 +23,7 @@ import java.util.stream.Collectors;
* 储值卡
*
* @author xxm
* @date 2022/3/14
* @since 2022/3/14
*/
@Slf4j
@Service

View File

@@ -13,7 +13,7 @@ import org.mapstruct.factory.Mappers;
* 转换
*
* @author xxm
* @date 2021/8/20
* @since 2021/8/20
*/
@Mapper
public interface WalletConvert {

View File

@@ -17,7 +17,7 @@ import java.util.Optional;
* 钱包日志
*
* @author xxm
* @date 2020/12/8
* @since 2020/12/8
*/
@Repository
@RequiredArgsConstructor

View File

@@ -8,7 +8,7 @@ import org.apache.ibatis.annotations.Mapper;
* 钱包日志
*
* @author xxm
* @date 2020/12/8
* @since 2020/12/8
*/
@Mapper
public interface WalletLogMapper extends BaseMapper<WalletLog> {

View File

@@ -25,7 +25,7 @@ import java.util.stream.Collectors;
* 钱包管理
*
* @author xxm
* @date 2020/12/8
* @since 2020/12/8
*/
@Repository
@RequiredArgsConstructor
@@ -34,14 +34,25 @@ public class WalletManager extends BaseManager<WalletMapper, Wallet> {
private final WalletMapper walletMapper;
/**
* 增加余额
* @param walletId 钱包
* 预占额度
* @param walletId 钱包ID
* @param amount 金额
* @return 更新数量
*/
public int increaseBalance(Long walletId, BigDecimal amount) {
public int freezeBalance(Long walletId, BigDecimal amount){
Long userId = SecurityUtil.getUserIdOrDefaultId();
return walletMapper.increaseBalance(walletId, amount, userId, LocalDateTime.now());
return walletMapper.freezeBalance(walletId, amount, userId, LocalDateTime.now());
}
/**
* 解冻金额
* @param walletId 钱包ID
* @param amount 金额
* @return 更新数量
*/
public int unfreezeBalance(Long walletId, BigDecimal amount){
Long userId = SecurityUtil.getUserIdOrDefaultId();
return walletMapper.unfreezeBalance(walletId, amount, userId, LocalDateTime.now());
}
/**
@@ -56,9 +67,31 @@ public class WalletManager extends BaseManager<WalletMapper, Wallet> {
}
/**
* 扣减余额-允许扣成负数
* 扣减余额同时解除预冻结的额度
* @param walletId 钱包ID
* @param amount 扣减金额
* @return 操作条数
*/
public int reduceAndUnfreezeBalance(Long walletId, BigDecimal amount) {
Long userId = SecurityUtil.getUserIdOrDefaultId();
return walletMapper.reduceAndUnfreezeBalance(walletId, amount, userId, LocalDateTime.now());
}
/**
* 增加余额
* @param walletId 钱包
* @param amount 金额
* @return 更新数量
*/
public int increaseBalance(Long walletId, BigDecimal amount) {
Long userId = SecurityUtil.getUserIdOrDefaultId();
return walletMapper.increaseBalance(walletId, amount, userId, LocalDateTime.now());
}
/**
* 更改余额-允许扣成负数
* @param walletId 钱包ID
* @param amount 更改的额度, 正数增加,负数减少
* @return 剩余条数
*/
public int reduceBalanceUnlimited(Long walletId, BigDecimal amount) {
@@ -66,13 +99,6 @@ public class WalletManager extends BaseManager<WalletMapper, Wallet> {
return walletMapper.reduceBalanceUnlimited(walletId, amount, userId, LocalDateTime.now());
}
/**
* 更新钱包状态
*/
public void setUpStatus(Long walletId, String status) {
lambdaUpdate().eq(Wallet::getId, walletId).set(Wallet::getStatus, status).update();
}
/**
* 用户钱包是否存在
*/

View File

@@ -16,11 +16,56 @@ import java.time.LocalDateTime;
* 钱包
*
* @author xxm
* @date 2021/2/24
* @since 2021/2/24
*/
@Mapper
public interface WalletMapper extends BaseMapper<Wallet> {
/**
* 预冻结余额
* @param walletId 钱包ID
* @param amount 冻结的额度
* @param operator 操作人
* @param date 时间
* @return 更新数量
*/
int freezeBalance(@Param("walletId") Long walletId, @Param("amount") BigDecimal amount,
@Param("operator") Long operator, @Param("date") LocalDateTime date);
/**
* 解冻金额
* @param walletId 钱包ID
* @param amount 冻结的额度
* @param operator 操作人
* @param date 时间
* @return 更新数量
*/
int unfreezeBalance(@Param("walletId") Long walletId, @Param("amount") BigDecimal amount,
@Param("operator") Long operator, @Param("date") LocalDateTime date);
/**
* 释放预占额度
* @param walletId 钱包ID
* @param amount 要释放的额度
* @param operator 操作人
* @param date 操作时间
* @return 操作条数
*/
int reduceBalance(@Param("walletId") Long walletId, @Param("amount") BigDecimal amount,
@Param("operator") Long operator, @Param("date") LocalDateTime date);
/**
* 扣减余额同时解除预冻结的额度
* @param walletId 钱包ID
* @param amount 减少的额度
* @param operator 操作人
* @param date 操作时间
* @return 操作条数
*/
int reduceAndUnfreezeBalance(@Param("walletId") Long walletId, @Param("amount") BigDecimal amount,
@Param("operator") Long operator, @Param("date") LocalDateTime date);
/**
* 增加余额
* @param walletId 钱包ID
@@ -30,29 +75,18 @@ public interface WalletMapper extends BaseMapper<Wallet> {
* @return 更新数量
*/
int increaseBalance(@Param("walletId") Long walletId, @Param("amount") BigDecimal amount,
@Param("operator") Long operator, @Param("date") LocalDateTime date);
@Param("operator") Long operator, @Param("date") LocalDateTime date);
/**
* 减少余额
* 扣减余额,允许扣成负数
* @param walletId 钱包ID
* @param amount 减少的额度
* @param operator 操作人
* @param date 操作时间
* @return 操作条数
*/
int reduceBalance(@Param("walletId") Long walletId, @Param("amount") BigDecimal amount,
@Param("operator") Long operator, @Param("date") LocalDateTime date);
/**
* 减少余额,允许扣成负数
* @param walletId 钱包ID
* @param amount 减少的额度
* @param amount 更改的额度, 正数增加,负数减少
* @param operator 操作人
* @param date 操作时间
* @return 操作条数
*/
int reduceBalanceUnlimited(@Param("walletId") Long walletId, @Param("amount") BigDecimal amount,
@Param("operator") Long operator, @Param("date") LocalDateTime date);
@Param("operator") Long operator, @Param("date") LocalDateTime date);
/**
* 待开通钱包的用户列表

View File

@@ -2,8 +2,10 @@ package cn.bootx.platform.daxpay.core.channel.wallet.entity;
import cn.bootx.mybatis.table.modify.annotation.DbColumn;
import cn.bootx.mybatis.table.modify.annotation.DbTable;
import cn.bootx.mybatis.table.modify.mybatis.mysq.annotation.DbMySqlIndex;
import cn.bootx.platform.common.core.function.EntityBaseFunction;
import cn.bootx.platform.common.mybatisplus.base.MpBaseEntity;
import cn.bootx.platform.daxpay.code.paymodel.WalletCode;
import cn.bootx.platform.daxpay.core.channel.wallet.convert.WalletConvert;
import cn.bootx.platform.daxpay.dto.channel.wallet.WalletDto;
import com.baomidou.mybatisplus.annotation.TableName;
@@ -17,7 +19,7 @@ import java.math.BigDecimal;
* 钱包
*
* @author xxm
* @date 2020/12/8
* @since 2020/12/8
*/
@EqualsAndHashCode(callSuper = true)
@Data
@@ -27,6 +29,7 @@ import java.math.BigDecimal;
public class Wallet extends MpBaseEntity implements EntityBaseFunction<WalletDto> {
/** 关联用户id */
@DbMySqlIndex
@DbColumn(comment = "关联用户id")
private Long userId;
@@ -36,9 +39,12 @@ public class Wallet extends MpBaseEntity implements EntityBaseFunction<WalletDto
/** 预冻结额度 */
@DbColumn(comment = "预冻结额度")
private BigDecimal freezeQuota;
private BigDecimal freezeBalance;
/** 状态 */
/**
* 状态
* @see WalletCode#STATUS_FORBIDDEN
*/
private String status;
@Override

View File

@@ -17,7 +17,7 @@ import java.math.BigDecimal;
* 钱包日志
*
* @author xxm
* @date 2020/12/8
* @since 2020/12/8
*/
@EqualsAndHashCode(callSuper = true)
@Data
@@ -48,7 +48,7 @@ public class WalletLog extends MpBaseEntity implements EntityBaseFunction<Wallet
/**
* 操作类型
* @see
* @see cn.bootx.platform.daxpay.code.paymodel.WalletCode#OPERATION_SOURCE_USER
*/
@DbColumn(comment = "操作类型")
private String operationSource;

View File

@@ -14,7 +14,7 @@ import lombok.experimental.Accessors;
* 钱包交易记录表
*
* @author xxm
* @date 2020/12/8
* @since 2020/12/8
*/
@EqualsAndHashCode(callSuper = true)
@Data

View File

@@ -15,7 +15,7 @@ import org.springframework.stereotype.Service;
* 钱包日志
*
* @author xxm
* @date 2020/12/8
* @since 2020/12/8
*/
@Slf4j
@Service

View File

@@ -24,7 +24,7 @@ import java.util.Optional;
* 钱包支付操作
*
* @author xxm
* @date 2021/2/27
* @since 2021/2/27
*/
@Slf4j
@Service
@@ -38,15 +38,14 @@ public class WalletPayService {
private final WalletLogManager walletLogManager;
/**
* 支付操作
* 支付前冻结余额操作
* @param amount 付款金额
* @param payment 支付记录
* @param wallet 钱包
*/
public void pay(BigDecimal amount, Payment payment, Wallet wallet) {
// 扣减余额
int i = walletManager.reduceBalance(wallet.getId(), amount);
public void freezeBalance(BigDecimal amount, Payment payment, Wallet wallet) {
// 冻结余额
int i = walletManager.freezeBalance(wallet.getId(), amount);
// 判断操作结果
if (i < 1) {
throw new WalletLackOfBalanceException();
@@ -56,17 +55,74 @@ public class WalletPayService {
.setUserId(wallet.getUserId())
.setPaymentId(payment.getId())
.setAmount(amount)
.setType(WalletCode.LOG_PAY)
.setRemark(String.format("钱包支付金额 %.2f ", amount))
.setType(WalletCode.LOG_FREEZE_BALANCE)
.setRemark(String.format("钱包预冻结金额 %.2f ", amount))
.setOperationSource(WalletCode.OPERATION_SOURCE_USER)
.setBusinessId(payment.getBusinessId());
walletLogManager.save(walletLog);
}
/**
* 取消支付并返还金额
* 支付成功更新
*/
public void close(Long paymentId) {
public void success(Long paymentId){
// 钱包支付记录
walletPaymentManager.findByPaymentId(paymentId).ifPresent(walletPayment -> {
Optional<Wallet> walletOpt = walletManager.findById(walletPayment.getWalletId());
if (!walletOpt.isPresent()) {
log.error("钱包出现恶性问题,需要人工排查");
return;
}
Wallet wallet = walletOpt.get();
walletPayment.setPayStatus(PayStatusCode.TRADE_CANCEL);
walletPaymentManager.save(walletPayment);
// 支付余额
int i = walletManager.reduceAndUnfreezeBalance(wallet.getId(), walletPayment.getAmount());
// 判断操作结果
if (i < 1) {
throw new WalletLackOfBalanceException();
}
// 日志
WalletLog walletLog = new WalletLog().setWalletId(wallet.getId())
.setUserId(wallet.getUserId())
.setPaymentId(walletPayment.getPaymentId())
.setAmount(walletPayment.getAmount())
.setType(WalletCode.LOG_REDUCE_AND_UNFREEZE_BALANCE)
.setRemark(String.format("钱包扣款金额 %.2f ", walletPayment.getAmount()))
.setOperationSource(WalletCode.OPERATION_SOURCE_USER)
.setBusinessId(walletPayment.getBusinessId());
walletLogManager.save(walletLog);
});
}
/**
* 直接进行支付
*/
public void pay(BigDecimal amount, Payment payment, Wallet wallet){
// 直接扣减余额
int i = walletManager.reduceBalance(wallet.getId(), amount);
// 判断操作结果
if (i < 1) {
throw new WalletLackOfBalanceException();
}
// 日志
WalletLog walletLog = new WalletLog().setWalletId(wallet.getId())
.setUserId(wallet.getUserId())
.setPaymentId(payment.getId())
.setAmount(amount)
.setType(WalletCode.LOG_PAY)
.setRemark(String.format("钱包支付金额 %.2f ", amount))
.setOperationSource(WalletCode.OPERATION_SOURCE_USER)
.setBusinessId(payment.getBusinessId());
walletLogManager.save(walletLog);
}
/**
* 取消支付, 可配置解除冻结金额
* @param freeze 是否是冻结模式, 是的话对冻结金额进行解冻, 不是的话返还扣减的金额
*/
public void close(Long paymentId,boolean freeze) {
// 钱包支付记录
walletPaymentManager.findByPaymentId(paymentId).ifPresent(walletPayment -> {
Optional<Wallet> walletOpt = walletManager.findById(walletPayment.getWalletId());
@@ -78,18 +134,24 @@ public class WalletPayService {
walletPayment.setPayStatus(PayStatusCode.TRADE_CANCEL);
walletPaymentManager.save(walletPayment);
// 金额返还
walletManager.increaseBalance(wallet.getId(), walletPayment.getAmount());
// 是否在进行了额度冻结
if (freeze){
// 解冻金额
walletManager.unfreezeBalance(wallet.getId(), walletPayment.getAmount());
} else {
// 金额返还
walletManager.increaseBalance(wallet.getId(), walletPayment.getAmount());
}
// 记录日志
WalletLog walletLog = new WalletLog().setAmount(walletPayment.getAmount())
.setPaymentId(walletPayment.getPaymentId())
.setWalletId(wallet.getId())
.setUserId(wallet.getUserId())
.setType(WalletCode.LOG_PAY_CLOSE)
.setRemark(String.format("取消支付返回金额 %.2f ", walletPayment.getAmount()))
.setOperationSource(WalletCode.OPERATION_SOURCE_SYSTEM)
.setBusinessId(walletPayment.getBusinessId());
.setPaymentId(walletPayment.getPaymentId())
.setWalletId(wallet.getId())
.setUserId(wallet.getUserId())
.setType(freeze?WalletCode.LOG_CLOSE_PAY:WalletCode.LOG_UNFREEZE_BALANCE)
.setRemark(String.format("取消支付金额 %.2f ", walletPayment.getAmount()))
.setOperationSource(WalletCode.OPERATION_SOURCE_SYSTEM)
.setBusinessId(walletPayment.getBusinessId());
// save log
walletLogManager.save(walletLog);
});

View File

@@ -21,7 +21,7 @@ import java.util.Optional;
* 钱包交易记录的相关操作
*
* @author xxm
* @date 2020/12/8
* @since 2020/12/8
*/
@Slf4j
@Service

View File

@@ -22,7 +22,7 @@ import org.springframework.stereotype.Service;
* 钱包
*
* @author xxm
* @date 2022/3/11
* @since 2022/3/11
*/
@Slf4j
@Service

View File

@@ -9,10 +9,8 @@ import cn.bootx.platform.daxpay.core.channel.wallet.dao.WalletManager;
import cn.bootx.platform.daxpay.core.channel.wallet.entity.Wallet;
import cn.bootx.platform.daxpay.core.channel.wallet.entity.WalletLog;
import cn.bootx.platform.daxpay.exception.waller.WalletBannedException;
import cn.bootx.platform.daxpay.exception.waller.WalletLogError;
import cn.bootx.platform.daxpay.exception.waller.WalletNotExistsException;
import cn.bootx.platform.daxpay.param.channel.wallet.WalletRechargeParam;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -27,7 +25,7 @@ import java.util.stream.Collectors;
* 钱包的相关操作
*
* @author xxm
* @date 2020/12/8
* @since 2020/12/8
*/
@Slf4j
@Service
@@ -47,57 +45,82 @@ public class WalletService {
if (walletManager.existsByUser(userId)) {
throw new BizException("钱包已经开通");
}
Wallet wallet = new Wallet().setUserId(userId).setBalance(BigDecimal.ZERO).setStatus(WalletCode.STATUS_NORMAL);
Wallet wallet = new Wallet().setUserId(userId)
.setBalance(BigDecimal.ZERO)
.setFreezeBalance(BigDecimal.ZERO)
.setStatus(WalletCode.STATUS_NORMAL);
walletManager.save(wallet);
// 激活 log
WalletLog activeLog = new WalletLog().setWalletId(wallet.getId())
.setUserId(wallet.getUserId())
.setType(WalletCode.LOG_ACTIVE)
.setRemark("激活钱包")
.setOperationSource(WalletCode.OPERATION_SOURCE_USER);
.setUserId(wallet.getUserId())
.setType(WalletCode.LOG_ACTIVE)
.setRemark("激活钱包")
.setOperationSource(WalletCode.OPERATION_SOURCE_USER);
walletLogManager.save(activeLog);
}
/**
* 批量开通
*/
@Transactional(rollbackFor = Exception.class)
public void createWalletBatch(List<Long> userIds) {
// 查询出
List<Long> existUserIds = walletManager.findExistUserIds(userIds);
userIds.removeAll(existUserIds);
List<Wallet> wallets = userIds.stream()
.map(userId -> new Wallet().setUserId(userId)
.setStatus(WalletCode.STATUS_NORMAL)
.setBalance(BigDecimal.ZERO))
.collect(Collectors.toList());
.map(userId -> new Wallet().setUserId(userId)
.setStatus(WalletCode.STATUS_NORMAL)
.setFreezeBalance(BigDecimal.ZERO)
.setBalance(BigDecimal.ZERO))
.collect(Collectors.toList());
walletManager.saveAll(wallets);
List<WalletLog> walletLogs = wallets.stream()
.map(wallet -> new WalletLog().setWalletId(wallet.getId())
.setUserId(wallet.getUserId())
.setAmount(BigDecimal.ZERO)
.setType(WalletCode.LOG_ACTIVE)
.setRemark("激活钱包")
.setOperationSource(WalletCode.OPERATION_SOURCE_USER))
.collect(Collectors.toList());
.map(wallet -> new WalletLog().setWalletId(wallet.getId())
.setUserId(wallet.getUserId())
.setAmount(BigDecimal.ZERO)
.setType(WalletCode.LOG_ACTIVE)
.setRemark("激活钱包")
.setOperationSource(WalletCode.OPERATION_SOURCE_USER))
.collect(Collectors.toList());
walletLogManager.saveAll(walletLogs);
}
/**
* 锁定钱包
*/
@Transactional(rollbackFor = Exception.class)
public void lock(Long walletId) {
walletManager.setUpStatus(walletId, WalletCode.STATUS_FORBIDDEN);
Wallet wallet = walletManager.findById(walletId).orElseThrow(DataNotExistException::new);
wallet.setStatus(WalletCode.STATUS_FORBIDDEN);
walletManager.updateById(wallet);
// 激活 log
WalletLog log = new WalletLog().setWalletId(wallet.getId())
.setUserId(wallet.getUserId())
.setType(WalletCode.LOG_LOCK)
.setRemark("锁定钱包")
.setOperationSource(WalletCode.OPERATION_SOURCE_ADMIN);
walletLogManager.save(log);
}
/**
* 解锁钱包
*/
@Transactional(rollbackFor = Exception.class)
public void unlock(Long walletId) {
walletManager.setUpStatus(walletId, WalletCode.STATUS_NORMAL);
Wallet wallet = walletManager.findById(walletId).orElseThrow(DataNotExistException::new);
wallet.setStatus(WalletCode.STATUS_NORMAL);
walletManager.updateById(wallet);
// 激活 log
WalletLog log = new WalletLog().setWalletId(wallet.getId())
.setUserId(wallet.getUserId())
.setType(WalletCode.LOG_UNLOCK)
.setRemark("解锁钱包")
.setOperationSource(WalletCode.OPERATION_SOURCE_ADMIN);
walletLogManager.save(log);
}
/**
* 充值操作 也可以扣款
* 更改余额 也可以扣款
*/
@Transactional(rollbackFor = Exception.class)
public void changerBalance(WalletRechargeParam param) {
@@ -112,57 +135,14 @@ public class WalletService {
}
Wallet wallet = walletManager.findById(param.getWalletId()).orElseThrow(DataNotExistException::new);
WalletLog walletLog = new WalletLog().setAmount(param.getAmount())
.setWalletId(wallet.getId())
.setType(WalletCode.LOG_ADMIN_CHANGER)
.setUserId(wallet.getUserId())
.setRemark(String.format("系统变动余额 %.2f ", param.getAmount()))
.setOperationSource(WalletCode.OPERATION_SOURCE_ADMIN);
.setWalletId(wallet.getId())
.setType(WalletCode.LOG_ADMIN_CHANGER)
.setUserId(wallet.getUserId())
.setRemark(String.format("系统变动余额 %.2f ", param.getAmount()))
.setOperationSource(WalletCode.OPERATION_SOURCE_ADMIN);
walletLogManager.save(walletLog);
}
/**
* 根据支付单对钱包充值的余额进行扣减
*/
@Transactional(rollbackFor = Exception.class)
public void deductedBalanceByPaymentId(Long paymentId, Long orderId, String remark, Boolean isThrowError) {
// 根据支付记录ID查询交易的金额和交易的钱包ID
WalletLog walletLog = walletLogManager.findFirstByPayment(paymentId).orElseThrow(DataNotExistException::new);
if (walletLog == null) {
return;
}
// 充值类型
List<String> chargeLogType = Lists.newArrayList(WalletCode.LOG_RECHARGE, WalletCode.LOG_AUTO_RECHARGE,
WalletCode.LOG_ADMIN_CHANGER);
// 保证是充值类型 且充值金额大于0
if (!chargeLogType.contains(walletLog.getType())
|| BigDecimalUtil.compareTo(walletLog.getAmount(), BigDecimal.ZERO) < 0) {
log.warn("退款 发现非充值交易日志ID:{},交易类型:{}", walletLog.getId(), walletLog.getType());
if (isThrowError) {
throw new WalletLogError();
}
return;
}
// 获取钱包ID 并扣减对应金额(允许扣成负数)
walletManager.reduceBalanceUnlimited(walletLog.getWalletId(), walletLog.getAmount());
// 记录日志
WalletLog log = new WalletLog().setWalletId(walletLog.getWalletId())
.setUserId(walletLog.getUserId())
.setPaymentId(paymentId)
.setAmount(walletLog.getAmount())
.setType(WalletCode.LOG_SYSTEM_REDUCE_BALANCE)
.setRemark(String.format("系统减少余额 %.2f (" + remark + ")", walletLog.getAmount()))
.setOperationSource(WalletCode.OPERATION_SOURCE_SYSTEM)
.setPaymentId(paymentId)
.setBusinessId(String.valueOf(orderId));
walletLogManager.save(log);
}
/**
* 查询钱包,如果钱包不存在或者钱包被禁用将抛出异常
*/
@@ -188,5 +168,4 @@ public class WalletService {
}
return wallet;
}
}

View File

@@ -12,7 +12,7 @@ import org.mapstruct.factory.Mappers;
* 微信转换类
*
* @author xxm
* @date 2021/6/21
* @since 2021/6/21
*/
@Mapper
public interface WeChatConvert {

View File

@@ -18,7 +18,7 @@ import java.util.Optional;
* 微信支付配置
*
* @author xxm
* @date 2021/3/19
* @since 2021/3/19
*/
@Repository
@RequiredArgsConstructor

View File

@@ -8,7 +8,7 @@ import org.apache.ibatis.annotations.Mapper;
* 微信支付配置
*
* @author xxm
* @date 2021/3/19
* @since 2021/3/19
*/
@Mapper
public interface WeChatPayConfigMapper extends BaseMapper<WeChatPayConfig> {

View File

@@ -12,7 +12,7 @@ import java.util.Optional;
* 微信支付记录
*
* @author xxm
* @date 2021/6/21
* @since 2021/6/21
*/
@Slf4j
@Repository

View File

@@ -23,7 +23,7 @@ import lombok.experimental.Accessors;
* 微信支付配置
*
* @author xxm
* @date 2021/3/1
* @since 2021/3/1
*/
@EqualsAndHashCode(callSuper = true)
@Data

View File

@@ -11,7 +11,7 @@ import lombok.experimental.Accessors;
/**
* @author xxm
* @date 2021/6/21
* @since 2021/6/21
*/
@EqualsAndHashCode(callSuper = true)
@Data

Some files were not shown because too many files have changed in this diff Show More