I’m using Revenue Cat for in app purchase and I’m trying to forward ios transactions from Revenue Cat to my local server.
I am receiving the payload from revenue cat, but when I try to sign it with my .p8 private key from apple I get the error:
Error verifying signature: JsonWebTokenError: invalid signature
I can verify its the correct key because its the same one I uploaded to revenueCat and its working there.
In my code I am using the key directly in a string. Do I need to read it with filesystem instead? Here is what I have.
import { NextApiRequest, NextApiResponse } from 'next'
import * as jwt from 'jsonwebtoken'
import fs from 'fs'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const signedPayload = req.body.signedPayload
if (!signedPayload) {
return res
.status(400)
.json({ error: 'Signed payload not found in request body' })
}
const privateKey = `-----BEGIN PRIVATE KEY-----
***mykey***
***mykey***
***mykey***
-----END PRIVATE KEY-----`
let decodedPayload
try {
decodedPayload = jwt.verify(signedPayload, privateKey)
} catch (error) {
console.error('Error verifying signature:', error)
return res.status(403).json({ error: 'Invalid signature' })
}
console.log(decodedPayload)
res.status(200).json({ message: 'Webhook received successfully' })
}
I’ve tried using the key without the beginning and ending tags. ie removing this part of the string. —–BEGIN PRIVATE KEY—– and —–END PRIVATE KEY—–
Here are the docs from revenueCat. https://www.revenuecat.com/docs/platform-resources/server-notifications/apple-server-notifications
From apple all I can find is this documentation. https://developer.apple.com/documentation/storekit/in-app_purchase/original_api_for_in-app_purchase/subscriptions_and_offers/enabling_app_store_server_notifications
Any help is appreciated.