Skip to content

Instantly share code, notes, and snippets.

@alexFaunt
Last active April 12, 2019 00:44
Show Gist options
  • Save alexFaunt/16c9b5381ea3fb8fe8237681081052db to your computer and use it in GitHub Desktop.
Save alexFaunt/16c9b5381ea3fb8fe8237681081052db to your computer and use it in GitHub Desktop.
Example JS to verify an app store receipt
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