Retry failed Stripe Webhook events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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