线上生产应用验证内购收据出现21007状态码的可能性及异常咨询
Absolutely, this does happen in live production apps—you’re not dealing with some obscure bug, this is a well-documented edge case that pops up for a small subset of users. Let’s break down why it occurs and how to fix it:
Why 21007 Shows Up in Production
The 21007 status code means you’re using Apple’s production validation endpoint to check a receipt generated in the sandbox environment. Here are the most common scenarios causing this in a live app:
- Sandbox test accounts in production: Some users (often testers, or people who obtained sandbox credentials) might be logged into the App Store with a sandbox test account but downloaded the official production version of your app. When they make an in-app purchase, Apple generates a sandbox receipt, which your production endpoint rejects with 21007.
- TestFlight carryover: Users who joined your TestFlight beta might forget to log out of their sandbox account before downloading the production app from the App Store. Their device could still route in-app purchase requests to the sandbox server, creating sandbox receipts.
- Device-level sandbox configuration: In rare cases, a user’s device might be set to use the sandbox environment (via developer tools or profile settings) even when running the production app build.
How to Resolve the Issue
Apple explicitly recommends handling this case gracefully. Here’s what you should do:
- When you receive a 21007 response, automatically retry the receipt validation using Apple’s sandbox endpoint instead of the production one. This will correctly validate the sandbox receipt, and the user won’t notice any behind-the-scenes adjustment.
- Don’t show error messages to users for this scenario. It’s a backend-only issue that doesn’t require user intervention.
- Log these occurrences in your backend analytics if you want to track how often this happens, but it’s typically a tiny percentage of your user base.
Adding this retry flow is a standard part of robust in-app purchase validation logic—once implemented, those edge-case users will have their purchases validated correctly without disruption.
内容的提问来源于stack exchange,提问作者Saurabh Singh Rathore

