添加比率

This commit is contained in:
lu7213 committed 2026-10-10 11:36:20 -04:00
1 parent 5be292c009
commit c0868f9d8f
4 files changed
+62 -4

No files matched your search

@@ -12,6 +12,7 @@ import com.ruoyi.system.mapper.AppsMapper;
import com.ruoyi.system.mapper.UsdtOrdersMapper;
import com.ruoyi.system.mapper.WalletAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
@@ -20,6 +21,7 @@ import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
@@ -50,6 +52,9 @@ public class PaymentOrderOpenApiService {
@Autowired private WalletAddressMapper walletAddressMapper;
@Autowired private UsdtOrdersMapper ordersMapper;
@Autowired private StringRedisTemplate redisTemplate;
/** 1 USDT 对应的业务货币金额,例如 7.20 表示 1 USDT = 7.20 业务货币。 */
@Value("${payment.usdt-exchange-rate:1}")
private BigDecimal usdtExchangeRate;
@Transactional(rollbackFor = Exception.class)
public PaymentOrderCreateResult createOrder(DecryptResult request) {
@@ -59,6 +64,7 @@ public class PaymentOrderOpenApiService {
String notifyUrl = required(parameters, "notify_url");
String redirectUrl = trimToNull(parameters.get("redirect_url"));
BigDecimal amount = parseAmount(required(parameters, "amount"));
BigDecimal actualAmount = convertToUsdt(amount);
validate(orderId, walletUserName, notifyUrl, redirectUrl);
// 锁定应用,使同一应用下的钱包领取和商户订单幂等检查串行执行。
@@ -84,9 +90,9 @@ public class PaymentOrderOpenApiService {
Date now = DateUtils.getNowDate();
String tradeId = UUID.randomUUID().toString().replace("-", "");
String slotKey = slotKey(request.getAppId(), wallet.getToken(), amount);
String slotKey = slotKey(request.getAppId(), wallet.getToken(), actualAmount);
UsdtOrdersPlus previousOrder = expirePreviousSlotOrder(
request.getAppId(), wallet.getToken(), amount, slotKey, now);
request.getAppId(), wallet.getToken(), actualAmount, slotKey, now);
if (!Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(
slotKey, tradeId, ORDER_TTL_MILLIS, TimeUnit.MILLISECONDS))) {
throw new CustomException("该收款地址和金额已有待支付订单,请稍后重试");
@@ -97,7 +103,7 @@ public class PaymentOrderOpenApiService {
order.setTradeId(tradeId);
order.setOrderId(orderId);
order.setAmount(amount);
order.setActualAmount(amount);
order.setActualAmount(actualAmount);
order.setToken(wallet.getToken());
order.setStatus(1L);
order.setNotifyUrl(notifyUrl);
@@ -232,6 +238,28 @@ public class PaymentOrderOpenApiService {
}
}
/**
* 将商户业务金额转换为应支付 USDT。
* 按支付契约向下保留两位,避免四舍五入导致用户多支付。
*/
private BigDecimal convertToUsdt(BigDecimal amount) {
return calculateActualAmount(amount, usdtExchangeRate);
}
static BigDecimal calculateActualAmount(BigDecimal amount, BigDecimal exchangeRate) {
if (exchangeRate == null || exchangeRate.signum() <= 0) {
throw new CustomException("USDT 兑换汇率配置无效");
}
BigDecimal actualAmount = amount.divide(exchangeRate, 2, RoundingMode.DOWN);
if (actualAmount.signum() <= 0) {
throw new CustomException("amount 按当前汇率换算后不足 0.01 USDT");
}
if (actualAmount.precision() > 19) {
throw new CustomException("amount 按当前汇率换算后超出金额范围");
}
return actualAmount;
}
private String required(Map<String, String> values, String name) {
String value = trimToNull(values.get(name));
if (value == null) throw new CustomException(name + " 不能为空");