Skip to content

Commit c390664

Browse files
yeojunyeojun
authored andcommitted
feat(#306):환불최종실패에 대한 로직 추가"
1 parent f04e66c commit c390664

7 files changed

Lines changed: 45 additions & 7 deletions

File tree

spot-order/src/main/java/com/example/Spot/order/application/service/OrderServiceImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.example.Spot.order.domain.enums.CancelledBy;
3131
import com.example.Spot.order.domain.enums.OrderStatus;
3232
import com.example.Spot.order.domain.exception.DuplicateOrderException;
33+
import com.example.Spot.order.domain.exception.InvalidOrderStatusTransitionException;
3334
import com.example.Spot.order.domain.repository.OrderItemOptionRepository;
3435
import com.example.Spot.order.domain.repository.OrderRepository;
3536
import com.example.Spot.order.infrastructure.aop.OrderStatusChange;
@@ -300,6 +301,10 @@ public OrderResponseDto acceptOrder(UUID orderId, Integer estimatedTime) {
300301
public OrderResponseDto rejectOrder(UUID orderId, String reason) {
301302

302303
OrderEntity order = OrderValidationContext.getCurrentOrder();
304+
if (order.getOrderStatus() != OrderStatus.PENDING) {
305+
throw new InvalidOrderStatusTransitionException(order.getOrderStatus(), OrderStatus.CANCEL_PENDING);
306+
}
307+
303308
order.initiateCancel(reason, null);
304309
orderEventProducer.reserveOrderCancelled(order.getId(), reason);
305310
sendSignalToWorkflow(orderId, OrderStatus.CANCEL_PENDING);

spot-order/src/main/java/com/example/Spot/order/domain/entity/OrderEntity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ public void finalizeCancel() {
226226
}
227227
}
228228

229+
public void markAsRefundError() {
230+
if (this.orderStatus != OrderStatus.CANCEL_PENDING) {
231+
throw new IllegalStateException("환불 대기 상태가 아닌 주문은 에러 처리를 할 수 없습니다.");
232+
}
233+
234+
this.orderStatus = OrderStatus.REFUND_ERROR;
235+
}
236+
229237
// 상태 전환 검증
230238
private void validateStatusTransition(OrderStatus newStatus) {
231239
if (!this.orderStatus.canTransitionTo(newStatus)) {

spot-order/src/main/java/com/example/Spot/order/domain/enums/OrderStatus.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public enum OrderStatus {
1010
READY("픽업 대기"),
1111
COMPLETED("픽업 완료"),
1212
CANCEL_PENDING("취소 처리 중"),
13-
CANCELLED("주문 취소");
13+
CANCELLED("주문 취소"),
14+
REFUND_ERROR("환불 확인 필요");
1415

1516
private final String description;
1617

@@ -23,7 +24,7 @@ public String getDescription() {
2324
}
2425

2526
public boolean isFinalStatus() {
26-
return this == COMPLETED || this == CANCELLED || this == REJECTED || this == PAYMENT_FAILED;
27+
return this == COMPLETED || this == CANCELLED || this == REJECTED || this == PAYMENT_FAILED || this == REFUND_ERROR;
2728
}
2829

2930
public boolean isPaid() {
@@ -39,7 +40,7 @@ public boolean canTransitionTo(OrderStatus newStatus) {
3940
case PENDING -> newStatus == ACCEPTED || newStatus == CANCEL_PENDING;
4041
case ACCEPTED -> newStatus == COOKING || newStatus == CANCEL_PENDING;
4142
case COOKING -> newStatus == READY || newStatus == CANCEL_PENDING;
42-
case CANCEL_PENDING -> newStatus == CANCELLED || newStatus == REJECTED;
43+
case CANCEL_PENDING -> newStatus == CANCELLED || newStatus == REJECTED || newStatus == REFUND_ERROR;
4344
case READY -> newStatus == COMPLETED;
4445
default -> false;
4546
};

spot-order/src/main/java/com/example/Spot/order/infrastructure/temporal/activity/OrderActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ public interface OrderActivity {
2121

2222
@ActivityMethod
2323
void finalizeOrder(UUID orderId);
24+
25+
@ActivityMethod
26+
void handleRefundTimeout(UUID orderId);
2427
}

spot-order/src/main/java/com/example/Spot/order/infrastructure/temporal/activity/OrderActivityImpl.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
import org.springframework.transaction.annotation.Transactional;
77

88
import com.example.Spot.order.domain.entity.OrderEntity;
9+
import com.example.Spot.order.domain.enums.CancelledBy;
910
import com.example.Spot.order.domain.enums.OrderStatus;
1011
import com.example.Spot.order.domain.repository.OrderRepository;
1112
import com.example.Spot.order.infrastructure.producer.OrderEventProducer;
1213
import com.example.Spot.order.infrastructure.temporal.config.OrderConstants;
1314

1415
import io.temporal.spring.boot.ActivityImpl;
1516
import lombok.RequiredArgsConstructor;
17+
import lombok.extern.slf4j.Slf4j;
1618

19+
@Slf4j
1720
@Component
1821
@RequiredArgsConstructor
1922
@ActivityImpl(taskQueues = OrderConstants.ORDER_TASK_QUEUE)
@@ -46,7 +49,7 @@ public void cancelOrder(UUID orderId, String reason) {
4649

4750
if (order.getOrderStatus() != OrderStatus.CANCELLED &&
4851
order.getOrderStatus() != OrderStatus.REJECTED) {
49-
order.initiateCancel(reason, null);
52+
order.initiateCancel(reason, CancelledBy.SYSTEM);
5053
orderEventProducer.reserveOrderCancelled(order.getId(), reason);
5154
}
5255
}
@@ -60,4 +63,16 @@ public void finalizeOrder(UUID orderId) {
6063
order.finalizeCancel();
6164
}
6265

66+
@Override
67+
@Transactional
68+
public void handleRefundTimeout(UUID orderId) {
69+
OrderEntity order = orderRepository.findByIdWithLock(orderId)
70+
.orElseThrow(() -> new IllegalArgumentException("주문 없음: " + orderId));
71+
72+
order.markAsRefundError();
73+
log.error("[환불 타임아웃 발생] 관찰 필요 - OrderID: {}, 현재상태: {}",
74+
orderId, order.getOrderStatus());
75+
orderRepository.save(order);
76+
}
77+
6378
}

spot-order/src/main/java/com/example/Spot/order/infrastructure/temporal/workflow/OrderWorkflowImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ private boolean handleCancelIfNecessary(UUID orderId, OrderActivity activities)
7272
}
7373

7474
private void waitForRefundAndFinalize(UUID orderId, OrderActivity activities) {
75-
Workflow.await(() -> isRefundCompleted);
76-
activities.finalizeOrder(orderId);
75+
boolean isSuccess = Workflow.await(Duration.ofMinutes(30), () -> isRefundCompleted);
76+
if (isSuccess) {
77+
activities.finalizeOrder(orderId);
78+
} else {
79+
activities.handleRefundTimeout(orderId);
80+
}
7781
}
7882

7983
@Override

spot-payment/src/main/java/com/example/Spot/payments/infrastructure/temporal/workflow/PaymentCancelWorkflowImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public class PaymentCancelWorkflowImpl implements PaymentCancelWorkflow {
2929
.setStartToCloseTimeout(Duration.ofMinutes(1))
3030
.setRetryOptions(RetryOptions.newBuilder()
3131
.setInitialInterval(Duration.ofSeconds(5))
32-
.setMaximumAttempts(8)
32+
.setBackoffCoefficient(2.0)
33+
.setMaximumInterval(Duration.ofMinutes(3))
34+
.setMaximumAttempts(15)
3335
.setDoNotRetry(DO_NOT_RETRY_EXCEPTIONS)
3436
.build())
3537
.build());

0 commit comments

Comments
 (0)