Drupal 8 Commerce: Fixing 'No Such Customer' error on checkout
Recently I was experiencing an issue on the Drupal 8 website I’m working on,
where a small number of users were not able to complete the checkout process and
instead got a generic The site has encountered an unexpected error message.
Looking at the log, I was able to see the error being thrown (the customer ID
has been redacted):
Stripe\Error\InvalidRequest: No such customer: cus_xxxxxxxxxxxxxx in
Stripe\ApiRequestor::_specificAPIError() (line 124 of
/var/www/vendor/stripe/stripe-php/lib/ApiRequestor.php).
Logging in to the Stripe account, I was able to confirm that the specified
customer ID did not exist. So where was it coming from, and why was Drupal
trying to retrieve a non-existent customer?
Investigation
After some investigation, I found a table in the database named
user__commerce_remote_id which stores the remote customer ID for each payment
method (again, the customer ID has been redacted).
The entity_id and revision_id values in this case refer to the user that the
Stripe customer has been associated with.
As there was no customer in Stripe with this ID, I think that this must be a
customer ID from the test environment (the data from which was deleted before
the site went live).
Drupal code
This I believe is the Drupal code where the error was being triggered:
After confirming that it was the correct user ID, simply removing that row from
the database allowed the new Stripe customer to be created and for the user to
check out successfully.
Was this useful?
About me
I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.