优化支付订单成功逻辑

This commit is contained in:
lu7213 committed 2026-10-14 03:38:27 -04:00
1 parent cef85b81ef
commit d7bcbb4e1e
4 files changed
+134 -7

No files matched your search

@@ -7,8 +7,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.system.domain.UsdtOrdersPlus;
import com.ruoyi.system.service.ITronService;
import com.ruoyi.system.service.IUsdtOrdersService;
import com.ruoyi.system.service.WalletLedgerPostingService;
import com.ruoyi.system.service.TronServiceProvider;
import com.ruoyi.system.service.WalletLedgerPostingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -18,6 +18,7 @@ import org.springframework.web.client.RestTemplate;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
/**
* 轮询已确认 TRC20 转账,按地址、六位金额和唯一交易哈希入账。
@@ -25,6 +26,9 @@ import java.math.RoundingMode;
@Component
public class TronPaymentTask {
private static final Logger log = LoggerFactory.getLogger(TronPaymentTask.class);
private static final long ORDER_TTL_MILLIS = 10L * 60L * 1000L;
/** 仅放宽订单开始时间,兼容应用服务器与 TRON 节点之间的轻微时钟偏差。 */
private static final long CLOCK_SKEW_MILLIS = 5L * 1000L;
@Autowired
private IUsdtOrdersService orders;
@@ -34,7 +38,7 @@ public class TronPaymentTask {
private WalletLedgerPostingService ledgerPostingService;
private final RestTemplate http = new RestTemplate();
@Scheduled(fixedDelay = 30000L)
@Scheduled(fixedDelay = 10000L)
public void scan() {
ITronService tron;
try {
@@ -53,6 +57,8 @@ public class TronPaymentTask {
for (int i = 0; i < data.size(); i++) {
JSONObject tx = data.getJSONObject(i);
if (!order.getToken().equals(tx.getString("to"))) continue;
long transactionTime = tx.getLongValue("block_timestamp");
if (!isTransactionInPaymentWindow(order.getCreatedAt(), transactionTime)) continue;
BigDecimal value = new BigDecimal(tx.getString("value")).movePointLeft(6).setScale(6, RoundingMode.DOWN);
if (value.compareTo(order.getActualAmount().setScale(6, RoundingMode.DOWN)) != 0) continue;
String hash = tx.getString("transaction_id");
@@ -63,9 +69,24 @@ public class TronPaymentTask {
}
} catch (Exception e) {
// 单笔订单查询失败不能中断其他订单,日志中禁止输出凭证和钱包私钥。
log.error(e.getMessage(), e);
log.warn("查询 TRON 支付交易失败,订单ID={},网络={}", order.getId(), tron.type());
}
}
}
/**
* 链上交易必须发生在订单有效期内,防止同地址、同金额的历史转账命中新订单。
* 截止时间不增加容差,避免订单过期后的付款被自动确认。
*/
static boolean isTransactionInPaymentWindow(Date orderCreatedAt, long transactionTime) {
if (orderCreatedAt == null || transactionTime <= 0L) {
return false;
}
long createdAt = orderCreatedAt.getTime();
long validFrom = createdAt - CLOCK_SKEW_MILLIS;
long validUntil = createdAt + ORDER_TTL_MILLIS;
return transactionTime >= validFrom && transactionTime <= validUntil;
}
}