优化支付订单成功逻辑
This commit is contained in:
1 parent
cef85b81ef
commit
d7bcbb4e1e
4 files changed
+134
-7
No files matched your search
+46
-3
@@ -39,7 +39,7 @@ import java.util.regex.Pattern;
|
||||
/** 商户开放 API 创建支付订单业务。 */
|
||||
@Service
|
||||
public class PaymentOrderOpenApiService {
|
||||
private static final long ORDER_TTL_MILLIS = 30L * 60L * 1000L;
|
||||
private static final long ORDER_TTL_MILLIS = 10L * 60L * 1000L;
|
||||
private static final Pattern PARAMETER_PATTERN = Pattern.compile(
|
||||
"(?:^|&)(amount|notify_url|order_id|redirect_url|walletUserName)=");
|
||||
private static final List<String> REQUIRED_PARAMETERS = Arrays.asList(
|
||||
@@ -93,8 +93,7 @@ public class PaymentOrderOpenApiService {
|
||||
String slotKey = slotKey(request.getAppId(), wallet.getToken(), actualAmount);
|
||||
UsdtOrdersPlus previousOrder = expirePreviousSlotOrder(
|
||||
request.getAppId(), wallet.getToken(), actualAmount, slotKey, now);
|
||||
if (!Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(
|
||||
slotKey, tradeId, ORDER_TTL_MILLIS, TimeUnit.MILLISECONDS))) {
|
||||
if (!acquireSlot(slotKey, tradeId, request.getAppId(), wallet.getToken(), actualAmount)) {
|
||||
throw new CustomException("该收款地址和金额已有待支付订单,请稍后重试");
|
||||
}
|
||||
restoreSlotAfterRollback(slotKey, tradeId, previousOrder);
|
||||
@@ -168,6 +167,50 @@ public class PaymentOrderOpenApiService {
|
||||
redisTemplate.execute(RELEASE_SCRIPT, Collections.singletonList(slotKey), tradeId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取金额槽位。若 Redis 中的 owner 已经不是待支付订单,则说明支付成功、过期处理或
|
||||
* 异常退出时遗留了旧键,可按 owner 原子删除后重试一次;仍有待支付订单时禁止抢占。
|
||||
*/
|
||||
private boolean acquireSlot(String slotKey, String tradeId, Long appId,
|
||||
String token, BigDecimal amount) {
|
||||
if (Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(
|
||||
slotKey, tradeId, ORDER_TTL_MILLIS, TimeUnit.MILLISECONDS))) {
|
||||
return true;
|
||||
}
|
||||
String ownerTradeId = redisTemplate.opsForValue().get(slotKey);
|
||||
if (ownerTradeId == null) {
|
||||
return Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(
|
||||
slotKey, tradeId, ORDER_TTL_MILLIS, TimeUnit.MILLISECONDS));
|
||||
}
|
||||
if (hasPendingSlotOwner(ownerTradeId, appId, token, amount)) {
|
||||
return false;
|
||||
}
|
||||
releaseSlot(slotKey, ownerTradeId);
|
||||
return Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(
|
||||
slotKey, tradeId, ORDER_TTL_MILLIS, TimeUnit.MILLISECONDS));
|
||||
}
|
||||
|
||||
private boolean hasPendingSlotOwner(String tradeId, Long appId,
|
||||
String token, BigDecimal amount) {
|
||||
return ordersMapper.selectCount(new LambdaQueryWrapper<UsdtOrdersPlus>()
|
||||
.eq(UsdtOrdersPlus::getTradeId, tradeId)
|
||||
.eq(UsdtOrdersPlus::getAppId, appId)
|
||||
.eq(UsdtOrdersPlus::getToken, token)
|
||||
.eq(UsdtOrdersPlus::getActualAmount, amount)
|
||||
.eq(UsdtOrdersPlus::getStatus, 1L)
|
||||
.isNull(UsdtOrdersPlus::getDeletedAt)) > 0;
|
||||
}
|
||||
|
||||
/** 支付事务提交成功后释放对应金额槽位,owner 不匹配时不会删除其他订单的锁。 */
|
||||
public void releasePaidOrderSlot(UsdtOrdersPlus order) {
|
||||
if (order == null || order.getAppId() == null || order.getToken() == null
|
||||
|| order.getActualAmount() == null || order.getTradeId() == null) {
|
||||
return;
|
||||
}
|
||||
releaseSlot(slotKey(order.getAppId(), order.getToken(), order.getActualAmount()),
|
||||
order.getTradeId());
|
||||
}
|
||||
|
||||
private String slotKey(Long appId, String token, BigDecimal amount) {
|
||||
return "payment:slot:" + appId + ":" + token + ":" + amount.toPlainString();
|
||||
}
|
||||
|
||||
+25
-2
@@ -16,6 +16,8 @@ import com.ruoyi.system.mapper.UsdtConfigMapper;
|
||||
import com.ruoyi.system.mapper.WalletAddressLedgerMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
import java.util.Date;
|
||||
import java.math.BigDecimal;
|
||||
@@ -36,6 +38,7 @@ public class WalletLedgerPostingService {
|
||||
private final AppsMapper appsMapper;
|
||||
private final AppsBalanceLedgerMapper appsLedgerMapper;
|
||||
private final UsdtConfigMapper configMapper;
|
||||
private final PaymentOrderOpenApiService paymentOrderOpenApiService;
|
||||
|
||||
public WalletLedgerPostingService(UsdtOrdersMapper ordersMapper,
|
||||
WalletAddressLedgerMapper ledgerMapper,
|
||||
@@ -43,7 +46,8 @@ public class WalletLedgerPostingService {
|
||||
IUsdtAggregationTaskItemService aggregationItemService,
|
||||
AppsMapper appsMapper,
|
||||
AppsBalanceLedgerMapper appsLedgerMapper,
|
||||
UsdtConfigMapper configMapper) {
|
||||
UsdtConfigMapper configMapper,
|
||||
PaymentOrderOpenApiService paymentOrderOpenApiService) {
|
||||
this.ordersMapper = ordersMapper;
|
||||
this.ledgerMapper = ledgerMapper;
|
||||
this.walletService = walletService;
|
||||
@@ -51,6 +55,7 @@ public class WalletLedgerPostingService {
|
||||
this.appsMapper = appsMapper;
|
||||
this.appsLedgerMapper = appsLedgerMapper;
|
||||
this.configMapper = configMapper;
|
||||
this.paymentOrderOpenApiService = paymentOrderOpenApiService;
|
||||
}
|
||||
|
||||
/** 将待支付订单结算为成功,同一订单重复扫描不会重复产生流水。 */
|
||||
@@ -92,7 +97,25 @@ public class WalletLedgerPostingService {
|
||||
ledgerMapper.insertSystemLedgerIgnoreDuplicate(ledger);
|
||||
|
||||
postOrderToAppBalance(order, transactionId, now);
|
||||
return ordersMapper.markOrderPaid(order.getId(), transactionId, now) == 1;
|
||||
if (ordersMapper.markOrderPaid(order.getId(), transactionId, now) != 1) {
|
||||
return false;
|
||||
}
|
||||
releasePaymentSlotAfterCommit(order);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** 数据库确认支付事务提交后再释放 Redis 槽位,避免回滚时提前开放相同地址和金额。 */
|
||||
private void releasePaymentSlotAfterCommit(final UsdtOrdersPlus order) {
|
||||
if (!TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
paymentOrderOpenApiService.releasePaidOrderSlot(order);
|
||||
return;
|
||||
}
|
||||
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
||||
@Override
|
||||
public void afterCommit() {
|
||||
paymentOrderOpenApiService.releasePaidOrderSlot(order);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user