-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathDataTransferEventHandler.java
More file actions
108 lines (92 loc) · 3.63 KB
/
DataTransferEventHandler.java
File metadata and controls
108 lines (92 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.loopers.application.event;
import com.loopers.domain.order.Order;
import com.loopers.domain.order.OrderService;
import com.loopers.domain.payment.Payment;
import com.loopers.domain.payment.PaymentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;
import org.springframework.transaction.event.TransactionPhase;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Slf4j
@RequiredArgsConstructor
@Component
public class DataTransferEventHandler {
private final OrderService orderService;
private final PaymentService paymentService;
private final ApplicationEventPublisher eventPublisher;
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void handlePaymentSuccess(PaymentSuccessEvent event) {
log.debug("Handling PaymentSuccessEvent for data transfer: orderId={}", event.orderId());
Payment payment = paymentService.findPaymentByOrderId(event.orderId());
// 결제 성공 데이터 전송
eventPublisher.publishEvent(new PaymentDataTransferEvent(
event.orderId(),
event.userId(),
event.amount(),
payment.getCardType(),
payment.getStatus(),
com.loopers.application.payment.TransactionStatus.SUCCESS,
event.reason(),
LocalDateTime.now(),
"PAYMENT_SUCCESS"
));
// 주문 결제 완료 데이터 전송 (이벤트 데이터 활용)
eventPublisher.publishEvent(new OrderDataTransferEvent(
event.orderId(),
event.userId(),
null, // order.getStatus() 대신 OrderCompletedEvent에서 처리
event.totalOrderAmount(),
LocalDateTime.now(),
"ORDER_PAID"
));
}
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void handlePaymentFailure(PaymentFailureEvent event) {
log.debug("Handling PaymentFailureEvent for data transfer: orderId={}", event.orderId());
Payment payment = paymentService.findPaymentByOrderId(event.orderId());
// 결제 실패 데이터 전송 (이벤트 데이터 활용)
eventPublisher.publishEvent(new PaymentDataTransferEvent(
event.orderId(),
event.userId(),
event.amount(),
payment.getCardType(),
payment.getStatus(),
com.loopers.application.payment.TransactionStatus.FAILED,
event.reason(),
LocalDateTime.now(),
"PAYMENT_FAILED"
));
}
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void handleOrderCreated(OrderCreatedEvent event) {
log.debug("Handling OrderCreatedEvent for data transfer: orderId={}", event.orderId());
Order order = orderService.getOrder(event.orderId());
// 주문 생성 데이터 전송
eventPublisher.publishEvent(new OrderDataTransferEvent(
event.orderId(),
event.userId(),
order.getStatus(),
order.getTotalPrice(),
LocalDateTime.now(),
"ORDER_CREATED"
));
}
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void handleOrderCancelled(OrderCancelledEvent event) {
log.debug("Handling OrderCancelledEvent for data transfer: orderId={}", event.orderId());
Order order = orderService.getOrder(event.orderId());
// 주문 취소 데이터 전송
eventPublisher.publishEvent(new OrderDataTransferEvent(
event.orderId(),
event.userId(),
order.getStatus(),
order.getTotalPrice(),
LocalDateTime.now(),
"ORDER_CANCELLED"
));
}
}