integrallis / stripe_event

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

Support for webhook route with dynamic segment #136

Open jkburges opened 3 years ago

jkburges commented 3 years ago

I'm trying to solve the problem of knowing from which stripe account events are coming. It doesn't seem like there's consistently an account field in the event from stripe (I think this may only be a stripe connect thing, which I don't have).

e.g. with a route definition having a dynamic segment such as:

mount StripeEvent::Engine, at: "/stripe/event/(:account_country)"

I can configure my US stripe account to hit "/stripe/event/us" and likewise my AU account to hit "/stripe/event/au".

The dynamic segment value is then available at https://github.com/integrallis/stripe_event/blob/master/app/controllers/stripe_event/webhook_controller.rb#L8 as params[:account_country].

After that point however, the information is lost and doesn't make it through to the event handlers.

One way to pass it on would be to change https://github.com/integrallis/stripe_event/blob/master/app/controllers/stripe_event/webhook_controller.rb#L8 to:

StripeEvent.instrument(verified_event, params)

and so on downstream, so that the event handler ends up having an interface such as:

class Handler
  def call(event, params)
  end
end

but I'm not sure if that can be done without breaking compatibility with the old interface.

Another alternative would be to piggyback on the event object itself, e.g at https://github.com/integrallis/stripe_event/blob/master/app/controllers/stripe_event/webhook_controller.rb#L8"

StripeEvent.instrument(verified_event.merge(params: params))

but that seems a bit of a hack.

Does anyone have some advice?

daniely commented 3 years ago

I have a need for seeing the params along with the event as well. Any update on this?

jkburges commented 3 years ago

@daniely FWIW I didn't get anywhere with this.

I might be able to submit a PR at some point but would need advice from the gem owners on the best way to proceed.