Skip to content

Commit c059cf9

Browse files
Update generated code for v2255 and
1 parent 9d5834a commit c059cf9

7 files changed

Lines changed: 3590 additions & 1 deletion

File tree

CODEGEN_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f0187e75a5c663247a7e8d02c02e8f3c69c9b144
1+
f02038df5433a605fce164b9af1b8b445252ee4f

src/main/java/com/stripe/StripeEventNotificationHandler.java

Lines changed: 2960 additions & 0 deletions
Large diffs are not rendered by default.

src/main/java/com/stripe/events/UnknownEventNotification.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// File copied from our code generator; changes here will be overwritten.
12
package com.stripe.events;
23

34
import com.google.gson.annotations.SerializedName;
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// File copied from our code generator; changes here will be overwritten.
2+
package com.stripe.examples;
3+
4+
import com.stripe.StripeClient;
5+
import com.stripe.StripeEventNotificationHandler;
6+
import com.stripe.StripeEventNotificationHandler.UnhandledNotificationDetails;
7+
import com.stripe.events.V1BillingMeterErrorReportTriggeredEventNotification;
8+
import com.stripe.exception.StripeException;
9+
import com.stripe.model.billing.Meter;
10+
import com.stripe.model.v2.core.EventNotification;
11+
import com.sun.net.httpserver.HttpExchange;
12+
import com.sun.net.httpserver.HttpHandler;
13+
import com.sun.net.httpserver.HttpServer;
14+
import java.io.ByteArrayOutputStream;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.net.InetSocketAddress;
18+
import java.nio.charset.StandardCharsets;
19+
20+
/**
21+
* Receive and process event notifications (AKA thin events) like
22+
* "v1.billing.meter.error_report_triggered" using EventNotificationHandler.
23+
*
24+
* <p>In this example, we:
25+
*
26+
* <ul>
27+
* <li>write a fallback callback to handle unrecognized event notifications
28+
* <li>create a StripeClient called client
29+
* <li>Initialize an EventNotificationHandler with the client, webhook secret, and fallback
30+
* callback
31+
* <li>register a specific handler for the "v1.billing.meter.error_report_triggered" event
32+
* notification type
33+
* <li>use handler.handle() to process the received notification webhook body
34+
* </ul>
35+
*/
36+
public class EventNotificationHandlerEndpoint {
37+
private static final String API_KEY = System.getenv("STRIPE_API_KEY");
38+
private static final String WEBHOOK_SECRET = System.getenv("WEBHOOK_SECRET");
39+
40+
private static final StripeClient client = new StripeClient(API_KEY);
41+
private static final StripeEventNotificationHandler handler =
42+
client.notificationHandler(
43+
WEBHOOK_SECRET, EventNotificationHandlerEndpoint::fallbackCallback);
44+
45+
public static void main(String[] args) throws IOException {
46+
handler.onV1BillingMeterErrorReportTriggered(
47+
EventNotificationHandlerEndpoint::handleMeterErrors);
48+
49+
HttpServer server = HttpServer.create(new InetSocketAddress(4242), 0);
50+
server.createContext("/webhook", new WebhookHandler());
51+
server.setExecutor(null);
52+
server.start();
53+
}
54+
55+
private static void fallbackCallback(
56+
EventNotification notif, StripeClient client, UnhandledNotificationDetails details) {
57+
System.out.println("Received unhandled event notification type: " + notif.getType());
58+
}
59+
60+
private static void handleMeterErrors(
61+
V1BillingMeterErrorReportTriggeredEventNotification notif, StripeClient client) {
62+
Meter meter;
63+
try {
64+
meter = notif.fetchRelatedObject();
65+
} catch (StripeException e) {
66+
// TODO Auto-generated catch block
67+
e.printStackTrace();
68+
return;
69+
}
70+
System.out.println("Handling meter error for meter: " + meter.getDisplayName());
71+
}
72+
73+
static class WebhookHandler implements HttpHandler {
74+
// For Java 1.8 compatibility
75+
public static byte[] readAllBytes(InputStream inputStream) throws IOException {
76+
final int bufLen = 1024;
77+
byte[] buf = new byte[bufLen];
78+
int readLen;
79+
80+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
81+
82+
while ((readLen = inputStream.read(buf, 0, bufLen)) != -1)
83+
outputStream.write(buf, 0, readLen);
84+
85+
return outputStream.toByteArray();
86+
}
87+
88+
@Override
89+
public void handle(HttpExchange exchange) throws IOException {
90+
if ("POST".equals(exchange.getRequestMethod())) {
91+
InputStream requestBody = exchange.getRequestBody();
92+
String webhookBody = new String(readAllBytes(requestBody), StandardCharsets.UTF_8);
93+
String sigHeader = exchange.getRequestHeaders().getFirst("Stripe-Signature");
94+
95+
try {
96+
handler.handle(webhookBody, sigHeader);
97+
98+
exchange.sendResponseHeaders(200, -1);
99+
} catch (StripeException e) {
100+
exchange.sendResponseHeaders(400, -1);
101+
}
102+
} else {
103+
exchange.sendResponseHeaders(405, -1);
104+
}
105+
exchange.close();
106+
}
107+
}
108+
}

src/main/java/com/stripe/examples/EventNotificationWebhookHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// File copied from our code generator; changes here will be overwritten.
12
package com.stripe.examples;
23

34
import com.stripe.StripeClient;

src/main/java/com/stripe/model/v2/core/EventNotification.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// File copied from our code generator; changes here will be overwritten.
12
package com.stripe.model.v2.core;
23

34
import com.google.gson.JsonObject;

0 commit comments

Comments
 (0)