Fixing React Native In-App Purchases That Validate Fine in Sandbox but Fail Silently in Production
A react-native-iap purchase flow works perfectly in TestFlight and sandbox testing — the receipt validates, the entitlement unlocks, everything looks solid. Then it ships, and real paying users get charged but never unlocked, with no error visible anywhere on the client. The cause almost always sits in how the backend talks to Apple's two separate receipt-verification environments.
The Problem
A react-native-iap purchase completes successfully on the device: the system payment sheet confirms, the transaction finishes, and the app receives a receipt it sends off for server-side validation. In TestFlight and local sandbox testing, that validation call comes back valid every time and the purchased content unlocks exactly as expected. After the app ships to the App Store, a slice of real paying customers go through the identical flow, Apple processes the charge, but the backend's validation call fails — the client either shows a generic error or, worse, shows nothing at all while silently never unlocking the entitlement the user already paid for.
Why It Happens
Apple runs two separate receipt-verification endpoints, and a production receipt sent to the wrong one is rejected outright
Apple's legacy verifyReceipt endpoint (and the newer App Store Server API follow the same split) exposes a sandbox URL and a production URL. A receipt generated by a sandbox or TestFlight purchase is only ever valid against the sandbox endpoint; a receipt generated by a real App Store purchase is only ever valid against the production endpoint. Sending either receipt to the wrong endpoint doesn't produce a vague error — Apple returns a specific status code telling the caller it hit the wrong environment, and if that status code isn't handled, the validation call simply reports failure.
The backend was built and tested entirely against the sandbox endpoint, so the production fallback path was never actually exercised
Apple's documented pattern is to always call the production endpoint first, and only retry against sandbox if the response comes back with status 21007 ("this receipt is from the test environment"). It's easy to build and test the opposite during development — hardcoding the sandbox URL because every test purchase during development is a sandbox purchase — and have that hardcoded URL never get caught, because every build before release is tested exclusively with sandbox or TestFlight accounts, which are the one case where the sandbox-only endpoint happens to work.
A TestFlight build uses the sandbox payment environment even though it's distributed through the same mechanism as a production release, hiding the gap until real launch
TestFlight builds, including external TestFlight testing with real App Store accounts, process purchases through Apple's sandbox regardless of how production-like the rest of the release pipeline looks. This means a full TestFlight beta cycle — the stage most teams treat as their final pre-launch validation — never actually sends a single production receipt through the validation code, so a production-endpoint bug has no chance to surface before the app is live and processing real charges.
The Fix
1. Always call the production verification endpoint first, and fall back to sandbox only on status 21007
async function verifyReceipt(receiptData, sharedSecret) {
const body = {
"receipt-data": receiptData,
"password": sharedSecret,
"exclude-old-transactions": true,
};
const prodRes = await fetch(
"https://buy.itunes.apple.com/verifyReceipt",
{ method: "POST", body: JSON.stringify(body) }
).then(r => r.json());
if (prodRes.status === 21007) {
// Receipt is from the sandbox environment, retry there
return fetch(
"https://sandbox.itunes.apple.com/verifyReceipt",
{ method: "POST", body: JSON.stringify(body) }
).then(r => r.json());
}
return prodRes;
}
This is the documented, mandatory pattern — production receipts are rejected by sandbox, and the only status code that safely tells the caller to retry against sandbox is 21007. A backend that hardcodes either URL, or that checks for a different status code before retrying, will reject some fraction of otherwise-legitimate production receipts.
2. Treat status 21002 and 21003 as retryable transient failures, not permanent rejections
21002 - The data in the receipt-data property was malformed
or the service experienced a temporary issue. Retry.
21003 - The receipt could not be authenticated. Retry once
before treating as a real failure.
21007 - Sandbox receipt sent to production. Retry on sandbox.
21008 - Production receipt sent to sandbox. Retry on production.
Apple's own status codes document several as safe to retry rather than fail outright, but it's common for a validation handler written against a narrow set of test cases to treat any non-zero status as a hard failure. A user whose purchase gets permanently denied because of a transient 21002 during an Apple-side hiccup has no way to recover the entitlement without contacting support, even though the charge already succeeded.
3. Log the raw Apple response status and environment on every validation call, not just a pass/fail boolean
logger.info("iap_verify_attempt", {
userId,
endpointTried: "production",
appleStatus: prodRes.status,
environment: prodRes.environment, // "Production" | "Sandbox"
bundleId: prodRes.receipt?.bundle_id,
});
Without the actual Apple status code and environment field logged per attempt, a production validation failure is indistinguishable from a network error, a malformed receipt, or a genuinely fraudulent purchase — all of which need different fixes. Logging only "validation failed" turns every real incident into a fresh investigation starting from zero.
4. Test the production endpoint path deliberately before relying on TestFlight as the final check
- Submit a build to App Review and test with a live,
released version from the App Store (even a tightly
limited phased release) before assuming TestFlight
coverage is sufficient
- Alternatively, use Apple's StoreKit Testing in Xcode
with a local StoreKit configuration file, which can
simulate production-shaped responses independent of
the sandbox/production endpoint split entirely
Since TestFlight purchases never exercise the production verification endpoint, a team relying on TestFlight as its sole pre-launch signal is structurally unable to catch this class of bug before real users hit it. A deliberate first-release smoke test against the live App Store build, even a small phased rollout, closes that gap.
Why This Works
The production-first-with-sandbox-fallback pattern isn't a workaround — it's the exact flow Apple's own documentation specifies, and it's the only sequence that correctly handles both environments without needing to know in advance which one a given receipt came from. Treating transient status codes as retryable, rather than as hard failures, prevents legitimate paying users from losing access to something they already bought because of a momentary Apple-side issue. And logging the actual status and environment on every attempt turns a vague "it doesn't work for some users" report into a status code that points directly at the cause.
Conclusion
In-app purchase validation that passes every sandbox and TestFlight test but fails for real paying users almost always traces back to the same root cause: the backend was only ever exercised against Apple's sandbox verification endpoint, and the production fallback logic — call production first, retry on sandbox only for status 21007 — was never actually run until real purchases hit it. Implementing that exact sequence, treating transient status codes as retryable, and logging the raw Apple response on every attempt turns a silent revenue-losing bug into one that's visible, diagnosable, and doesn't require a TestFlight cycle to catch again.
