优化支付订单成功逻辑
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();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user