charge.succeeded event

36 views
Skip to first unread message

Bob Lund

unread,
Jul 4, 2022, 1:05:10 PM7/4/22
to Stripe API Discussion
I want to use the charge.succeeded tripper to capture when a customer payment succeeds. I've created the webhook endpoint. The docs show a "customer" property in the event (nodejs). I tested with:

stripe trigger charge.succeeded --add charge.succeeded:customer=cus_XXXX

The webhook endpoint gets the charge.succeeded event but event.data.object.customer is null.

I see a charge made for cus_XXXX on the stripe console.

I need the customer ID to update the payment state in my app. How do I do that?

Thanks




Remi J.

unread,
Jul 4, 2022, 1:11:31 PM7/4/22
to api-d...@lists.stripe.com
Hello Bob,

The CLI supports the options `--add` or `--override`. Both of those expect the name of the resource/API you're setting the override on so `--add charge:customer=cus_XXX` is what you want. The problem is that this won't work for `charge.succeeded`. This event is specifically triggered by using a fixture defined here [1]. That fixture uses the Create Charge API [2] but passes the `source: 'tok_visa'` parameter. You can't send a Token id if you're also passing a Customer id at the same time. So what you need to do here is override both to pass both the customer id and its card id (card_123) that you would have set up first and the command would look like this:

$ stripe trigger charge.succeeded --override charge:customer=cus_ABC --override charge:source=card_123

While this works, I would recommend not using this. Instead use the PaymentIntents API which is the new default integration path and listen to the `payment_intent.succeeded` event. You can easily trigger it like this:

$ stripe trigger payment_intent.succeeded --add payment_intent:customer=cus_ABC

Hope this helps!
Best,
Remi


--
To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss...@lists.stripe.com.

Bob Lund

unread,
Jul 4, 2022, 1:45:22 PM7/4/22
to api-d...@lists.stripe.com
Hi Remi,

Many thanks for the quick response and suggestion. Is there a chance that the charge won't succeed given a payment_intent.succeeded event?

Bob




To unsubscribe from this topic, visit https://groups.google.com/a/lists.stripe.com/d/topic/api-discuss/AUUy8UvqljE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to api-discuss...@lists.stripe.com.

Remi J.

unread,
Jul 4, 2022, 1:47:15 PM7/4/22
to api-d...@lists.stripe.com
Hello,

No this could never happen. When a PaymentIntent has `status: 'succeeded'` it indicates that there's an underlying Charge that succeeded at that point. I'd recommend reading https://stripe.com/docs/payments/payment-intents which covers the state machine in details!

Best,
Remi

Bob Lund

unread,
Jul 4, 2022, 1:54:34 PM7/4/22
to api-d...@lists.stripe.com
Hi Remi,

One last question. Does the payment_intent.succeeded event in a live setting automatically include the custom ID in the customer property?

Thanks,
Bob

Remi J.

unread,
Jul 4, 2022, 1:57:26 PM7/4/22
to api-d...@lists.stripe.com
If the PaymentIntent or the Charge are associated with a Customer then `customer: 'cus_123'` will be set as expected otherwise it will be `customer: null`. This works exactly the same way in Live and Test mode.

The CLI works the same way too since it uses real API requests under the hood to trigger those events. It's just that the fixtures associated with those events don't create a Customer first for simplicity. If you write code that first creates a Customer and then passes that customer id in the `customer` parameter then the events will have the `customer` property set.

I'd recommend testing all of this in Test mode with code yourself. The `trigger` command for the CLI is really here just to quickly test your webhook handler.

Best,
Remi
Reply all
Reply to author
Forward
0 new messages