mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-12-09 00:01:53 -05:00
* manual changes * move eventNotification to v2 namespace * updated rbi * add tests * Add basic rbi * generate event data types * move some things, fix tests * add missing attributes * update gemspec, examples, and rbi * fix example
38 lines
1.3 KiB
Ruby
38 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
# typed: false
|
|
|
|
# event_notification_webhook_handler.rb - receive and process event notification like the
|
|
# v1.billing.meter.error_report_triggered event.
|
|
#
|
|
# In this example, we:
|
|
# - create a StripeClient called client
|
|
# - use client.parse_event_notification to parse the received event notification webhook body
|
|
# - call client.v2.core.events.retrieve to retrieve the full event object
|
|
# - if it is a V1BillingMeterErrorReportTriggeredEvent event type, call
|
|
# event.fetchRelatedObject to retrieve the Billing Meter object associated
|
|
# with the event.
|
|
|
|
require "stripe"
|
|
require "sinatra"
|
|
|
|
api_key = ENV.fetch("STRIPE_API_KEY", nil)
|
|
# Retrieve the webhook secret from the environment variable
|
|
webhook_secret = ENV.fetch("WEBHOOK_SECRET", nil)
|
|
|
|
client = Stripe::StripeClient.new(api_key)
|
|
|
|
post "/webhook" do
|
|
webhook_body = request.body.read
|
|
sig_header = request.env["HTTP_STRIPE_SIGNATURE"]
|
|
event_notification = client.parse_event_notification(webhook_body, sig_header, webhook_secret)
|
|
|
|
if event_notification.instance_of?(Stripe::Events::V1BillingMeterErrorReportTriggeredEventNotification)
|
|
meter = event_notification.fetch_related_object
|
|
meter_id = meter.id
|
|
puts "Success!", meter_id
|
|
end
|
|
|
|
# Record the failures and alert your team
|
|
status 200
|
|
end
|