最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to retrieve Stripe customer_id at checkout session creation? - Stack Overflow

programmeradmin4浏览0评论

According to Stripe documentation

  1. First creating a checkout session with $stripe->checkout->sessions->create(). But at this point we don't have customer_id.

  2. Then embedded checkout is triggered and customer pays.

  3. Then after successful payment we get session_id on success page. Now we are able to retrieve customer_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

  1. First creating a checkout session with $stripe->checkout->sessions->create(). But at this point we don't have customer_id.

  2. Then embedded checkout is triggered and customer pays.

  3. Then after successful payment we get session_id on success page. Now we are able to retrieve customer_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?

Share Improve this question edited Feb 7 at 17:35 XIMRX asked Feb 7 at 16:09 XIMRXXIMRX 2,1723 gold badges32 silver badges61 bronze badges 3
  • Which webhook are you listening to? – Prerak Sola Commented Feb 7 at 19:48
  • customer.subscription.created, customer.subscription.updated, and customer.subscription.deleted – XIMRX Commented 2 days ago
  • Maybe what you can try is, to listen for the checkout.session.completed webhook which contains client_reference_id and customer_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 the customer.subscription.created webhook. For the customer.subscription.updated and customer.subscription.deleted webhook, you get the custtomer_id, so you can find from your db, to which user id it belongs to. – Prerak Sola Commented yesterday
Add a comment  | 

2 Answers 2

Reset to default 1

Obviously 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.

发布评论

评论列表(0)

  1. 暂无评论