integrallis / stripe_event

Stripe webhook integration for Rails applications.
https://rubygems.org/gems/stripe_event
MIT License
841 stars 107 forks source link

Returning proper error responses back to Stripe webhook calls #22

Closed codespore closed 10 years ago

codespore commented 10 years ago

Hi Guys,

I think it's good to add in the README for sending proper error response back to Stripe Webhook calls by raising Stripe::StripeError, since your Webhook controller implementation already has a rescue that returns a :unauthorized status. Example:-

subscribe 'invoice.payment_succeeded' do |event|
  subscription = Subscription.find_by_stripe_invoice_id(event.data.object.id)
  if subscription
    subscription.succeed!
  else
    raise Stripe::StripeError
  end
end

Quite often that Stripe sends over a post call to my application where, for some reason, records are not found and responds with a 500 error. Only after a 2nd retry from Stripe that it goes through.

It's also stated in Stripe that they will retry if the response they get is other than 200 OK - https://stripe.com/docs/webhooks

Let me know what you guys think.

invisiblefunnel commented 10 years ago

There's discussion on exception handling on #23. I'll likely remove the rescue altogether.

Quite often that Stripe sends over a post call to my application where, for some reason, records are not found and responds with a 500 error. Only after a 2nd retry from Stripe that it goes through.

It's possible you're seeing a race condition where the database record has not been committed by the time the webhook arrives. I'd be interested to hear more details on this problem.