Skip to content

Commit 90e5bd9

Browse files
Improve event notification example (#2082)
* improve example * update docs
1 parent bf925ff commit 90e5bd9

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.stripe.events.V1BillingMeterErrorReportTriggeredEventNotification;
77
import com.stripe.exception.StripeException;
88
import com.stripe.model.billing.Meter;
9-
import com.stripe.model.v2.core.Event;
109
import com.stripe.model.v2.core.EventNotification;
1110
import com.sun.net.httpserver.HttpExchange;
1211
import com.sun.net.httpserver.HttpHandler;
@@ -67,33 +66,39 @@ public void handle(HttpExchange exchange) throws IOException {
6766
String sigHeader = exchange.getRequestHeaders().getFirst("Stripe-Signature");
6867

6968
try {
70-
EventNotification eventNotif =
69+
EventNotification notif =
7170
client.parseEventNotification(webhookBody, sigHeader, WEBHOOK_SECRET);
7271

73-
// determine what sort of event you have
74-
if (eventNotif instanceof V1BillingMeterErrorReportTriggeredEventNotification) {
72+
if (notif instanceof V1BillingMeterErrorReportTriggeredEventNotification) {
7573
V1BillingMeterErrorReportTriggeredEventNotification eventNotification =
76-
(V1BillingMeterErrorReportTriggeredEventNotification) eventNotif;
74+
(V1BillingMeterErrorReportTriggeredEventNotification) notif;
7775

78-
// after casting, can fetch the related object (which is correctly typed)
79-
Meter meter = eventNotification.fetchRelatedObject();
80-
System.out.println(meter.getId());
76+
// there's basic info about the related object in the notification
77+
System.out.println(
78+
"Meter w/ id " + eventNotification.getRelatedObject().getId() + " had a problem");
8179

80+
// or you can fetch the full object form the API for more details
81+
Meter meter = eventNotification.fetchRelatedObject();
82+
StringBuilder sb = new StringBuilder();
83+
sb.append("Meter ")
84+
.append(meter.getDisplayName())
85+
.append(" (")
86+
.append(meter.getId())
87+
.append(") had a problem");
88+
System.out.println(sb.toString());
89+
90+
// And you can always fetch the full event:
8291
V1BillingMeterErrorReportTriggeredEvent event = eventNotification.fetchEvent();
83-
System.out.println(event.getData().getDeveloperMessageSummary());
84-
85-
// add additional logic
86-
}
87-
// ... check other event types you know about
88-
else if (eventNotif instanceof UnknownEventNotification) {
89-
UnknownEventNotification unknownEvent = (UnknownEventNotification) eventNotif;
90-
System.out.println("Received unknown event: " + unknownEvent.getId());
91-
// can keep matching on the "type" field
92-
// other helper methods still work, but you'll have to handle types yourself
92+
System.out.println("More info: " + event.getData().getDeveloperMessageSummary());
93+
} else if (notif instanceof UnknownEventNotification) {
94+
// Events that were introduced after this SDK version release are
95+
// represented as `UnknownEventNotification`s.
96+
// They're valid, the SDK just doesn't have corresponding classes for them.
97+
// You must match on the "type" property instead.
98+
UnknownEventNotification unknownEvent = (UnknownEventNotification) notif;
9399
if (unknownEvent.getType().equals("some.new.event")) {
94-
Event event = unknownEvent.fetchEvent();
95-
System.out.println(event.getReason());
96-
// handle
100+
// you can still `.fetchEvent()` and `.fetchRelatedObject()`, but the latter may
101+
// return `null` if that event type doesn't have a related object.
97102
}
98103
}
99104

0 commit comments

Comments
 (0)