Sha256: 6207bd3e49e5f486e9b695a4cc72983e38c04423b75a7f4bb437062edfb2fb76

Contents?: true

Size: 1.31 KB

Versions: 24

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true
# typed: false

# thinevent_webhook_handler.rb - receive and process thin events like the
# v1.billing.meter.error_report_triggered event.
#
# In this example, we:
#     - create a StripeClient called client
#     - use client.parse_thin_event to parse the received thin event 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"]
  thin_event = client.parse_thin_event(webhook_body, sig_header, webhook_secret)

  # Fetch the event data to understand the failure
  event = client.v2.core.events.retrieve(thin_event.id)
  if event.instance_of? Stripe::V1BillingMeterErrorReportTriggeredEvent
    meter = event.fetch_related_object
    meter_id = meter.id
    puts "Success!", meter_id
  end

  # Record the failures and alert your team
  status 200
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
stripe-13.6.0.pre.beta.1 examples/thinevent_webhook_handler.rb
stripe-13.5.0 examples/thinevent_webhook_handler.rb
stripe-13.5.0.pre.beta.1 examples/thinevent_webhook_handler.rb
stripe-13.4.1 examples/thinevent_webhook_handler.rb
stripe-13.4.0 examples/thinevent_webhook_handler.rb
stripe-13.4.0.pre.beta.4 examples/thinevent_webhook_handler.rb
stripe-13.4.0.pre.beta.3 examples/thinevent_webhook_handler.rb
stripe-13.3.1 examples/thinevent_webhook_handler.rb
stripe-13.4.0.pre.beta.2 examples/thinevent_webhook_handler.rb
stripe-13.4.0.pre.beta.1 examples/thinevent_webhook_handler.rb
stripe-13.3.0 examples/thinevent_webhook_handler.rb
stripe-13.3.0.pre.beta.3 examples/thinevent_webhook_handler.rb
stripe-13.3.0.pre.beta.2 examples/thinevent_webhook_handler.rb
stripe-13.3.0.pre.beta.1 examples/thinevent_webhook_handler.rb
stripe-13.2.0 examples/thinevent_webhook_handler.rb
stripe-13.1.2 examples/thinevent_webhook_handler.rb
stripe-13.2.0.pre.beta.3 examples/thinevent_webhook_handler.rb
stripe-13.2.0.pre.beta.2 examples/thinevent_webhook_handler.rb
stripe-13.1.1 examples/thinevent_webhook_handler.rb
stripe-13.2.0.pre.beta.1 examples/thinevent_webhook_handler.rb