Skip to content

Commit 313cbac

Browse files
improve example (stripe#1693)
1 parent c269993 commit 313cbac

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

examples/event_notification_webhook_handler.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
# In this example, we:
88
# - create a StripeClient called client
99
# - use client.parse_event_notification to parse the received event notification webhook body
10-
# - call client.v2.core.events.retrieve to retrieve the full event object
10+
# - call event_notification.fetch_event to retrieve the full event object
1111
# - if it is a V1BillingMeterErrorReportTriggeredEvent event type, call
12-
# event.fetchRelatedObject to retrieve the Billing Meter object associated
12+
# event_notification.fetch_related_object to retrieve the Billing Meter object associated
1313
# with the event.
14+
# - if it is an UnknownEventNotification, check the type property to see if it matches
15+
# a known event type and handle it accordingly
1416

1517
require "stripe"
1618
require "sinatra"
@@ -27,9 +29,23 @@
2729
event_notification = client.parse_event_notification(webhook_body, sig_header, webhook_secret)
2830

2931
if event_notification.instance_of?(Stripe::Events::V1BillingMeterErrorReportTriggeredEventNotification)
32+
# there's basic info about the related object in the notification
33+
puts "Received event for meter", event_notification.related_object.id
34+
35+
# but you can also fetch it if you need more info
3036
meter = event_notification.fetch_related_object
31-
meter_id = meter.id
32-
puts "Success!", meter_id
37+
puts "Meter name:", meter.display_name
38+
39+
# there's often more information on the actual event
40+
event = event_notification.fetch_event
41+
42+
puts "Meter had an error", event.data.developer_message_summary
43+
elsif event_notification.instance_of?(Stripe::Events::UnknownEventNotification)
44+
# this is a valid event type, but this SDK predates it
45+
# we'll have to match on type instead
46+
if event_notification.type == "some.new.event"
47+
# your logic goes here
48+
end
3349
end
3450

3551
# Record the failures and alert your team

0 commit comments

Comments
 (0)