|
|
@@ -2,22 +2,24 @@ package com.izouma.nineth.service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.izouma.nineth.config.GeneralProperties;
|
|
|
-import com.izouma.nineth.domain.GiftOrder;
|
|
|
-import com.izouma.nineth.domain.MintOrder;
|
|
|
-import com.izouma.nineth.domain.Order;
|
|
|
+import com.izouma.nineth.domain.*;
|
|
|
import com.izouma.nineth.enums.MintOrderStatus;
|
|
|
import com.izouma.nineth.enums.OrderStatus;
|
|
|
+import com.izouma.nineth.enums.PayMethod;
|
|
|
+import com.izouma.nineth.event.OrderNotifyEvent;
|
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
|
-import com.izouma.nineth.repo.GiftOrderRepo;
|
|
|
-import com.izouma.nineth.repo.MintOrderRepo;
|
|
|
-import com.izouma.nineth.repo.OrderRepo;
|
|
|
+import com.izouma.nineth.repo.*;
|
|
|
import com.izouma.nineth.utils.DateTimeUtils;
|
|
|
+import com.izouma.nineth.utils.SnowflakeIdWorker;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
@Service
|
|
|
@@ -26,12 +28,20 @@ import java.util.Optional;
|
|
|
public class OrderPayService {
|
|
|
private static String PAY_CHANNEL = "sandPay";
|
|
|
|
|
|
- private final OrderRepo orderRepo;
|
|
|
- private final MintOrderRepo mintOrderRepo;
|
|
|
- private final GiftOrderRepo giftOrderRepo;
|
|
|
- private final SandPayService sandPayService;
|
|
|
- private final HMPayService hmPayService;
|
|
|
- private final GeneralProperties generalProperties;
|
|
|
+ private final OrderRepo orderRepo;
|
|
|
+ private final MintOrderRepo mintOrderRepo;
|
|
|
+ private final GiftOrderRepo giftOrderRepo;
|
|
|
+ private final SandPayService sandPayService;
|
|
|
+ private final HMPayService hmPayService;
|
|
|
+ private final GeneralProperties generalProperties;
|
|
|
+ private final UserBalanceService userBalanceService;
|
|
|
+ private final RocketMQTemplate rocketMQTemplate;
|
|
|
+ private final GiftOrderService giftOrderService;
|
|
|
+ private final MintOrderService mintOrderService;
|
|
|
+ private final UserRepo userRepo;
|
|
|
+ private final SnowflakeIdWorker snowflakeIdWorker;
|
|
|
+ private final RechargeOrderRepo rechargeOrderRepo;
|
|
|
+ private final SysConfigService sysConfigService;
|
|
|
|
|
|
public static void setPayChannel(String payChannel) {
|
|
|
if ("hmPay".equals(payChannel) || "sandPay".equals(payChannel)) {
|
|
|
@@ -58,6 +68,22 @@ public class OrderPayService {
|
|
|
throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
|
|
|
}
|
|
|
|
|
|
+ public void payOrderBalance(Long orderId, Long userId, String tradeCode) {
|
|
|
+ Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ if (order.getStatus() != OrderStatus.NOT_PAID) {
|
|
|
+ throw new BusinessException("订单状态错误");
|
|
|
+ }
|
|
|
+ if (!Objects.equals(order.getUserId(), userId)) {
|
|
|
+ throw new BusinessException("订单不属于该用户");
|
|
|
+ }
|
|
|
+ if (!Objects.equals(userRepo.findTradeCode(userId), tradeCode)) {
|
|
|
+ throw new BusinessException("交易码错误");
|
|
|
+ }
|
|
|
+ BalanceRecord record = userBalanceService.balancePay(order.getUserId(), order.getTotalPrice(), orderId, order.getName());
|
|
|
+ rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
|
|
|
+ new OrderNotifyEvent(orderId, PayMethod.BALANCE, record.getId().toString(), System.currentTimeMillis()));
|
|
|
+ }
|
|
|
+
|
|
|
@Cacheable(value = "payOrder", key = "'gift#'+#orderId")
|
|
|
public String payGiftOrder(Long orderId) {
|
|
|
GiftOrder order = giftOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
@@ -79,6 +105,21 @@ public class OrderPayService {
|
|
|
throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
|
|
|
}
|
|
|
|
|
|
+ public void payGiftBalance(Long orderId, Long userId, String tradeCode) {
|
|
|
+ GiftOrder order = giftOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ if (order.getStatus() != OrderStatus.NOT_PAID) {
|
|
|
+ throw new BusinessException("订单状态错误");
|
|
|
+ }
|
|
|
+ if (!Objects.equals(order.getUserId(), userId)) {
|
|
|
+ throw new BusinessException("订单不属于该用户");
|
|
|
+ }
|
|
|
+ if (!Objects.equals(userRepo.findTradeCode(userId), tradeCode)) {
|
|
|
+ throw new BusinessException("交易码错误");
|
|
|
+ }
|
|
|
+ BalanceRecord record = userBalanceService.balancePay(order.getUserId(), order.getGasPrice(), orderId, "转赠");
|
|
|
+ giftOrderService.giftNotify(orderId, PayMethod.BALANCE, record.getId().toString());
|
|
|
+ }
|
|
|
+
|
|
|
@Cacheable(value = "payOrder", key = "'mintOrder#'+#orderId")
|
|
|
public String payMintOrder(Long orderId) {
|
|
|
MintOrder order = mintOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
@@ -99,4 +140,46 @@ public class OrderPayService {
|
|
|
}
|
|
|
throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
|
|
|
}
|
|
|
+
|
|
|
+ public void payMintOrderBalance(Long orderId, Long userId, String tradeCode) {
|
|
|
+ MintOrder order = mintOrderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ if (order.getStatus() != MintOrderStatus.NOT_PAID) {
|
|
|
+ throw new BusinessException("订单状态错误");
|
|
|
+ }
|
|
|
+ if (!Objects.equals(order.getUserId(), userId)) {
|
|
|
+ throw new BusinessException("订单不属于该用户");
|
|
|
+ }
|
|
|
+ if (!Objects.equals(userRepo.findTradeCode(userId), tradeCode)) {
|
|
|
+ throw new BusinessException("交易码错误");
|
|
|
+ }
|
|
|
+ BalanceRecord record = userBalanceService.balancePay(order.getUserId(), order.getGasPrice(), orderId, "铸造活动");
|
|
|
+ giftOrderService.giftNotify(orderId, PayMethod.BALANCE, record.getId().toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ public String recharge(Long userId, BigDecimal amount) {
|
|
|
+ BigDecimal minAmount = sysConfigService.getBigDecimal("min_recharge_amount");
|
|
|
+ if (amount.compareTo(minAmount) < 0) {
|
|
|
+ throw new BusinessException("充值金额不能小于" + minAmount);
|
|
|
+ }
|
|
|
+ RechargeOrder order = RechargeOrder.builder()
|
|
|
+ .id(snowflakeIdWorker.nextId())
|
|
|
+ .userId(userId)
|
|
|
+ .amount(amount)
|
|
|
+ .status(OrderStatus.NOT_PAID)
|
|
|
+ .build();
|
|
|
+ rechargeOrderRepo.save(order);
|
|
|
+ switch (PAY_CHANNEL) {
|
|
|
+ case "sandPay":
|
|
|
+ return sandPayService.requestAlipay(order.getId() + "", order.getAmount(),
|
|
|
+ "余额充值", "余额充值",
|
|
|
+ SandPayService.getTimeout(order.getCreatedAt(), 180),
|
|
|
+ "{\"type\":\"recharge\",\"id\":\"" + order.getId() + "\"}");
|
|
|
+ case "hmPay":
|
|
|
+ return hmPayService.requestAlipay(order.getId() + "", order.getAmount(),
|
|
|
+ "余额充值",
|
|
|
+ HMPayService.getTimeout(order.getCreatedAt(), 180),
|
|
|
+ "recharge", generalProperties.getHost() + "/9th/home");
|
|
|
+ }
|
|
|
+ throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
|
|
|
+ }
|
|
|
}
|