Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe CLI triggering random events instead of payment_intent.succeeded

This is my first time testing with Stripe and I can't wrap my head around this issue that I'm having. I copied the code for Node/Express from this link (I just changed the port to 3000 and removed the bodyParser since I had already set it up with app.use(bodyParser.json())) and I successfully logged in to my account typing

stripe login

in the CLI. After that, I set up my endpoint for webhook listening with

stripe listen --forward-to http://localhost:3000/webhook

After everything seemed to be set up, I started testing, but I noticed somewhat of a random behaviour in the Stripe CLI: every time I tried triggering a payment intent success with

stripe trigger payment_intent.succeeded

I got a different result, as shown below:

2020-07-04 14:25:32   --> charge.succeeded [evt_1H1AQxH9PqQvGDtCJ7ShdAQE]
2020-07-04 14:25:32  <--  [400] POST http://localhost:3000/webhook [evt_1H1AQxH9PqQvGDtCJ7ShdAQE]
2020-07-04 14:25:34   --> payment_intent.succeeded [evt_1H1AQzH9PqQvGDtCl8ajm7po]
2020-07-04 14:25:34  <--  [200] POST http://localhost:3000/webhook [evt_1H1AQzH9PqQvGDtCl8ajm7po]
2020-07-04 14:25:34   --> payment_intent.created [evt_1H1AR0H9PqQvGDtC1xty9l6R]
2020-07-04 14:25:34  <--  [400] POST http://localhost:3000/webhook [evt_1H1AR0H9PqQvGDtC1xty9l6R]
2020-07-04 14:28:46   --> charge.succeeded [evt_1H1AU5H9PqQvGDtCHJ95Jb7H]
2020-07-04 14:28:46  <--  [400] POST http://localhost:3000/webhook [evt_1H1AU5H9PqQvGDtCHJ95Jb7H]

I never triggered charge.succeeded nor payment_intent.created, but despite this they showed up in the CLI (as 400 errors) instead of the normal 200 payment_intent.succeeded. I tried console logging the event.type used by the switch statement in the Stripe Docs example above and it indeed showed that the events were charge.succeeded and payment_intent.created.

Now my question is: is this some sort of bug which hasn't been already fixed or am I doing something wrong? And maybe could anyone help me out with this?

Thanks in advance for your time :)

like image 560
LuckeeDev Avatar asked Jul 04 '20 13:07

LuckeeDev


1 Answers

Answer from one of Stripe's dev:


When you trigger an event with the Stripe CLI, under the hood it's making all of the requisite API methods to result in that event firing. In this case, it is creating and confirming a PaymentIntent.

To get a resulting payment_intent.succeeded event, you'll need to create a PaymentIntent (payment_intent.created will fire for this one). Then you need to confirm the payment intent and actually collect payment (which results in: charge.created, charge.succeeded, and payment_intent.succeeded).

For other event types like checkout.session.completed, many other events will fire that represent the objects that are prerequisite to get to a valid checkout.session.completed event.

If you only want to forward the payment_intent.succeeded event locally, (and it's a good idea when in production to enable only selected event types for your webhook endpoint), then you might want to pass the -e argument to stripe listen with the comma separated list of specific events you want to listen for. In this case you might update your listen command to:

stripe listen --forward-to http://localhost:3000/webhook -e payment_intent.succeeded

Hope that helps. If you run into any issues, feel free to reach out to the team at support.stripe.com/contact or on IRC in the #stripe channel on Freenode.


I hope it helps somebody as much as it helped me!

like image 196
LuckeeDev Avatar answered Nov 13 '22 09:11

LuckeeDev