diff --git a/.rubocop.yml b/.rubocop.yml index a12b40ef..63202dfe 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -18,6 +18,7 @@ Layout/LineLength: Exclude: - "lib/stripe/object_types.rb" - "lib/stripe/stripe_client.rb" + - "lib/stripe/stripe_event_handler.rb" - "lib/stripe/resources/**/*.rb" - "lib/stripe/services/**/*.rb" - "lib/stripe/events/**/*.rb" @@ -86,6 +87,10 @@ Metrics/ParameterLists: - "lib/stripe/stripe_client.rb" - "lib/stripe/params/**/*.rb" +Naming/MethodName: + Exclude: + - "lib/stripe/stripe_event_handler.rb" + Naming/MethodParameterName: # We have many parameters that are less than 3 characters for tax codes Exclude: diff --git a/lib/stripe/stripe_event_handler.rb b/lib/stripe/stripe_event_handler.rb new file mode 100644 index 00000000..c73fe7e6 --- /dev/null +++ b/lib/stripe/stripe_event_handler.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +module Stripe + class StripeEventHandler + def initialize(client, webhook_secret) + @client = client + @registered_handlers = {} + @webhook_secret = webhook_secret + end + + def handle(webhook_body, sig_header) + @client.parse_event_notification( + webhook_body, + sig_header, + @webhook_secret + ) + + # TODO: rebind stripe-context temporarily + case event_notification.type + # event-method-routing: The beginning of the section generated from our OpenAPI spec + when + "v1.billing.meter.error_report_triggered" + on_V1BillingMeterErrorReportTriggeredEventNotification(event_notification, @client) + when + "v1.billing.meter.no_meter_found" + on_V1BillingMeterNoMeterFoundEventNotification(event_notification, @client) + when + "v2.core.event_destination.ping" + on_V2CoreEventDestinationPingEventNotification(event_notification, @client) + # event-method-routing: The end of the section generated from our OpenAPI spec + else + # only used for types that the SDK has no class for + on_UnknownEventNotification( + event_notification, + @client + ) + end + end + + # Overwrite this method to handle events that the SDK has types for, but you've chosen not to handle with their dedicated method. + def on_unhandled_event_notification(event_notification, _client) + 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}\"" + end + + def on_UnknownEventNotification(event_notification, _client) + 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." + end + + # event-handler-methods: The beginning of the section generated from our OpenAPI spec + def on_V1BillingMeterErrorReportTriggeredEventNotification(event_notification, client) + on_unhandled_event_notification(event_notification, client) + end + + def on_V1BillingMeterNoMeterFoundEventNotification(event_notification, client) + on_unhandled_event_notification(event_notification, client) + end + + def on_V2CoreEventDestinationPingEventNotification(event_notification, client) + on_unhandled_event_notification(event_notification, client) + end + # event-handler-methods: The end of the section generated from our OpenAPI spec + end +end diff --git a/rbi/stripe/stripe_event_handler.rbi b/rbi/stripe/stripe_event_handler.rbi new file mode 100644 index 00000000..d887a7bf --- /dev/null +++ b/rbi/stripe/stripe_event_handler.rbi @@ -0,0 +1,53 @@ +# frozen_string_literal: true +# typed: true + +module Stripe + class StripeEventHandler + sig do + params(client: ::Stripe::StripeClient, webhook_secret: String) + .void + end + def initialize(client, webhook_secret); end + end + + sig do + params( + webhook_body: String, + sig_header: String + ) + .void + end + def handle(webhook_body, sig_header); end + + sig do + overridable.params(event_notification: ::Stripe::V2::Core::EventNotification, client: ::Stripe::StripeClient).void + end + def on_unhandled_event_notification(event_notification, client); end + + sig do + overridable.params(event_notification: ::Stripe::Events::UnknownEventNotification, client: ::Stripe::StripeClient).void + end + def on_UnknownEventNotification(event_notification, client); end + + # event-handler-methods: The beginning of the section generated from our OpenAPI spec + sig do + overridable.params(event_notification: ::Stripe::Events::V1BillingMeterErrorReportTriggeredEventNotification, client: ::Stripe::StripeClient).void + end + def on_V1BillingMeterErrorReportTriggeredEventNotification(event_notification, client); + end + + sig do + overridable.params(event_notification: ::Stripe::Events::V1BillingMeterNoMeterFoundEventNotification, client: ::Stripe::StripeClient).void + end + def on_V1BillingMeterNoMeterFoundEventNotification(event_notification, client); + end + + sig do + overridable.params(event_notification: ::Stripe::Events::V2CoreEventDestinationPingEventNotification, client: ::Stripe::StripeClient).void + end + def on_V2CoreEventDestinationPingEventNotification(event_notification, client); + end + + + # event-handler-methods: The end of the section generated from our OpenAPI spec + end