integrallis / stripe_event

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

Can we get a "test mode" option that skips the 'retrieve' check. #58

Closed george-carlin closed 8 years ago

george-carlin commented 9 years ago

From the README:

This button sends an example event to your webhook urls, including an id of evt_00000000000000. To confirm that Stripe sent the webhook, StripeEvent attempts to retrieve the event details from Stripe using the given id. In this case the event does not exist and StripeEvent responds with 401 Unauthorized. Instead of using the 'Test Webhooks' button, trigger webhooks by using the Stripe API or Dashboard to create test payments, customers, etc.

Makes sense - except this is completely impractical for many kinds of events. (How can I trigger a customer.subscription.trial_will_end event from the dashboard, short of creating a plan with a 1-day trial, creating a new customer on that plan, then waiting 24 hours for it to expire? I'd like to able to trigger test webhooks more than once a day!)

I've been able to work around this in my tests by replacing this line with the following:

    event = Stripe::Event.construct_from(params)

Of course, this is a terrible idea in a production environment, but it makes my life MUCH easier when testing (and I don't care about validating that the stripe event is 'real' in my tests).

How about I add a config option to StripeEvent called "test_mode", a boolean that's false by default? When it's false, nothing changes from the current implementation of the gem. When it's true, the gem skips the authorization checks using my line above.

(Potentially it could also raise a warning or an error if StripeEvent is in test mode while the Rails env is in production)

What do you think? I'm happy to code this up myself, I just wanted to make sure it would actually get accepted before I start coding.

spinlock99 commented 9 years ago

This is a great idea. Please do implement it. I just had to onboard a new hire who was really confused when he tried to send test hooks from stripe.

xn commented 9 years ago

Couldn't you just do:

StripeEvent.event_retriever = lambda do |params|
  Stripe::Event.construct_from(params)
end
xn commented 9 years ago

You could stick that in the spec_helper or guard it with a if Rails.env.test? call.

xn commented 9 years ago

Also, you probably want to call Stripe::Event.construct_from(params.deep_symbolize_keys) to make sure all the keys are symbols.

spinlock99 commented 9 years ago

Great idea. I think this would actually be best suited for Rails.env.development? rather than test in our case. We want to spin up the rails server on our dev machines and then send test hooks through Stripe's web api and observe the result locally.

The specs actually work fine with the current setup. They use the Ruby api to interact with Stripe and it sends well formed responses (that can be validated) back to the test server. (we also use VCR so that we aren't constantly hitting the api in our specs).

Thanks for the tip!

xn commented 9 years ago

Hey, I used this library for the first time yesterday. I just read the code. This gem is extremely well written. Kudos to the maintainers.

rmm5t commented 8 years ago

This can be done using a custom EventRetriever. For example: https://github.com/integrallis/stripe_event#configuration