I am doing a Stripe Checkout payment.
I’ve created a Stripe Session so the user can fill his information:
$session = StripeCheckoutSession::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 = StripeCheckoutSession::retrieve($session_id);
After that, I want to retrieve Customer info to store in database. So I do:
$customer = StripeCustomer::retrieve($checkout_session->customer);
Here is the problem. Stripe is returning an error:
StripeCustomer instance has invalid ID: StripeGetOrder.php(34): StripeCustomer::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
3
Answers
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.
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 actuallynull
? In other words, doesecho $checkout_session->customer
return a Customer ID?If it turns out that Customer is indeed
null
and you are not settingmode=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 thesuccess_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.
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.