#demonshadow90

1 messages · Page 1 of 1 (latest)

heady sirenBOT
keen sun
#

Hi! Let me help you with this.

acoustic scaffold
#

Hey vanya 🙂

keen sun
#

I would also suggest double checking the webhook secret

acoustic scaffold
#

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?

keen sun
#

I don't know how nuxt works, but there must be a way not to parse the body. Stringifying it back will likely fail.

acoustic scaffold
#

yeah thats my issue, googled out can't find anything

#

it all points to getting the body using readBody()

keen sun
#

Could you please provide the whole webhook handler?

acoustic scaffold
#
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,
    };
  }
});
keen sun
#

Where is the readBody method from?

acoustic scaffold
keen sun
#

I would suggest reaching out to nuxt developer. I don't know how this framework works unfortunately.