PayPalWebhookHandler.java
2.24 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
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 PayPalWebhookHandler {
private final PaymentRepository paymentRepository;
private final RabbitMQService rabbitMQService;
public void handlePaymentCaptureCompleted(Map<String, Object> resource) {
String gatewayPaymentId = (String) resource.get("id");
String status = (String) resource.get("status");
log.info("Processing PayPal payment completed: {}", 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 handlePaymentCaptureDenied(Map<String, Object> resource) {
String gatewayPaymentId = (String) resource.get("id");
log.info("Processing PayPal payment denied: {}", gatewayPaymentId);
paymentRepository.findByGatewayPaymentId(gatewayPaymentId).ifPresent(payment -> {
payment.setStatus("FAILED");
payment.setFailureReason("Payment denied by PayPal");
paymentRepository.save(payment);
rabbitMQService.sendPaymentFailedEvent(payment);
log.info("Payment updated to FAILED: {}", payment.getPaymentId());
});
}
public void handlePaymentCaptureRefunded(Map<String, Object> resource) {
String gatewayCaptureId = (String) resource.get("id");
log.info("Processing PayPal refund completed: {}", gatewayCaptureId);
// In a real implementation, you would update the refund status
// based on the capture refunded event
}
}