StripeWebhookHandler.java
2.49 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
package com.ecommerce.payment.webhook;
import com.ecommerce.payment.model.Payment;
import com.ecommerce.payment.repository.PaymentRepository;
import com.ecommerce.payment.service.RabbitMQService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Map;
@Slf4j
@Component
@RequiredArgsConstructor
public class StripeWebhookHandler {
private final PaymentRepository paymentRepository;
private final RabbitMQService rabbitMQService;
public void handlePaymentIntentSucceeded(Map<String, Object> paymentIntent) {
String gatewayPaymentId = (String) paymentIntent.get("id");
String status = (String) paymentIntent.get("status");
log.info("Processing Stripe payment succeeded: {}", gatewayPaymentId);
paymentRepository.findByGatewayPaymentId(gatewayPaymentId).ifPresent(payment -> {
payment.setStatus("SUCCEEDED");
payment.setProcessedAt(LocalDateTime.now());
paymentRepository.save(payment);
rabbitMQService.sendPaymentSuccessEvent(payment);
log.info("Payment updated to SUCCEEDED: {}", payment.getPaymentId());
});
}
public void handlePaymentIntentPaymentFailed(Map<String, Object> paymentIntent) {
String gatewayPaymentId = (String) paymentIntent.get("id");
Map<String, Object> lastError = (Map<String, Object>) paymentIntent.get("last_payment_error");
log.info("Processing Stripe payment failed: {}", gatewayPaymentId);
paymentRepository.findByGatewayPaymentId(gatewayPaymentId).ifPresent(payment -> {
payment.setStatus("FAILED");
if (lastError != null) {
payment.setFailureReason((String) lastError.get("message"));
payment.setFailureCode((String) lastError.get("code"));
}
paymentRepository.save(payment);
rabbitMQService.sendPaymentFailedEvent(payment);
log.info("Payment updated to FAILED: {}", payment.getPaymentId());
});
}
public void handleChargeRefunded(Map<String, Object> charge) {
String gatewayChargeId = (String) charge.get("id");
log.info("Processing Stripe refund completed: {}", gatewayChargeId);
// In a real implementation, you would update the refund status
// based on the charge refunded event
}
}