package com.ruoyi.task; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; 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.TronServiceProvider; import com.ruoyi.system.service.TronChainDataClient; import com.ruoyi.system.service.WalletLedgerPostingService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Date; /** * 轮询已确认 TRC20 转账,按地址、六位金额和唯一交易哈希入账。 */ @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; @Autowired private TronServiceProvider tronServiceProvider; @Autowired private WalletLedgerPostingService ledgerPostingService; @Autowired private TronChainDataClient chainDataClient; @Scheduled(fixedDelay = 10000L) @com.ruoyi.quartz.annotation.AllowedJobMethod public void scan() { ITronService tron = null; try { tron = tronServiceProvider.getCurrentService(); } catch (RuntimeException e) { log.warn("无法读取 TRON 全局网络配置,仅跳过未保存网络的历史订单扫描:{}", e.getMessage()); // 新订单自带网络,不因旧全局配置缺失阻断新支付订单。 } for (UsdtOrdersPlus order : orders.list(new LambdaQueryWrapper() .eq(UsdtOrdersPlus::getStatus, 1) .and(wrapper -> wrapper.isNull(UsdtOrdersPlus::getBlockTransactionId) .or().eq(UsdtOrdersPlus::getBlockTransactionId, "")))) { if (order.getToken() == null || order.getActualAmount() == null) continue; try { ITronService orderTron = order.getPaymentNetwork() == null ? tron : tronServiceProvider.getServiceForNetwork(order.getPaymentNetwork()); if (orderTron == null) continue; JSONArray data = chainDataClient.queryConfirmedTrc20Transactions( orderTron, order.getToken(), 200); 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"); if (hash == null || hash.trim().isEmpty()) continue; if (!ledgerPostingService.recordDetectedPayment(order.getId(), hash)) { log.warn("记录链上支付交易失败,订单ID={}", order.getId()); break; } try { if (ledgerPostingService.settlePaidOrder(order.getId(), hash)) { break; } } catch (RuntimeException settlementException) { log.error("支付订单账务结算失败,订单ID={},原因={}", order.getId(), settlementException.getMessage()); break; } } } catch (Exception e) { // 单笔订单查询失败不能中断其他订单,日志中禁止输出凭证和钱包私钥。 log.error(e.getMessage(), e); log.warn("查询 TRON 支付交易失败,订单ID={},网络={}", order.getId(), order.getPaymentNetwork()); } } } /** * 链上交易必须发生在订单有效期内,防止同地址、同金额的历史转账命中新订单。 * 截止时间不增加容差,避免订单过期后的付款被自动确认。 */ 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; } }