|
|
@@ -1108,8 +1108,7 @@ public class PayScoreServiceImpl implements PayScoreService {
|
|
|
* 生产环境下回调正常运转后,此方法只在用户多次快速扫码(测试/异常)时触发
|
|
|
*/
|
|
|
private void cancelStalePayScoreOrders(Long userId, String deviceId) {
|
|
|
- // 1. 只取消 CREATED 状态的孤儿订单(创建了但用户从未授权使用)
|
|
|
- // DOING/USER_PAYING 是正常的进行中状态,不能取消
|
|
|
+ // 1. 取消 CREATED 状态的孤儿订单(创建了但用户从未授权使用)
|
|
|
try {
|
|
|
List<Order> inProgressOrders = orderService.lambdaQuery()
|
|
|
.eq(Order::getUserId, userId)
|
|
|
@@ -1119,7 +1118,7 @@ public class PayScoreServiceImpl implements PayScoreService {
|
|
|
.list();
|
|
|
|
|
|
if (inProgressOrders != null && !inProgressOrders.isEmpty()) {
|
|
|
- log.info("[支付分服务] 清理用户 {} 的 {} 笔DB进行中订单", userId, inProgressOrders.size());
|
|
|
+ log.info("[支付分服务] 清理用户 {} 的 {} 笔CREATED状态订单", userId, inProgressOrders.size());
|
|
|
for (Order order : inProgressOrders) {
|
|
|
cancelPayScoreOrderByOutOrderNo(order.getPayScoreOrderId(), order.getId());
|
|
|
}
|
|
|
@@ -1128,6 +1127,29 @@ public class PayScoreServiceImpl implements PayScoreService {
|
|
|
log.error("[支付分服务] 清理DB进行中订单失败 - userId: {}", userId, e);
|
|
|
}
|
|
|
|
|
|
+ // 1.5 取消 DOING/USER_PAYING 状态但金额为空的孤儿订单
|
|
|
+ // 这类订单由回调签名失败等异常造成(用户已确认但金额未写入),新预授权创建时必须清理
|
|
|
+ try {
|
|
|
+ List<Order> stuckOrders = orderService.lambdaQuery()
|
|
|
+ .eq(Order::getUserId, userId)
|
|
|
+ .eq(Order::getPayChannel, PaymentChannel.WECHAT_PAYSCORE.getCode())
|
|
|
+ .in(Order::getPayScoreState, Arrays.asList(
|
|
|
+ PayScoreState.DOING.getCode(),
|
|
|
+ PayScoreState.USER_PAYING.getCode()))
|
|
|
+ .isNotNull(Order::getPayScoreOrderId)
|
|
|
+ .and(w -> w.isNull(Order::getTotalAmount).or().eq(Order::getTotalAmount, BigDecimal.ZERO))
|
|
|
+ .list();
|
|
|
+
|
|
|
+ if (stuckOrders != null && !stuckOrders.isEmpty()) {
|
|
|
+ log.info("[支付分服务] 清理用户 {} 的 {} 笔已确认但金额为空订单", userId, stuckOrders.size());
|
|
|
+ for (Order order : stuckOrders) {
|
|
|
+ cancelPayScoreOrderByOutOrderNo(order.getPayScoreOrderId(), order.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("[支付分服务] 清理金额为空订单失败 - userId: {}", userId, e);
|
|
|
+ }
|
|
|
+
|
|
|
// 2. 取消 Redis 追踪的预创建订单(之前未完成购物流程留下的)
|
|
|
// 必须检查关联的本地订单状态,避免误取消已确认/正在扣款的订单
|
|
|
if (stringRedisTemplate != null) {
|