18

I want to test my application's handling of webhook events from stripe when a subscription payment has been made (or failed). Here is what I've tried so far:

  • Set up a new subscription
  • Update user's credit card to be the one that can be added to an account, but will fail to actually be charged
  • Change the trial end date to be in one second
  • Wait a few seconds expecting the webhook to be sent

However, According to the documentation:

If you have configured webhooks, the invoice will wait until one hour after the last webhook is successfully sent (or the last webhook times out after failing).

One hour is a long time to wait, since I am trying to do this as part of an automated integration test suite.

One suggestion (from IRC) is to fake out the webhook request, so that my integration test sends the event, instead of Stripe sending it. However, since Stripe doesn't include any sort of HMAC in the webhooks, I can't trust the data in the payload. So, my application just takes the event ID from the webhook payload and fetches the event from the Stripe API:

If security is a concern, or if it's important to confirm that Stripe sent the webhook, you should only use the ID sent in your webhook and should request the remaining details from the API directly.

This will obviously not work if I am trying to inject fake events for my test (by design).

What are the best practices for testing this sort of scenario?

pkaeding
  • 36,513
  • 30
  • 103
  • 141
  • 2
    I would say to not verify the event while you are testing this flow? What I do myself is retrieve the event from the API in Live mode but in test mode I just "trust" the payload since I'm the one sending it (and it's Test mode). – koopajah Apr 24 '15 at 12:21
  • @koopajah thanks, I suppose that is the way to go. If you'd like to post that as an answer, I will mark it as 'accepted'. – pkaeding Apr 27 '15 at 17:26
  • Not sure it's a really great answer! Happy to have you answer your own question with what you tried maybe? :) – koopajah Apr 27 '15 at 18:17
  • 1
    Further reading: http://blog.launchdarkly.com/best-practices-for-testing-stripe-webhook-event-processing/ – ptim Feb 02 '18 at 07:37
  • @ptim heh, that is actually my blog post, that I wrote up after figuring all this out :) – pkaeding Feb 02 '18 at 19:36
  • 2
    Ha! Awesome, very helpful, thanks :) – ptim Feb 02 '18 at 22:55
  • [This](https://stackoverflow.com/a/68191847/3411787) helped me. – Mohammad Zaid Pathan Sep 28 '22 at 20:14

3 Answers3

2

It seems there isn't a perfect way to do this. As suggested by @koopajah in a comment, I added a configuration value in my application that will disable fetching the event from Stripe, and instead just trust the event data in the webhook. This allows me to test my flow in almost the same way as it would work on production, since the event data in the webhook and the event fetched from Stripe are identical (assuming it is an authentic webhook request :)

Unless/until Stripe includes an HMAC signature in the webhook request to authenticate that it came from them, I think this is the best way to solve the problem.

Community
  • 1
  • 1
pkaeding
  • 36,513
  • 30
  • 103
  • 141
  • i am also using stripe webhook for payment success(recurring payment), but how i can test if subscribed user card expire, card have empty funds? what will be my hook response? i searched on stripe documentation but i didn't find any this type testing. – shazim ali Jan 24 '21 at 05:08
  • @shazimali You should listen out for the invoice.payment_failed event, it doesn't matter what reason as Stripe will return the reason in the error code. – James B Feb 17 '21 at 09:46
  • @shazimali, I put an expiration date in my user-access document (no SQL) and let the invoice.paid renew it. I don't have a high ticket subscription and can just give my user a free month if something goes wrong. Nothing has yet. – Jordan Aug 23 '21 at 00:37
0

One hour is a long time to wait, since I am trying to do this as part of an automated integration test suite.

You can shorten the wait by going to the invoice and selecting the "Charge customer" button, as shown below.

enter image description here

aaronbartell
  • 1,010
  • 1
  • 12
  • 19
0

I know this question is old but I faced the same question and found the test clock feature of Stripe https://stripe.com/docs/billing/testing/test-clocks

Just in case anyone is in the same situation.

black-hawk
  • 151
  • 1
  • 5