According to Stripe documentation
First creating a checkout session with
$stripe->checkout->sessions->create()
. But at this point we don't have customer_id.Then embedded checkout is triggered and customer pays.
Then after successful payment we get
session_id
on success page. Now we are able to retrievecustomer_id
.
At step 3 we are able get customer_id
but the webhooks are already fired before success page load. And at the webhook trigger time we don't have customer_id
to identify for which customer this customer_id
mentioned in webhook is about.
$stripe->checkout->sessions->create()
allows client_reference_id
parameter but webhook data don't return this field.
How can we get customer_id
of new customer before webhook is triggered?
According to Stripe documentation
First creating a checkout session with
$stripe->checkout->sessions->create()
. But at this point we don't have customer_id.Then embedded checkout is triggered and customer pays.
Then after successful payment we get
session_id
on success page. Now we are able to retrievecustomer_id
.
At step 3 we are able get customer_id
but the webhooks are already fired before success page load. And at the webhook trigger time we don't have customer_id
to identify for which customer this customer_id
mentioned in webhook is about.
$stripe->checkout->sessions->create()
allows client_reference_id
parameter but webhook data don't return this field.
How can we get customer_id
of new customer before webhook is triggered?
2 Answers
Reset to default 1Obviously I don’t know why you would need the Customer ID at Checkout Session creation. But from what I know about Stripe checkout is that the earliest a customer gets created during a Checkout Session is at successful completion of the session. With that there won’t be a way to retrieve the Customer ID earlier.
The checkout.session.completed
event sent by Stripe does include the customer_id in the payload, to reference Checkout Session to the Customer, so you do have access to it when the webhook is triggered.
If you need to pass additional data (like a user ID) to reference Checkout Session or Customer to your otherwise used systems, consider using the metadata
field instead of client_reference_id
, as this data in the metadata object will be included in the webhook event. This allows you to reliably associate the event with the correct customer.
If you need a customer.id
already at Checkout creation time, then I would recommend creating a Customer through the API and then passing the customer.id
of the newly created Customer in the Checkout Session.
The webhook events you are mentioning now are not webhook events for the Checkout Session, but for the Subscription. I assumed you were talking about Checkout Session events previously. The webhook events for the Checkout Session would also include the customer.id
.
With that being said, you can pass in additional information about the Customer not in the Session’s metadata
object, but into the subscription_data.metadata
object. This specific metadata
object will be added to the corresponding Subscription object and with that will also be available in the webhook events you mentioned.
customer.subscription.created
,customer.subscription.updated
, andcustomer.subscription.deleted
– XIMRX Commented 2 days agocheckout.session.completed
webhook which containsclient_reference_id
andcustomer_id
. So, at this time, you can map the customer id with your system's user id and store it in your db. Then do what you are doing right now for thecustomer.subscription.created
webhook. For thecustomer.subscription.updated
andcustomer.subscription.deleted
webhook, you get thecusttomer_id
, so you can find from your db, to which user id it belongs to. – Prerak Sola Commented yesterday