stripe-payments

How to retrieve Stripe customer_id at checkout session creation?


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?


Solution

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