Associate Stripe checkout session with subscription webhook

Viewed 1186

I'm using Stripe to handle payments for a subscription service I'm setting up.

I gather the relevant information from my customer, then on the server side, I use the Stripe PHP IDE to set up a new customer and create a checkout session for a price object which I've set up as a subscription. I save the checkout session ID to my database, then use that same session ID client side to take payment from the customer, via a redirect to Stripe.

The webhook checkout.session.completed, then lets me link up the previous checkout session ID with the subscription ID. Then I need the second webhook customer.subscription.updated to get the status of the subscription from the subscription id.

It feels like I'm doing something wrong here. I'm using two webhooks to get the information I need. If the checkout.session.completed webhook were to arrive after the customer.subscription.updated webhook, then my logic will fail.

Is there a better/correct way to manage this flow?

1 Answers

You only need checkout.session.completed here. That event indicates a successful Checkout and payment.

I would ignore the initial customer.subscription.updated event and instead, if you need that status, fetch the Subscription with https://stripe.com/docs/api/subscriptions/retrieve when you receive the checkout.session.completed event.

Related