• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stripe / stripe-java / #16660

21 Nov 2024 09:08PM CUT coverage: 12.4%. Remained the same
#16660

push

github

ramya-stripe
Bump version to 28.2.0-beta.1

18860 of 152092 relevant lines covered (12.4%)

0.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/com/stripe/examples/ThinEventWebhookHandler.java
1
package com.stripe.examples;
2

3
import com.stripe.StripeClient;
4
import com.stripe.events.V1BillingMeterErrorReportTriggeredEvent;
5
import com.stripe.exception.StripeException;
6
import com.stripe.model.ThinEvent;
7
import com.stripe.model.billing.Meter;
8
import com.stripe.model.v2.Event;
9
import com.sun.net.httpserver.HttpExchange;
10
import com.sun.net.httpserver.HttpHandler;
11
import com.sun.net.httpserver.HttpServer;
12
import java.io.ByteArrayOutputStream;
13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.net.InetSocketAddress;
16
import java.nio.charset.StandardCharsets;
17

18
/**
19
 * Receive and process thin events like the v1.billing.meter.error_report_triggered event.
20
 *
21
 * <p>In this example, we:
22
 *
23
 * <ul>
24
 *   <li>use parseThinEvent to parse the received thin event webhook body
25
 *   <li>call StripeClient.v2.core.events.retrieve to retrieve the flil event object
26
 *   <li>if it is a V1BillingMeterErrorReportTriggeredEvent event type, call fetchRelatedObject to
27
 *       retrieve the Billing Meter object associated with the event.
28
 * </ul>
29
 */
30
public class ThinEventWebhookHandler {
×
31
  private static final String API_KEY = System.getenv("STRIPE_API_KEY");
×
32
  private static final String WEBHOOK_SECRET = System.getenv("WEBHOOK_SECRET");
×
33

34
  private static final StripeClient client = new StripeClient(API_KEY);
×
35

36
  public static void main(String[] args) throws IOException {
37

38
    HttpServer server = HttpServer.create(new InetSocketAddress(4242), 0);
×
39
    server.createContext("/webhook", new WebhookHandler());
×
40
    server.setExecutor(null);
×
41
    server.start();
×
42
  }
×
43

44
  static class WebhookHandler implements HttpHandler {
×
45

46
    // For Java 1.8 compatibility
47
    public static byte[] readAllBytes(InputStream inputStream) throws IOException {
48
      final int bufLen = 1024;
×
49
      byte[] buf = new byte[bufLen];
×
50
      int readLen;
51

52
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
×
53

54
      while ((readLen = inputStream.read(buf, 0, bufLen)) != -1)
×
55
        outputStream.write(buf, 0, readLen);
×
56

57
      return outputStream.toByteArray();
×
58
    }
59

60
    @Override
61
    public void handle(HttpExchange exchange) throws IOException {
62
      if ("POST".equals(exchange.getRequestMethod())) {
×
63
        InputStream requestBody = exchange.getRequestBody();
×
64
        String webhookBody = new String(readAllBytes(requestBody), StandardCharsets.UTF_8);
×
65
        String sigHeader = exchange.getRequestHeaders().getFirst("Stripe-Signature");
×
66

67
        try {
68
          ThinEvent thinEvent = client.parseThinEvent(webhookBody, sigHeader, WEBHOOK_SECRET);
×
69

70
          // Fetch the event data to understand the failure
71
          Event baseEvent = client.v2().core().events().retrieve(thinEvent.getId());
×
72
          if (baseEvent instanceof V1BillingMeterErrorReportTriggeredEvent) {
×
73
            V1BillingMeterErrorReportTriggeredEvent event =
×
74
                (V1BillingMeterErrorReportTriggeredEvent) baseEvent;
75
            Meter meter = event.fetchRelatedObject();
×
76

77
            String meterId = meter.getId();
×
78
            System.out.println(meterId);
×
79

80
            // Record the failures and alert your team
81
            // Add your logic here
82
          }
83

84
          exchange.sendResponseHeaders(200, -1);
×
85
        } catch (StripeException e) {
×
86
          exchange.sendResponseHeaders(400, -1);
×
87
        }
×
88
      } else {
×
89
        exchange.sendResponseHeaders(405, -1);
×
90
      }
91
      exchange.close();
×
92
    }
×
93
  }
94
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc