Closed heaven closed 4 years ago
Hi @heaven (wow great handle!), the call to Stripe is protection against spoofing attacks.
If you're trying to test this with an automated test, I recommend mocking the call to Stripe.
If you're trying to test to see if you have everything hooked up correctly to Stripe, I recommend checking out the "Signing Webhooks" section of the readme: https://github.com/tansengming/stripe-rails#signed-webhooks
Setting up signing skips the call to Stripe since we can verify that the webhook come from Stripe.
Hi @tansengming, thanks ;)
I came up with a bit trickier but working solution.
# code that makes the charge and actually reaches Stripe API
# (we are going to add VCR to reduce the calls but still would like to
# perform the actual calls from time to time).
# charge = do_perform_charge
# Sent charge succeed event
StripeMock.mock do # mocks stripe events
Sidekiq::Testing.inline! do # processes the event right away
Stripe::Rails::Testing.send_event "charge.succeeded", charge.to_h
end
end
So that I use a mocked event from StripeMock
but actual charge object.
I've got the idea regarding spoofing protection, that's great and we also do this. Actually, we still do this even with these callbacks. The idea of callbacks is awesome but in our case, we process events asynchronously so this API call is redundant. We only keep event id and type from the callback and store them in the database, in our internal StripeEvent model, which adds itself to Sidekiq queue after commit.
Another idea that came to my mind when I was working with this is that it would be awesome if stripe-rails had it's own Testing module that could be used in tests and would allow to simulate callbacks with actual event returned from Stripe API. I'm not sure if there's API that would allow this but this would be an awesome addition to what we already have.
No worries, I'm glad to hear that you've got it figured out.
I'm not sure it'll work for you but the gem also comes with a testing module https://github.com/tansengming/stripe-rails/#unit-testing
@tansengming Hi, one more addition to this – sending a test event from Stripe Dashboard also fails due to this same problem.
My code is very simple, what I'm basically doing is just:
From the backtrace it seems like Stripe is trying to load the original event. I'm wondering how I will be testing this. Previously I used to implement my own webhooks controller for stripe and was simply using event id and type. The original even was loaded later on from a sidekiq job. Now it simply slows things down.