|
7 | 7 | # In this example, we: |
8 | 8 | # - create a StripeClient called client |
9 | 9 | # - 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 |
11 | 11 | # - 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 |
13 | 13 | # 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 |
14 | 16 |
|
15 | 17 | require "stripe" |
16 | 18 | require "sinatra" |
|
27 | 29 | event_notification = client.parse_event_notification(webhook_body, sig_header, webhook_secret) |
28 | 30 |
|
29 | 31 | 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 |
30 | 36 | 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 |
33 | 49 | end |
34 | 50 |
|
35 | 51 | # Record the failures and alert your team |
|
0 commit comments