#demonshadow90
1 messages · Page 1 of 1 (latest)
Hi! Let me help you with this.
Hey vanya 🙂
I would also suggest double checking the webhook secret
Yes, i've checked that already!
I think the issue might be becuase using nuxt 3 server event, we have to use a composable:
const body = await readBody(event);
this converts it to a javascript object, and then I called JSON.stringify(body) to convert it back to string...
maybe somewhere there its causing the issue?
I don't know how nuxt works, but there must be a way not to parse the body. Stringifying it back will likely fail.
yeah thats my issue, googled out can't find anything
it all points to getting the body using readBody()
Could you please provide the whole webhook handler?
import Stripe from "stripe";
export default defineEventHandler(async (event) => {
console.log("SERVER API CALLED: api/stripe/webhook44aagg.post");
console.log(event);
try {
const config = useRuntimeConfig();
const body = await readBody(event);
const signature = getRequestHeader(event, "stripe-signature");
if (!body || !signature) {
return {
success: false,
error: "There was an error with the webhook.",
};
}
const stripe = new Stripe(config.stripeSecret, {
apiVersion: "2023-08-16",
});
let eventDetails;
try {
eventDetails = stripe.webhooks.constructEvent(
JSON.stringify(body),
signature,
config.stripeSigningKey
);
} catch (err) {
console.log("event error", err);
}
console.log("eventDetails", eventDetails);
return {
success: true,
};
} catch (err) {
return {
success: false,
error: "There was an error running the API request.",
errorData: err,
};
}
});
Where is the readBody method from?
its part of the nuxt server:
https://nuxt.com/docs/guide/directory-structure/server#handling-requests-with-body
I would suggest reaching out to nuxt developer. I don't know how this framework works unfortunately.