Stripe "No such customer": the 5 causes (test vs live mode is #1) and the fix that does not open your billing
Your Stripe call fails with: StripeInvalidRequestError: No such customer: 'cus_Nxxxxxxxxxxxx' Checkout works for you, then breaks for someone else, or works locally and fails in production. The error is clear. T
Your Stripe call fails with:
StripeInvalidRequestError: No such customer: 'cus_Nxxxxxxxxxxxx'
Checkout works for you, then breaks for someone else, or works locally and fails in production. The error is clear. The usual cause is not a typo, and one common fix opens a payment bug.
What it means
Stripe looked for a customer with that ID in the account and mode your API key belongs to, and found none. The ID may be real, but it is not in this place.
The 5 usual causes
-
Test mode vs live mode mix. A
cus_ID created with a test key does not exist in live mode, and the reverse. This is the number one cause after launch. - Different Stripe account. The deployed app uses a key from another account (or a Connect account) than the one that created the customer.
- Customer was deleted. Your database still stores the old ID.
- Copied test data into production. A database dump brought test customer IDs along.
-
Connect: the customer lives on the connected account, and the request is missing the
Stripe-Accountheader.
The fix, in order
- Check the prefix of your secret key:
sk_test_orsk_live_. Make sure it matches the mode where the customer was created. - In the Stripe dashboard, switch the test/live toggle and search the customer ID.
- Confirm the key belongs to the same account that created the customer.
- If the customer is gone, create a new one and update your stored ID. Handle the "missing" case in code instead of assuming the ID is valid.
- Never trust a customer ID sent from the browser. Look up the customer on the server from the signed-in user.
The shortcut that creates a bigger bug
Some quick fixes read the customer ID from the request body or URL "so it always works". Then any user can send someone else's ID and open their billing portal or see their invoices. Always map the signed-in user to their Stripe customer on the server.
The other shortcut is putting your secret key (sk_live_...) in frontend code to make calls from the browser. A secret key in a public bundle lets anyone act on your Stripe account. If that ever happened, roll the key in the dashboard now.
Check what your site ships
The free read-only check at https://proofforge.live looks at your deployed JavaScript for leaked keys, including Stripe secret keys, and lists what it could not test. To check that one user cannot read another's data, use the free two-account test: https://proofforge.live/two-account-test
Paid one-time fix reports are $9, $29 and $79, and the free scan does not require them.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes â full credit and traffic to the original publisher.