Skip to content

Commit 98b345b

Browse files
Improve event notification example (#1934)
* update webhook example * Fix comments * update docs * linting
1 parent 63c4e46 commit 98b345b

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

examples/EventNotificationWebhookHandler.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,24 @@
2626

2727
// check what type of event notification we have
2828
if ($event_notification instanceof Stripe\Events\V1BillingMeterErrorReportTriggeredEventNotification) {
29-
$meter = $event_notification->fetchRelatedObject();
30-
$meter_id = $meter->id;
29+
// there's basic info about the related object in the notification
30+
echo "Meter with id {$event_notification->related_object->id} reported an error\n";
3131

32-
// Record the failures and alert your team
33-
// Add your logic here
32+
// or you can fetch the full object form the API for more details
33+
$meter = $event_notification->fetchRelatedObject();
34+
echo "Meter {$meter->display_name} ({$meter->id}) had a problem\n";
3435

35-
// can fetch full event w/ data
36+
// And you can always fetch the full event:
3637
$event = $event_notification->fetchEvent();
37-
// data is fully typed
38-
$event->data->developer_message_summary;
38+
echo "More info: {$event->data->developer_message_summary}\n";
39+
} elseif ($event_notification instanceof Stripe\Events\UnknownEventNotification) {
40+
// Events that were introduced after this SDK version release are
41+
// represented as `UnknownEventNotification`s.
42+
// They're valid, the SDK just doesn't have corresponding classes for them.
43+
// You must match on the "type" property instead.
44+
if ('some.new.event' === $event_notification->type) {
45+
// handle it the same way as above
46+
}
3947
}
4048

4149
return $response->withStatus(200);

0 commit comments

Comments
 (0)