Next.js Discord

Discord Forum

Stripe webhook error

Unanswered
Mallow bee posted this in #help-forum
Open in Discord
Mallow beeOP
So i'm getting this error in my webhooks from stripe, idk why i get this error:
Webhook Error: Webhook payload must be provided as a string or a Buffer (https://nodejs.org/api/buffer.html) instance representing the _raw_ request body.Payload was provided as a parsed JavaScript object instead. 
Signature verification is impossible without access to the original signed material.



export async function POST(req) {
  const sig = req.headers.get("stripe-signature");
  const json = await req.json();

  let event;

  try {
    event = stripeClient.webhooks.constructEvent(json, sig, webhookSecret);
  } catch (error) {
    console.log(`Webhook Error: ${error.message}`);
    return NextResponse.json(
      { received: false, status: "bad" },
      { status: 400 }
    );
  }

  switch (event.type) {
    case "checkout.session.completed": {
      // get metadata from event
      //   const { user_discord_id, swipes_to_add } = event.data.object.metadata;

      console.log("META_DATA" + event.data.object.metadata ?? "NO META DATA");
    }
    default:
      console.log(`Unhandled event type ${event.type}`);
  }

  return NextResponse.json({ received: true, status: "ok" }, { status: 200 });
}

2 Replies

British Shorthair
Replace:

const json = await req.json()
with:

const json = await req.text()