WebhookController.java
3.55 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
package com.ecommerce.payment.controller;
import com.ecommerce.payment.service.PayPalService;
import com.ecommerce.payment.service.StripeService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/api/webhooks")
@RequiredArgsConstructor
public class WebhookController {
private final StripeService stripeService;
private final PayPalService payPalService;
private final ObjectMapper objectMapper;
@PostMapping("/stripe")
public ResponseEntity<Map<String, String>> handleStripeWebhook(
@RequestBody String payload,
@RequestHeader("Stripe-Signature") String signature,
HttpServletRequest request) {
try {
// Verify webhook signature
if (!stripeService.verifyWebhookSignature(payload, signature)) {
log.warn("Invalid Stripe webhook signature");
return ResponseEntity.badRequest().body(Map.of("error", "Invalid signature"));
}
// Parse event
Map<String, Object> event = objectMapper.readValue(payload, Map.class);
// Process event
stripeService.handleWebhookEvent(event);
log.info("Stripe webhook processed successfully");
return ResponseEntity.ok(Map.of("status", "success"));
} catch (Exception e) {
log.error("Stripe webhook processing failed: {}", e.getMessage());
return ResponseEntity.badRequest().body(Map.of("error", "Webhook processing failed"));
}
}
@PostMapping("/paypal")
public ResponseEntity<Map<String, String>> handlePayPalWebhook(
@RequestBody String payload,
@RequestHeader("Paypal-Transmission-Id") String transmissionId,
@RequestHeader("Paypal-Cert-Url") String certUrl,
@RequestHeader("Paypal-Auth-Algo") String authAlgo,
@RequestHeader("Paypal-Transmission-Sig") String signature,
@RequestHeader("Paypal-Transmission-Time") String transmissionTime) {
try {
// Verify webhook signature
if (!payPalService.verifyWebhookSignature(payload, signature)) {
log.warn("Invalid PayPal webhook signature");
return ResponseEntity.badRequest().body(Map.of("error", "Invalid signature"));
}
// Parse event
Map<String, Object> event = objectMapper.readValue(payload, Map.class);
// Process event
payPalService.handleWebhookEvent(event);
log.info("PayPal webhook processed successfully");
return ResponseEntity.ok(Map.of("status", "success"));
} catch (Exception e) {
log.error("PayPal webhook processing failed: {}", e.getMessage());
return ResponseEntity.badRequest().body(Map.of("error", "Webhook processing failed"));
}
}
@GetMapping("/test")
public ResponseEntity<Map<String, String>> testWebhook() {
Map<String, String> response = new HashMap<>();
response.put("message", "Webhook endpoint is active");
response.put("timestamp", String.valueOf(System.currentTimeMillis()));
return ResponseEntity.ok(response);
}
}