I’m trying to receive notifications on my server from Apple for in app purchase transactions.
I found one SO question with an implementation
Appstore server notification set up [Receiving App Store Server Notifications version2],
but it seems apple has created a library to make this easier. Here are the GitHub and docs for server library :
Github: https://github.com/apple/app-store-server-library-node
Docs: https://developer.apple.com/documentation/appstoreservernotifications/enabling_app_store_server_notifications
My specific problem is that when I run the line
const verifiedNotification =
await signedDataVerifier.verifyAndDecodeNotification(signedPayload)
console.log('data:', verifiedNotification.data)
It doesn’t throw an error, but it also doesn’t decode the signedPayload. I still get something like this when I log the verifiedNotification:
ZDJSeVp6WXVaR1Z5TURFR0NDc0dBUVVGQnpBQmhpVm9kSFJ3T2k4dmIyTnpjQzVoY0hCc1pTNWpiMjB2YjJOemNEQXpMWGQzWkhKbk5qQXlNSUlCSGdZRFZSMGdCSUlCRlRDQ0FSRXdnZ0VOQmdvcWhraUc5Mk5rQlFZQk1JSCtNSUhEQmdnckJnRUZCUWNDQWpDQnRneUJzMUpsYkdsaGJtTmxJRzl1SUhSb2FYTWdZMlZ5ZEdsbWFXTmhkR1VnWW5rZ1lXNTVJSEJoY25SNUlHRnpjM1Z0WlhNZ1lXTmpaWEIwWVc1alpTQnZaaUIwYUdVZ2RHaGxiaUJoY0hCc2FXTmhZbXhsSUhOMFlXNWtZWEprSUhSbGNtMXpJR0Z1WkNCamIyNWthWFJwYjI
My implementation is in a next.js api route. I can verify I am receiving the signedPayload from apple. Here is my full implementation:
import {
Environment,
SignedDataVerifier
} from '@apple/app-store-server-library'
import fs from 'fs'
import path from 'path'
export default async function handler(req, res) {
const signedPayload = req.body.signedPayload
//app data
const bundleId = 'com.myappname.www'
const environment = Environment.SANDBOX
const enableOnlineChecks = true
const appAppleId = **********
//join the certifications required from apple, could order here matter?
const rootCAs = [
fs.readFileSync(
path.join(
process.cwd(),
'src/pages/api/webhooks/AppleComputerRootCertificate.cer'
)
),
fs.readFileSync(
path.join(
process.cwd(),
'src/pages/api/webhooks/AppleIncRootCertificate.cer'
)
),
fs.readFileSync(
path.join(process.cwd(), 'src/pages/api/webhooks/AppleRootCA-G2.cer')
),
fs.readFileSync(
path.join(process.cwd(), 'src/pages/api/webhooks/AppleRootCA-G3.cer')
)
]
try {
const signedDataVerifier = new SignedDataVerifier(
rootCAs,
enableOnlineChecks,
environment,
bundleId,
appAppleId
)
const verifiedNotification =
await signedDataVerifier.verifyAndDecodeNotification(signedPayload)
console.log('data:', verifiedNotification.data)
res.status(200).json({ message: 'Notification processed successfully' })
} catch (error) {
console.error('Error processing notification:', error)
res.status(500).json({ error: 'Error processing notification' })
}
}
Any ideas? thanks for help.