Last active
April 12, 2019 00:44
-
-
Save alexFaunt/16c9b5381ea3fb8fe8237681081052db to your computer and use it in GitHub Desktop.
Example JS to verify an app store receipt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async verifyReceipt(encodedReceipt) { | |
const body = JSON.stringify({ | |
'receipt-data': encodedReceipt, | |
// Set in App Store Connect | |
password: process.env.APP_STORE_SECRET, | |
// This means Apple only returns the latest receipt info instead of all transactions. | |
'exclude-old-transactions': true, | |
}) | |
// Try to verify the receipt as if it's a real production receipt | |
const productionResponse = await fetch('https://buy.itunes.apple.com/verifyReceipt', { | |
method: 'POST', | |
body, | |
}) | |
// If it fails with status 21007 it means it's a Sandbox receipt | |
if (productionResponse.body.status === 21007) { | |
const sandboxResponse = await fetch('https://sandbox.itunes.apple.com/verifyReceipt', { | |
method: 'POST', | |
body, | |
}) | |
return sandboxResponse.body | |
} | |
return productionResponse.body | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment