integrallis / stripe_event

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

How do you manage responses? #131

Open amex-doug opened 4 years ago

amex-doug commented 4 years ago

I didn't see this in the documentation, but how does this library respond with 400s?

maltesa commented 4 years ago

I was wondering about that too. For me it seems that it is not possible to control the response code. In the webhook controller matching hooks are called and 200 ok responsive is returned.

def event
      StripeEvent.instrument(verified_event)
      head :ok
 rescue Stripe::SignatureVerificationError => e
      log_error(e)
      head :bad_request
 end

Isn't it a common use case to return error codes if something goes wrong? In my case I convert a user to a premium user for incoming 'checkout.session.completed' events. If that failes I would like to return a 422 or something similar.

Update

I think a problem is that an event can have many subscribers. Each may set a different response code. I tried to solve that by using an exception. I implemented it testwise (https://github.com/integrallis/stripe_event/pull/132) but I think it breaks the processing of the event by other subscribers. So it is probably not a good solution. Just wanted to reference it here.

dmz006 commented 3 years ago

I'm the first to admit I'm a hack coder so maybe I don't grok how different subscribers would break this. I'm testing out a slight change to your patch #132 but made a slight change to your webhook controller for ProcessError:

rescue Stripe::SignatureVerificationError => e
  log_error(e)
  head :bad_request
rescue StripeEvent::ProcessError => e
  head e.message.to_i
end

And I'm calling it by raising the error similar to yours but passing through the return code I want sent: raise StripeEvent::ProcessError.new(400)

So far it's working in my tests and I can customize the header by just sending a valid response code number.

rromanchuk commented 3 years ago

I'm also struggling to find a clean way to extend this controller, i realized these webhooks were failing to get tagged properly as i'm not appending needed params for lograge https://github.com/roidrage/lograge

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  def append_info_to_payload(payload)
    super
    payload[:service] = :stripe
   # ...truncated
  end
end

pretty reasonable. anyone happy with their implementation of something similar?