I am trying to verify a Paddle webhook by creating a singled payload as described on this page:
https://developer.paddle.com/webhooks/signature-verification
The issue is that every time I try to get a RAW body from the request, I am getting the following error:
BACKEND: 2023-12-28T18:38:08.384Z error: An error occurred ->InternalServerError: stream is not readable
Here is my code:
import { NextApiRequest, NextApiResponse } from "next";
import crud from "../../crudWrapper";
import logger from "../../logger";
import { StatusCodes } from "http-status-codes";
import { authOptions } from "../../auth/[...nextauth]";
import { getServerSession } from "next-auth/next";
let getRawBody = require('raw-body')
/**
* POST Function to call a pecific webhook
* @param req - Next API route request object
* @param res - Next API route response
*/
const postFunc = async (req: NextApiRequest, res: NextApiResponse) => {
const session = await getServerSession(req, res, authOptions);
console.log(session)
try {
if (!req.body) return res.status(StatusCodes.BAD_REQUEST).json({ error: "No request body found" })
// Step 1. Pare PADDLE Header (i.e., paddle-signature)
const paddleSignature = req.headers['paddle-signature'];
const timeStamp = (paddleSignature as string).split(';')[0].split('=')[1]
const h1 = (paddleSignature as string).split(';')[1].split('=')[1]
// Step 2. Create a SIGNED PAYLOAD
const rawBody = await getRawBody(req);
const data = JSON.parse(Buffer.from(rawBody).toString('utf8'));
const signedPayload = timeStamp + ':' + rawBody;
switch (req.body.event_type) {
case "transaction.completed":
logger.info('User ' + session.user.email + " has successfully completed a transaction!")
break;
default:
break;
}
return res.status(StatusCodes.OK).json({ success: true });
} catch (e) {
logger.error("An error occurred ->" + e);
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
status: StatusCodes.INTERNAL_SERVER_ERROR,
message: e.message,
});
}
};
export default crud(null, postFunc, null, null);
Can someone please help me out, what am I doing wrong here?