|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Stripe |
| 4 | + class StripeEventHandler |
| 5 | + def initialize(client, webhook_secret) |
| 6 | + @client = client |
| 7 | + @registered_handlers = {} |
| 8 | + @webhook_secret = webhook_secret |
| 9 | + end |
| 10 | + |
| 11 | + def handle(webhook_body, sig_header) |
| 12 | + @client.parse_event_notification( |
| 13 | + webhook_body, |
| 14 | + sig_header, |
| 15 | + @webhook_secret |
| 16 | + ) |
| 17 | + |
| 18 | + # TODO: rebind stripe-context temporarily |
| 19 | + case event_notification.type |
| 20 | + # event-method-routing: The beginning of the section generated from our OpenAPI spec |
| 21 | + when |
| 22 | + "v1.billing.meter.error_report_triggered" |
| 23 | + on_V1BillingMeterErrorReportTriggeredEventNotification(event_notification, @client) |
| 24 | + when |
| 25 | + "v1.billing.meter.no_meter_found" |
| 26 | + on_V1BillingMeterNoMeterFoundEventNotification(event_notification, @client) |
| 27 | + when |
| 28 | + "v2.core.event_destination.ping" |
| 29 | + on_V2CoreEventDestinationPingEventNotification(event_notification, @client) |
| 30 | + # event-method-routing: The end of the section generated from our OpenAPI spec |
| 31 | + else |
| 32 | + # only used for types that the SDK has no class for |
| 33 | + on_UnknownEventNotification( |
| 34 | + event_notification, |
| 35 | + @client |
| 36 | + ) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + # Overwrite this method to handle events that the SDK has types for, but you've chosen not to handle with their dedicated method. |
| 41 | + def on_unhandled_event_notification(event_notification, _client) |
| 42 | + raise "Received an event that the SDK has a corresponding class for, but for which you haven't written a corresponding handler: \"#{event_notification.type}\"" |
| 43 | + end |
| 44 | + |
| 45 | + def on_UnknownEventNotification(event_notification, _client) |
| 46 | + raise "Received event type that the SDK doesn't have a corresponding class for: \"#{event_notification.type}\". Consider upgrading your SDK to handle this more gracefully." |
| 47 | + end |
| 48 | + |
| 49 | + # event-handler-methods: The beginning of the section generated from our OpenAPI spec |
| 50 | + def on_V1BillingMeterErrorReportTriggeredEventNotification(event_notification, client) |
| 51 | + on_unhandled_event_notification(event_notification, client) |
| 52 | + end |
| 53 | + |
| 54 | + def on_V1BillingMeterNoMeterFoundEventNotification(event_notification, client) |
| 55 | + on_unhandled_event_notification(event_notification, client) |
| 56 | + end |
| 57 | + |
| 58 | + def on_V2CoreEventDestinationPingEventNotification(event_notification, client) |
| 59 | + on_unhandled_event_notification(event_notification, client) |
| 60 | + end |
| 61 | + # event-handler-methods: The end of the section generated from our OpenAPI spec |
| 62 | + end |
| 63 | +end |
0 commit comments