Commit 0e1b8126dd6cd28e1cd55224810fb60c7b8eb08e
1 parent
bcb3b194
新增钱包充值。启动报错修复
Showing
14 changed files
with
335 additions
and
14 deletions
sql/mysql/pay_wallet.sql
| @@ -57,12 +57,11 @@ CREATE TABLE `pay_wallet_recharge` | @@ -57,12 +57,11 @@ CREATE TABLE `pay_wallet_recharge` | ||
| 57 | `pay_order_id` bigint NULL COMMENT '支付订单编号', | 57 | `pay_order_id` bigint NULL COMMENT '支付订单编号', |
| 58 | `pay_channel_code` varchar(16) NULL COMMENT '支付成功的支付渠道', | 58 | `pay_channel_code` varchar(16) NULL COMMENT '支付成功的支付渠道', |
| 59 | `pay_time` datetime NULL COMMENT '订单支付时间', | 59 | `pay_time` datetime NULL COMMENT '订单支付时间', |
| 60 | - `wallet_transaction_id` bigint NULL COMMENT '充值钱包流水', | ||
| 61 | `pay_refund_id` bigint NULL COMMENT '支付退款单编号', | 60 | `pay_refund_id` bigint NULL COMMENT '支付退款单编号', |
| 62 | `refund_price` int NOT NULL DEFAULT 0 COMMENT '退款金额,包含赠送金额', | 61 | `refund_price` int NOT NULL DEFAULT 0 COMMENT '退款金额,包含赠送金额', |
| 63 | `refund_pay_price` int NOT NULL DEFAULT 0 COMMENT '退款支付金额', | 62 | `refund_pay_price` int NOT NULL DEFAULT 0 COMMENT '退款支付金额', |
| 64 | `refund_wallet_bonus` int NOT NULL DEFAULT 0 COMMENT '退款钱包赠送金额', | 63 | `refund_wallet_bonus` int NOT NULL DEFAULT 0 COMMENT '退款钱包赠送金额', |
| 65 | - `refund_wallet_transaction_id` bigint NULL COMMENT '充值退款钱包流水', | 64 | + `refund_time` datetime NULL COMMENT '退款时间', |
| 66 | `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者', | 65 | `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者', |
| 67 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', | 66 | `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', |
| 68 | `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者', | 67 | `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者', |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/combination/CombinationApiImpl.java
| @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.promotion.api.combination; | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.promotion.api.combination; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.module.promotion.api.combination.dto.CombinationActivityUpdateStockReqDTO; | 3 | import cn.iocoder.yudao.module.promotion.api.combination.dto.CombinationActivityUpdateStockReqDTO; |
| 4 | import cn.iocoder.yudao.module.promotion.service.combination.CombinationActivityService; | 4 | import cn.iocoder.yudao.module.promotion.service.combination.CombinationActivityService; |
| 5 | +import org.springframework.stereotype.Service; | ||
| 5 | 6 | ||
| 6 | import javax.annotation.Resource; | 7 | import javax.annotation.Resource; |
| 7 | 8 | ||
| @@ -10,6 +11,7 @@ import javax.annotation.Resource; | @@ -10,6 +11,7 @@ import javax.annotation.Resource; | ||
| 10 | * | 11 | * |
| 11 | * @author HUIHUI | 12 | * @author HUIHUI |
| 12 | */ | 13 | */ |
| 14 | +@Service | ||
| 13 | public class CombinationApiImpl implements CombinationApi { | 15 | public class CombinationApiImpl implements CombinationApi { |
| 14 | 16 | ||
| 15 | @Resource | 17 | @Resource |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/combination/CombinationRecordServiceImpl.java
| @@ -38,6 +38,7 @@ import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*; | @@ -38,6 +38,7 @@ import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*; | ||
| 38 | public class CombinationRecordServiceImpl implements CombinationRecordService { | 38 | public class CombinationRecordServiceImpl implements CombinationRecordService { |
| 39 | 39 | ||
| 40 | @Resource | 40 | @Resource |
| 41 | + @Lazy | ||
| 41 | private CombinationActivityService combinationActivityService; | 42 | private CombinationActivityService combinationActivityService; |
| 42 | @Resource | 43 | @Resource |
| 43 | private CombinationRecordMapper recordMapper; | 44 | private CombinationRecordMapper recordMapper; |
yudao-module-pay/yudao-module-pay-api/src/main/java/cn/iocoder/yudao/module/pay/enums/ErrorCodeConstants.java
| @@ -46,6 +46,11 @@ public interface ErrorCodeConstants { | @@ -46,6 +46,11 @@ public interface ErrorCodeConstants { | ||
| 46 | ErrorCode WALLET_TRANSACTION_NOT_FOUND = new ErrorCode(1007007002, "未找到对应的钱包交易"); | 46 | ErrorCode WALLET_TRANSACTION_NOT_FOUND = new ErrorCode(1007007002, "未找到对应的钱包交易"); |
| 47 | ErrorCode WALLET_REFUND_AMOUNT_ERROR = new ErrorCode(1007007003, "钱包退款金额不对"); | 47 | ErrorCode WALLET_REFUND_AMOUNT_ERROR = new ErrorCode(1007007003, "钱包退款金额不对"); |
| 48 | ErrorCode WALLET_REFUND_EXIST = new ErrorCode(1007007004, "已经存在钱包退款"); | 48 | ErrorCode WALLET_REFUND_EXIST = new ErrorCode(1007007004, "已经存在钱包退款"); |
| 49 | + ErrorCode WALLET_RECHARGE_NOT_FOUND = new ErrorCode(1007007005, "钱包充值记录不存在"); | ||
| 50 | + ErrorCode WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID = new ErrorCode(1007007006, "钱包充值更新支付状态失败,钱包充值记录不是【未支付】状态"); | ||
| 51 | + ErrorCode WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR = new ErrorCode(1007007007, "钱包充值更新支付状态失败,支付单编号不匹配"); | ||
| 52 | + ErrorCode WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_STATUS_NOT_SUCCESS = new ErrorCode(1007007008, "钱包充值更新支付状态失败,支付单状态不是【支付成功】状态"); | ||
| 53 | + ErrorCode WALLET_RECHARGE_UPDATE_PAID_PAY_PRICE_NOT_MATCH = new ErrorCode(1007007009, "钱包充值更新支付状态失败,支付单金额不匹配"); | ||
| 49 | 54 | ||
| 50 | // ========== 示例订单 1007900000 ========== | 55 | // ========== 示例订单 1007900000 ========== |
| 51 | ErrorCode DEMO_ORDER_NOT_FOUND = new ErrorCode(1007900000, "示例订单不存在"); | 56 | ErrorCode DEMO_ORDER_NOT_FOUND = new ErrorCode(1007900000, "示例订单不存在"); |
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/app/wallet/AppPayWalletRechargeController.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.pay.controller.app.wallet; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.common.pojo.CommonResult; | ||
| 4 | +import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; | ||
| 5 | +import cn.iocoder.yudao.module.pay.api.notify.dto.PayOrderNotifyReqDTO; | ||
| 6 | +import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge.AppPayWalletRechargeCreateReqVO; | ||
| 7 | +import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge.AppPayWalletRechargeCreateRespVO; | ||
| 8 | +import cn.iocoder.yudao.module.pay.convert.wallet.PayWalletRechargeConvert; | ||
| 9 | +import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO; | ||
| 10 | +import cn.iocoder.yudao.module.pay.service.wallet.PayWalletRechargeService; | ||
| 11 | +import io.swagger.v3.oas.annotations.Operation; | ||
| 12 | +import io.swagger.v3.oas.annotations.tags.Tag; | ||
| 13 | +import lombok.extern.slf4j.Slf4j; | ||
| 14 | +import org.springframework.validation.annotation.Validated; | ||
| 15 | +import org.springframework.web.bind.annotation.PostMapping; | ||
| 16 | +import org.springframework.web.bind.annotation.RequestBody; | ||
| 17 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
| 18 | +import org.springframework.web.bind.annotation.RestController; | ||
| 19 | + | ||
| 20 | +import javax.annotation.Resource; | ||
| 21 | +import javax.annotation.security.PermitAll; | ||
| 22 | +import javax.validation.Valid; | ||
| 23 | + | ||
| 24 | +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | ||
| 25 | +import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId; | ||
| 26 | +import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserType; | ||
| 27 | + | ||
| 28 | +@Tag(name = "用户 APP - 钱包充值") | ||
| 29 | +@RestController | ||
| 30 | +@RequestMapping("/pay/wallet-recharge") | ||
| 31 | +@Validated | ||
| 32 | +@Slf4j | ||
| 33 | +public class AppPayWalletRechargeController { | ||
| 34 | + | ||
| 35 | + @Resource | ||
| 36 | + private PayWalletRechargeService payWalletRechargeService; | ||
| 37 | + | ||
| 38 | + @PostMapping("/create") | ||
| 39 | + @Operation(summary = "创建钱包充值记录") | ||
| 40 | + public CommonResult<AppPayWalletRechargeCreateRespVO> createWalletRecharge(@Valid @RequestBody AppPayWalletRechargeCreateReqVO reqVO) { | ||
| 41 | + PayWalletRechargeDO walletRecharge = payWalletRechargeService.createWalletRecharge(getLoginUserId(), | ||
| 42 | + getLoginUserType(), reqVO); | ||
| 43 | + return success(PayWalletRechargeConvert.INSTANCE.convert(walletRecharge)); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + @PostMapping("/update-paid") | ||
| 47 | + @Operation(summary = "更新钱包充值为已充值") // 由 pay-module 支付服务,进行回调,可见 PayNotifyJob | ||
| 48 | + @PermitAll // 无需登录,安全由 内部校验实现 | ||
| 49 | + @OperateLog(enable = false) // 禁用操作日志,因为没有操作人 | ||
| 50 | + public CommonResult<Boolean> updateWalletRechargerPaid(@RequestBody PayOrderNotifyReqDTO notifyReqDTO) { | ||
| 51 | + payWalletRechargeService.updateWalletRechargerPaid(Long.valueOf(notifyReqDTO.getMerchantOrderId()), | ||
| 52 | + notifyReqDTO.getPayOrderId()); | ||
| 53 | + return success(true); | ||
| 54 | + } | ||
| 55 | +} |
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/app/wallet/AppPayWalletTransactionController.java
| @@ -18,7 +18,6 @@ import org.springframework.web.bind.annotation.RestController; | @@ -18,7 +18,6 @@ import org.springframework.web.bind.annotation.RestController; | ||
| 18 | 18 | ||
| 19 | import javax.annotation.Resource; | 19 | import javax.annotation.Resource; |
| 20 | import javax.validation.Valid; | 20 | import javax.validation.Valid; |
| 21 | - | ||
| 22 | import java.time.LocalDateTime; | 21 | import java.time.LocalDateTime; |
| 23 | 22 | ||
| 24 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | 23 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/app/wallet/vo/recharge/AppPayWalletRechargeCreateReqVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge; | ||
| 2 | + | ||
| 3 | +import io.swagger.v3.oas.annotations.media.Schema; | ||
| 4 | +import lombok.Data; | ||
| 5 | + | ||
| 6 | +import javax.validation.constraints.DecimalMin; | ||
| 7 | +import javax.validation.constraints.NotNull; | ||
| 8 | + | ||
| 9 | +@Schema(description = "用户 APP - 创建钱包充值 Request VO") | ||
| 10 | +@Data | ||
| 11 | +public class AppPayWalletRechargeCreateReqVO { | ||
| 12 | + | ||
| 13 | + @Schema(description = "支付金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "1000") | ||
| 14 | + @NotNull(message = "支付金额不能为空") | ||
| 15 | + @DecimalMin(value = "0", inclusive = false, message = "支付金额必须大于零") | ||
| 16 | + private Integer payPrice; | ||
| 17 | + | ||
| 18 | + @Schema(description = "钱包赠送金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "1000") | ||
| 19 | + @NotNull(message = "钱包赠送金额不能为空") | ||
| 20 | + @DecimalMin(value = "0", message = "钱包赠送金额必须大于等于零") | ||
| 21 | + private Integer walletBonus; | ||
| 22 | +} |
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/app/wallet/vo/recharge/AppPayWalletRechargeCreateRespVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge; | ||
| 2 | + | ||
| 3 | +import io.swagger.v3.oas.annotations.media.Schema; | ||
| 4 | +import lombok.Data; | ||
| 5 | + | ||
| 6 | +@Schema(description = "用户 APP - 创建钱包充值 Resp VO") | ||
| 7 | +@Data | ||
| 8 | +public class AppPayWalletRechargeCreateRespVO { | ||
| 9 | + | ||
| 10 | + @Schema(description = "钱包充值编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") | ||
| 11 | + private Long id; | ||
| 12 | + | ||
| 13 | + @Schema(description = "支付订单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "100") | ||
| 14 | + private Long payOrderId; | ||
| 15 | +} |
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/convert/wallet/PayWalletRechargeConvert.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.pay.convert.wallet; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge.AppPayWalletRechargeCreateReqVO; | ||
| 4 | +import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge.AppPayWalletRechargeCreateRespVO; | ||
| 5 | +import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO; | ||
| 6 | +import org.mapstruct.Mapper; | ||
| 7 | +import org.mapstruct.factory.Mappers; | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * @author jason | ||
| 11 | + */ | ||
| 12 | +@Mapper | ||
| 13 | +public interface PayWalletRechargeConvert { | ||
| 14 | + | ||
| 15 | + PayWalletRechargeConvert INSTANCE = Mappers.getMapper(PayWalletRechargeConvert.class); | ||
| 16 | + | ||
| 17 | + PayWalletRechargeDO convert(AppPayWalletRechargeCreateReqVO vo); | ||
| 18 | + | ||
| 19 | + default PayWalletRechargeDO convert(Long walletId, AppPayWalletRechargeCreateReqVO vo) { | ||
| 20 | + PayWalletRechargeDO walletRecharge = convert(vo); | ||
| 21 | + return walletRecharge.setWalletId(walletId) | ||
| 22 | + .setPrice(walletRecharge.getPayPrice() + walletRecharge.getWalletBonus()); | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + AppPayWalletRechargeCreateRespVO convert(PayWalletRechargeDO bean); | ||
| 26 | + | ||
| 27 | +} |
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/wallet/PayWalletMapper.java
| @@ -42,6 +42,19 @@ public interface PayWalletMapper extends BaseMapperX<PayWalletDO> { | @@ -42,6 +42,19 @@ public interface PayWalletMapper extends BaseMapperX<PayWalletDO> { | ||
| 42 | .ge(PayWalletDO::getBalance, price); // cas 逻辑 | 42 | .ge(PayWalletDO::getBalance, price); // cas 逻辑 |
| 43 | return update(null, lambdaUpdateWrapper); | 43 | return update(null, lambdaUpdateWrapper); |
| 44 | } | 44 | } |
| 45 | + | ||
| 46 | + /** | ||
| 47 | + * 当充值的时候,更新钱包 | ||
| 48 | + * @param price 钱包金额 | ||
| 49 | + * @param id 钱包 id | ||
| 50 | + */ | ||
| 51 | + default int updateWhenRecharge(Integer price, Long id){ | ||
| 52 | + LambdaUpdateWrapper<PayWalletDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<PayWalletDO>() | ||
| 53 | + .setSql(" balance = balance + " + price | ||
| 54 | + + ", total_recharge = total_recharge + " + price) | ||
| 55 | + .eq(PayWalletDO::getId, id); | ||
| 56 | + return update(null, lambdaUpdateWrapper); | ||
| 57 | + } | ||
| 45 | } | 58 | } |
| 46 | 59 | ||
| 47 | 60 |
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletRechargeService.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.pay.service.wallet; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge.AppPayWalletRechargeCreateReqVO; | ||
| 4 | +import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * 钱包充值 Service 接口 | ||
| 8 | + * | ||
| 9 | + * @author jason | ||
| 10 | + */ | ||
| 11 | +public interface PayWalletRechargeService { | ||
| 12 | + | ||
| 13 | + /** | ||
| 14 | + * 创建钱包充值记录 | ||
| 15 | + * @param userId 用户 id | ||
| 16 | + * @param userType 用户类型 | ||
| 17 | + * @param vo 钱包充值请求 vo | ||
| 18 | + * @return 钱包充值记录 | ||
| 19 | + */ | ||
| 20 | + PayWalletRechargeDO createWalletRecharge(Long userId, Integer userType, AppPayWalletRechargeCreateReqVO vo); | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * 更新钱包充值成功 | ||
| 25 | + * @param walletRechargeId 钱包充值 id | ||
| 26 | + * @param payOrderId 支付订单 id | ||
| 27 | + */ | ||
| 28 | + void updateWalletRechargerPaid(Long walletRechargeId, Long payOrderId); | ||
| 29 | +} |
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletRechargeServiceImpl.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.pay.service.wallet; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO; | ||
| 4 | +import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge.AppPayWalletRechargeCreateReqVO; | ||
| 5 | +import cn.iocoder.yudao.module.pay.convert.wallet.PayWalletRechargeConvert; | ||
| 6 | +import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO; | ||
| 7 | +import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO; | ||
| 8 | +import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO; | ||
| 9 | +import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletRechargeMapper; | ||
| 10 | +import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum; | ||
| 11 | +import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum; | ||
| 12 | +import cn.iocoder.yudao.module.pay.service.order.PayOrderService; | ||
| 13 | +import lombok.extern.slf4j.Slf4j; | ||
| 14 | +import org.springframework.stereotype.Service; | ||
| 15 | +import org.springframework.transaction.annotation.Transactional; | ||
| 16 | + | ||
| 17 | +import javax.annotation.Resource; | ||
| 18 | +import java.time.Duration; | ||
| 19 | +import java.time.LocalDateTime; | ||
| 20 | + | ||
| 21 | +import static cn.hutool.core.util.ObjectUtil.notEqual; | ||
| 22 | +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | ||
| 23 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.addTime; | ||
| 24 | +import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; | ||
| 25 | +import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP; | ||
| 26 | +import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*; | ||
| 27 | + | ||
| 28 | +/** | ||
| 29 | + * 钱包充值 Service 实现类 | ||
| 30 | + * | ||
| 31 | + * @author jason | ||
| 32 | + */ | ||
| 33 | +@Service | ||
| 34 | +@Slf4j | ||
| 35 | +public class PayWalletRechargeServiceImpl implements PayWalletRechargeService { | ||
| 36 | + /** | ||
| 37 | + * TODO 放到 配置文件中 | ||
| 38 | + */ | ||
| 39 | + private static final Long WALLET_PAY_APP_ID = 8L; | ||
| 40 | + | ||
| 41 | + private static final String WALLET_RECHARGE_ORDER_SUBJECT = "钱包余额充值"; | ||
| 42 | + | ||
| 43 | + @Resource | ||
| 44 | + private PayWalletRechargeMapper walletRechargeMapper; | ||
| 45 | + @Resource | ||
| 46 | + private PayWalletService payWalletService; | ||
| 47 | + @Resource | ||
| 48 | + private PayOrderService payOrderService; | ||
| 49 | + | ||
| 50 | + | ||
| 51 | + @Override | ||
| 52 | + @Transactional(rollbackFor = Exception.class) | ||
| 53 | + public PayWalletRechargeDO createWalletRecharge(Long userId, Integer userType, AppPayWalletRechargeCreateReqVO vo) { | ||
| 54 | + // 1. 获取钱包 | ||
| 55 | + PayWalletDO wallet = payWalletService.getOrCreateWallet(userId, userType); | ||
| 56 | + // 2. 新增钱包充值记录 | ||
| 57 | + PayWalletRechargeDO walletRecharge = PayWalletRechargeConvert.INSTANCE.convert(wallet.getId(), vo); | ||
| 58 | + walletRechargeMapper.insert(walletRecharge); | ||
| 59 | + // 3.创建支付单 | ||
| 60 | + Long payOrderId = payOrderService.createOrder(new PayOrderCreateReqDTO() | ||
| 61 | + .setAppId(WALLET_PAY_APP_ID).setUserIp(getClientIP()) | ||
| 62 | + .setMerchantOrderId(walletRecharge.getId().toString()) // 业务的订单编号 | ||
| 63 | + .setSubject(WALLET_RECHARGE_ORDER_SUBJECT).setBody("").setPrice(walletRecharge.getPayPrice()) | ||
| 64 | + .setExpireTime(addTime(Duration.ofHours(2L)))); | ||
| 65 | + // 4.更新钱包充值记录中支付订单 | ||
| 66 | + walletRechargeMapper.updateById(new PayWalletRechargeDO().setPayOrderId(payOrderId) | ||
| 67 | + .setId(walletRecharge.getId())); | ||
| 68 | + return walletRechargeMapper.selectById(walletRecharge.getId()); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + @Override | ||
| 72 | + @Transactional(rollbackFor = Exception.class) | ||
| 73 | + public void updateWalletRechargerPaid(Long walletRechargeId, Long payOrderId) { | ||
| 74 | + // 1. 获取钱包充值记录 | ||
| 75 | + PayWalletRechargeDO walletRecharge = walletRechargeMapper.selectById(walletRechargeId); | ||
| 76 | + if (walletRecharge == null) { | ||
| 77 | + log.error("[updateWalletRechargerPaid],钱包充值记录不存在,钱包充值 Id:{} ", walletRechargeId); | ||
| 78 | + throw exception(WALLET_RECHARGE_NOT_FOUND); | ||
| 79 | + } | ||
| 80 | + // 2. 校验钱包充值是否可以支付 | ||
| 81 | + PayOrderDO payOrderDO = validateWalletRechargerCanPaid(walletRecharge, payOrderId); | ||
| 82 | + // 3. 更新钱包充值的支付状态 | ||
| 83 | + int updateCount = walletRechargeMapper.updateByIdAndPaid(walletRechargeId,false, new PayWalletRechargeDO().setId(walletRechargeId) | ||
| 84 | + .setPayStatus(true).setPayTime(LocalDateTime.now()) | ||
| 85 | + .setPayChannelCode(payOrderDO.getChannelCode())); | ||
| 86 | + if (updateCount == 0) { | ||
| 87 | + throw exception(WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID); | ||
| 88 | + } | ||
| 89 | + // 4. 更新钱包余额 | ||
| 90 | + payWalletService.addWalletBalance(walletRecharge.getWalletId(), String.valueOf(walletRechargeId), | ||
| 91 | + PayWalletBizTypeEnum.RECHARGE, walletRecharge.getPrice()); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + private PayOrderDO validateWalletRechargerCanPaid(PayWalletRechargeDO walletRecharge, Long payOrderId) { | ||
| 95 | + | ||
| 96 | + // 1.1 校验充值记录的支付状态 | ||
| 97 | + if (walletRecharge.getPayStatus()) { | ||
| 98 | + log.error("[validateWalletRechargerCanPaid][钱包({}) 不处于未支付状态! 钱包数据是:{}]", | ||
| 99 | + walletRecharge.getId(), toJsonString(walletRecharge)); | ||
| 100 | + throw exception(WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID); | ||
| 101 | + } | ||
| 102 | + // 1.2 校验支付订单匹配 | ||
| 103 | + if (notEqual(walletRecharge.getPayOrderId(), payOrderId)) { // 支付单号 | ||
| 104 | + log.error("[validateWalletRechargerCanPaid][钱包({}) 支付单不匹配({}),请进行处理! 钱包数据是:{}]", | ||
| 105 | + walletRecharge.getId(), payOrderId, toJsonString(walletRecharge)); | ||
| 106 | + throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR); | ||
| 107 | + } | ||
| 108 | + // 2.1 校验支付单是否存在 | ||
| 109 | + PayOrderDO payOrder = payOrderService.getOrder(payOrderId); | ||
| 110 | + if (payOrder == null) { | ||
| 111 | + log.error("[validateWalletRechargerCanPaid][钱包({}) payOrder({}) 不存在,请进行处理!]", | ||
| 112 | + walletRecharge.getId(), payOrderId); | ||
| 113 | + throw exception(PAY_ORDER_NOT_FOUND); | ||
| 114 | + } | ||
| 115 | + // 2.2 校验支付单已支付 | ||
| 116 | + if (!PayOrderStatusEnum.isSuccess(payOrder.getStatus())) { | ||
| 117 | + log.error("[validateWalletRechargerCanPaid][钱包({}) payOrder({}) 未支付,请进行处理!payOrder 数据是:{}]", | ||
| 118 | + walletRecharge.getId(), payOrderId, toJsonString(payOrder)); | ||
| 119 | + throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_STATUS_NOT_SUCCESS); | ||
| 120 | + } | ||
| 121 | + // 2.3 校验支付金额一致 | ||
| 122 | + if (notEqual(payOrder.getPrice(), walletRecharge.getPayPrice())) { | ||
| 123 | + log.error("[validateDemoOrderCanPaid][钱包({}) payOrder({}) 支付金额不匹配,请进行处理!钱包 数据是:{},payOrder 数据是:{}]", | ||
| 124 | + walletRecharge.getId(), payOrderId, toJsonString(walletRecharge), toJsonString(payOrder)); | ||
| 125 | + throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_PRICE_NOT_MATCH); | ||
| 126 | + } | ||
| 127 | + // 2.4 校验支付订单的商户订单匹配 | ||
| 128 | + if (notEqual(payOrder.getMerchantOrderId(), walletRecharge.getId().toString())) { | ||
| 129 | + log.error("[validateDemoOrderCanPaid][钱包({}) 支付单不匹配({}),请进行处理!payOrder 数据是:{}]", | ||
| 130 | + walletRecharge.getId(), payOrderId, toJsonString(payOrder)); | ||
| 131 | + throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR); | ||
| 132 | + } | ||
| 133 | + return payOrder; | ||
| 134 | + } | ||
| 135 | +} |
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletService.java
| @@ -22,6 +22,13 @@ public interface PayWalletService { | @@ -22,6 +22,13 @@ public interface PayWalletService { | ||
| 22 | PayWalletDO getOrCreateWallet(Long userId, Integer userType); | 22 | PayWalletDO getOrCreateWallet(Long userId, Integer userType); |
| 23 | 23 | ||
| 24 | /** | 24 | /** |
| 25 | + * 获取钱包信息 | ||
| 26 | + * | ||
| 27 | + * @param walletId 钱包 id | ||
| 28 | + */ | ||
| 29 | + PayWalletDO getWallet(Long walletId); | ||
| 30 | + | ||
| 31 | + /** | ||
| 25 | * 钱包订单支付 | 32 | * 钱包订单支付 |
| 26 | * | 33 | * |
| 27 | * @param userId 用户 id | 34 | * @param userId 用户 id |
| @@ -56,14 +63,13 @@ public interface PayWalletService { | @@ -56,14 +63,13 @@ public interface PayWalletService { | ||
| 56 | /** | 63 | /** |
| 57 | * 增加钱包余额 | 64 | * 增加钱包余额 |
| 58 | * | 65 | * |
| 59 | - * @param userId 用户 id | ||
| 60 | - * @param userType 用户类型 | 66 | + * @param walletId 钱包 id |
| 61 | * @param bizId 业务关联 id | 67 | * @param bizId 业务关联 id |
| 62 | * @param bizType 业务关联分类 | 68 | * @param bizType 业务关联分类 |
| 63 | * @param price 增加金额 | 69 | * @param price 增加金额 |
| 64 | * @return 钱包流水 | 70 | * @return 钱包流水 |
| 65 | */ | 71 | */ |
| 66 | - PayWalletTransactionDO addWalletBalance(Long userId, Integer userType, | ||
| 67 | - Long bizId, PayWalletBizTypeEnum bizType, Integer price); | 72 | + PayWalletTransactionDO addWalletBalance(Long walletId, String bizId, |
| 73 | + PayWalletBizTypeEnum bizType, Integer price); | ||
| 68 | 74 | ||
| 69 | } | 75 | } |
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java
| @@ -56,6 +56,11 @@ public class PayWalletServiceImpl implements PayWalletService { | @@ -56,6 +56,11 @@ public class PayWalletServiceImpl implements PayWalletService { | ||
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | @Override | 58 | @Override |
| 59 | + public PayWalletDO getWallet(Long walletId) { | ||
| 60 | + return walletMapper.selectById(walletId); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + @Override | ||
| 59 | @Transactional(rollbackFor = Exception.class) | 64 | @Transactional(rollbackFor = Exception.class) |
| 60 | public PayWalletTransactionDO orderPay(Long userId, Integer userType, String outTradeNo, Integer price) { | 65 | public PayWalletTransactionDO orderPay(Long userId, Integer userType, String outTradeNo, Integer price) { |
| 61 | // 1. 判断支付交易拓展单是否存 | 66 | // 1. 判断支付交易拓展单是否存 |
| @@ -80,7 +85,7 @@ public class PayWalletServiceImpl implements PayWalletService { | @@ -80,7 +85,7 @@ public class PayWalletServiceImpl implements PayWalletService { | ||
| 80 | PayWalletDO wallet = walletMapper.selectById(walletId); | 85 | PayWalletDO wallet = walletMapper.selectById(walletId); |
| 81 | Assert.notNull(wallet, "钱包 {} 不存在", walletId); | 86 | Assert.notNull(wallet, "钱包 {} 不存在", walletId); |
| 82 | // 2. 增加余额 | 87 | // 2. 增加余额 |
| 83 | - return addWalletBalance(wallet.getUserId(), wallet.getUserType(), payRefund.getId(), PAYMENT_REFUND, refundPrice); | 88 | + return addWalletBalance(walletId, String.valueOf(payRefund.getId()), PAYMENT_REFUND, refundPrice); |
| 84 | } | 89 | } |
| 85 | 90 | ||
| 86 | /** | 91 | /** |
| @@ -139,25 +144,33 @@ public class PayWalletServiceImpl implements PayWalletService { | @@ -139,25 +144,33 @@ public class PayWalletServiceImpl implements PayWalletService { | ||
| 139 | } | 144 | } |
| 140 | 145 | ||
| 141 | @Override | 146 | @Override |
| 142 | - public PayWalletTransactionDO addWalletBalance(Long userId, Integer userType, | ||
| 143 | - Long bizId, PayWalletBizTypeEnum bizType, Integer price) { | 147 | + public PayWalletTransactionDO addWalletBalance(Long walletId, |
| 148 | + String bizId, PayWalletBizTypeEnum bizType, Integer price) { | ||
| 144 | // 1. 获取钱包 | 149 | // 1. 获取钱包 |
| 145 | - PayWalletDO payWallet = getOrCreateWallet(userId, userType); | 150 | + PayWalletDO payWallet = getWallet(walletId); |
| 151 | + | ||
| 152 | + if (payWallet == null) { | ||
| 153 | + log.error("[addWalletBalance],用户钱包({})不存在.", walletId); | ||
| 154 | + throw exception(WALLET_NOT_FOUND); | ||
| 155 | + } | ||
| 156 | + | ||
| 146 | switch (bizType) { | 157 | switch (bizType) { |
| 147 | case PAYMENT_REFUND: { | 158 | case PAYMENT_REFUND: { |
| 148 | - // 更新退款 | 159 | + // 退款更新 |
| 149 | walletMapper.updateWhenConsumptionRefund(price, payWallet.getId()); | 160 | walletMapper.updateWhenConsumptionRefund(price, payWallet.getId()); |
| 150 | break; | 161 | break; |
| 151 | } | 162 | } |
| 152 | case RECHARGE: { | 163 | case RECHARGE: { |
| 153 | - //TODO | 164 | + // 充值更新 |
| 165 | + walletMapper.updateWhenRecharge(price, payWallet.getId()); | ||
| 154 | break; | 166 | break; |
| 155 | } | 167 | } |
| 168 | + // TODO 其它类型 | ||
| 156 | } | 169 | } |
| 157 | 170 | ||
| 158 | // 2. 生成钱包流水 | 171 | // 2. 生成钱包流水 |
| 159 | CreateWalletTransactionBO bo = new CreateWalletTransactionBO().setWalletId(payWallet.getId()) | 172 | CreateWalletTransactionBO bo = new CreateWalletTransactionBO().setWalletId(payWallet.getId()) |
| 160 | - .setPrice(price).setBalance(payWallet.getBalance()+price).setBizId(String.valueOf(bizId)) | 173 | + .setPrice(price).setBalance(payWallet.getBalance()+price).setBizId(bizId) |
| 161 | .setBizType(bizType.getType()).setTitle(bizType.getDescription()); | 174 | .setBizType(bizType.getType()).setTitle(bizType.getDescription()); |
| 162 | return walletTransactionService.createWalletTransaction(bo); | 175 | return walletTransactionService.createWalletTransaction(bo); |
| 163 | } | 176 | } |