Home > front end >  Stripe payment session has NULL customer field
Stripe payment session has NULL customer field

Time:10-04

I am doing a Stripe Checkout payment.

I've created a Stripe Session so the user can fill his information:

$session = \Stripe\Checkout\Session::create([ 
            ...
       ]);

Once he is redirected to the success page, I call a function that retrieve the Stripe Checkout Session with the session_id.

$checkout_session = \Stripe\Checkout\Session::retrieve($session_id);

After that, I want to retrieve Customer info to store in database. So I do:

$customer = \Stripe\Customer::retrieve($checkout_session->customer);

Here is the problem. Stripe is returning an error: Stripe\Customer instance has invalid ID: StripeGetOrder.php(34): Stripe\Customer::retrieve(NULL)

So, in Live mode of Stripe, $checkout_session->customer return NULL.

After I checked the documentation, creating a new Stripe Session should also create a new Customer with a new ID, so why the customer's ID is NULL when I retrieve my Session?

Thanks, Skyfrid

CodePudding user response:

Checkout will create a new Customer object based on information provided during the payment flow. The customer will only be created once the payment has been complete.

CodePudding user response:

It is possible for a Checkout Session to not create a Customer on success(for example: when mode=setup on the Checkout Session object). Assuming that is not the case: do you know that $checkout_session->customer is actually null? In other words, does echo $checkout_session->customer return a Customer ID?

If it turns out that Customer is indeed null and you are not setting mode=setup on the Checkout Session object, then it might be that either (a) the Customer retrieve call went through before the Checkout Session was completed by the customer, or (b) the payment was not actually successful and the customer was not redirected to the success_url after all.

Note: a Customer is only created after a payment is successful when using Checkout.

I would recommend finding that specific Checkout Session in the Stripe Dashboard logs (see here), so you can figure out when the retrieve call was made during the lifecycle of the session.

CodePudding user response:

Checkout will not create a Customer by default if one is not required. You have to set customer_creation: 'always' if you want to force a Customer to be created all the time. This is documented here.

  • Related