Skip to content

Instantly share code, notes, and snippets.

@jeremywadsack
Created May 29, 2019 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremywadsack/d77fd9b8d8ee360e8c6a9d7d999ecb4b to your computer and use it in GitHub Desktop.
Save jeremywadsack/d77fd9b8d8ee360e8c6a9d7d999ecb4b to your computer and use it in GitHub Desktop.
Retry failed Stripe Webhook events
# frozen_string_literal: true
# Stripe will retry failed webhooks seven times with exponential backoff. If that has been exceeded
# then the webhook event is marked "failed" and won't be retried. We can resend the event by [retrieving
# the event data and posting it to the
# webhook](https://groups.google.com/a/lists.stripe.com/forum/#!topic/api-discuss/N33ZXqp3NzI).
#
# To use this, configure your endpoint URL below, then run this in providing event IDs on the command line.
WEB_HOOK = "YOUR ENDPOINT HERE"
SECRET = StripeEvent.signing_secrets.first # Hard-code this if you don't have the secret in this environment
event_ids = ARGV # Or hard-code a list of event IDs
def sign_event(data)
time = Time.now.to_i
payload = "#{time}.#{data}"
signature = Stripe::Webhook::Signature.send(:compute_signature, payload, SECRET)
"t=#{time},v1=#{signature},v0=thisdoesntmatter"
end
event_ids.each do |event_id|
event_data = Stripe::Event.retrieve(event_id).to_json
response = Excon.post(
WEB_HOOK,
headers: {
"Content-Type" => "application/json; charset=utf8",
"Cache-Control" => "no-cache",
"User-Agent" => "Stripe/1.0 (+https://stripe.com/docs/webhooks)",
"Stripe-Signature" => sign_event(event_data)
},
body: event_data
)
puts "#{event_id}: (#{response.status})\n#{response.body}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment