RabbitMQService.java
5.68 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.ecommerce.payment.service;
import com.ecommerce.payment.model.Payment;
import com.ecommerce.payment.model.Refund;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
@RequiredArgsConstructor
public class RabbitMQService {
private final RabbitTemplate rabbitTemplate;
private final ObjectMapper objectMapper;
public void sendPaymentSuccessEvent(Payment payment) {
try {
Map<String, Object> message = new HashMap<>();
message.put("eventType", "PAYMENT_SUCCESS");
message.put("paymentId", payment.getPaymentId());
message.put("orderId", payment.getOrderId());
message.put("orderNumber", payment.getOrderNumber());
message.put("userId", payment.getUserId());
message.put("amount", payment.getAmount());
message.put("currency", payment.getCurrency());
message.put("paymentMethod", payment.getPaymentMethod());
message.put("gatewayPaymentId", payment.getGatewayPaymentId());
message.put("timestamp", System.currentTimeMillis());
rabbitTemplate.convertAndSend("payment.exchange", "payment.success", message);
log.info("Payment success event sent: {}", payment.getPaymentId());
} catch (Exception e) {
log.error("Failed to send payment success event: {}", payment.getPaymentId(), e);
}
}
public void sendPaymentFailedEvent(Payment payment) {
try {
Map<String, Object> message = new HashMap<>();
message.put("eventType", "PAYMENT_FAILED");
message.put("paymentId", payment.getPaymentId());
message.put("orderId", payment.getOrderId());
message.put("orderNumber", payment.getOrderNumber());
message.put("userId", payment.getUserId());
message.put("amount", payment.getAmount());
message.put("failureReason", payment.getFailureReason());
message.put("failureCode", payment.getFailureCode());
message.put("timestamp", System.currentTimeMillis());
rabbitTemplate.convertAndSend("payment.exchange", "payment.failed", message);
log.info("Payment failed event sent: {}", payment.getPaymentId());
} catch (Exception e) {
log.error("Failed to send payment failed event: {}", payment.getPaymentId(), e);
}
}
public void sendPaymentCancelledEvent(Payment payment) {
try {
Map<String, Object> message = new HashMap<>();
message.put("eventType", "PAYMENT_CANCELLED");
message.put("paymentId", payment.getPaymentId());
message.put("orderId", payment.getOrderId());
message.put("orderNumber", payment.getOrderNumber());
message.put("userId", payment.getUserId());
message.put("amount", payment.getAmount());
message.put("timestamp", System.currentTimeMillis());
rabbitTemplate.convertAndSend("payment.exchange", "payment.cancelled", message);
log.info("Payment cancelled event sent: {}", payment.getPaymentId());
} catch (Exception e) {
log.error("Failed to send payment cancelled event: {}", payment.getPaymentId(), e);
}
}
public void sendRefundSuccessEvent(Refund refund) {
try {
Payment payment = refund.getPayment();
Map<String, Object> message = new HashMap<>();
message.put("eventType", "REFUND_SUCCESS");
message.put("refundId", refund.getRefundId());
message.put("paymentId", payment.getPaymentId());
message.put("orderId", payment.getOrderId());
message.put("orderNumber", payment.getOrderNumber());
message.put("userId", payment.getUserId());
message.put("amount", refund.getAmount());
message.put("currency", refund.getCurrency());
message.put("reason", refund.getReason());
message.put("gatewayRefundId", refund.getGatewayRefundId());
message.put("timestamp", System.currentTimeMillis());
rabbitTemplate.convertAndSend("payment.exchange", "refund.success", message);
log.info("Refund success event sent: {}", refund.getRefundId());
} catch (Exception e) {
log.error("Failed to send refund success event: {}", refund.getRefundId(), e);
}
}
public void sendRefundFailedEvent(Refund refund) {
try {
Payment payment = refund.getPayment();
Map<String, Object> message = new HashMap<>();
message.put("eventType", "REFUND_FAILED");
message.put("refundId", refund.getRefundId());
message.put("paymentId", payment.getPaymentId());
message.put("orderId", payment.getOrderId());
message.put("orderNumber", payment.getOrderNumber());
message.put("userId", payment.getUserId());
message.put("amount", refund.getAmount());
message.put("failureReason", refund.getFailureReason());
message.put("timestamp", System.currentTimeMillis());
rabbitTemplate.convertAndSend("payment.exchange", "refund.failed", message);
log.info("Refund failed event sent: {}", refund.getRefundId());
} catch (Exception e) {
log.error("Failed to send refund failed event: {}", refund.getRefundId(), e);
}
}
}