|
6 | 6 | import com.stripe.events.V1BillingMeterErrorReportTriggeredEventNotification; |
7 | 7 | import com.stripe.exception.StripeException; |
8 | 8 | import com.stripe.model.billing.Meter; |
9 | | -import com.stripe.model.v2.core.Event; |
10 | 9 | import com.stripe.model.v2.core.EventNotification; |
11 | 10 | import com.sun.net.httpserver.HttpExchange; |
12 | 11 | import com.sun.net.httpserver.HttpHandler; |
@@ -67,33 +66,39 @@ public void handle(HttpExchange exchange) throws IOException { |
67 | 66 | String sigHeader = exchange.getRequestHeaders().getFirst("Stripe-Signature"); |
68 | 67 |
|
69 | 68 | try { |
70 | | - EventNotification eventNotif = |
| 69 | + EventNotification notif = |
71 | 70 | client.parseEventNotification(webhookBody, sigHeader, WEBHOOK_SECRET); |
72 | 71 |
|
73 | | - // determine what sort of event you have |
74 | | - if (eventNotif instanceof V1BillingMeterErrorReportTriggeredEventNotification) { |
| 72 | + if (notif instanceof V1BillingMeterErrorReportTriggeredEventNotification) { |
75 | 73 | V1BillingMeterErrorReportTriggeredEventNotification eventNotification = |
76 | | - (V1BillingMeterErrorReportTriggeredEventNotification) eventNotif; |
| 74 | + (V1BillingMeterErrorReportTriggeredEventNotification) notif; |
77 | 75 |
|
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"); |
81 | 79 |
|
| 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: |
82 | 91 | 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; |
93 | 99 | 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. |
97 | 102 | } |
98 | 103 | } |
99 | 104 |
|
|
0 commit comments