#rockywearsahat

1 messages · Page 1 of 1 (latest)

bitter epochBOT
#

Hello rockywearsahat, we'll be with you shortly! Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
rockywearsahat, 0 days ago, 31 messages
rockywearsahat, 0 days ago, 36 messages
rockywearsahat, 0 days ago, 9 messages

potent valley
#

Hello! This is for verifying webhook signatures, correct?

opal pelican
#

Hey, yea, I'm still working on creating a webhook to fufil products that are ordered through the site I'm building

#

I currently have the webhook setup to recieve events and if the method is a post request it will try {

#

sorry lol

potent valley
#

You need to provide the raw, unaltered, unparsed incoming HTTP body to verify the signature. When using Node there are a lot of things that can modify the body before you get to it, and there's not a one-size-fits-all solution. There are a lot of potential solutions here though: https://github.com/stripe/stripe-node/issues/341

opal pelican
#
try {
//This await req.json gets the body correctly but I get a StripeSignatureVerificationError saying the entered value has to be in some sort of string or buffer
const body = await req.json();

const headersList = headers(req);
const signature = headersList.get("stripe-signature");

event = stripe.webhooks.constructEvent(
  body,
  signature,
process.env.STRIPE_WEBHOOK_SIGNING_SECRET
);
} catch(err) {
...
}

can I just convert the json to a raw string and pass that data

#

?

potent valley
#

No.

#

The original whitespace and everyhing needs to remain intact.

bitter epochBOT
opal pelican
#

ok I see, so it can't be an object so I have to parse an array with the values in it, but I'm still really quite confused on how to do so, currently my req object looks like:

NextRequest [Request] {
  [Symbol(realm)]: { settingsObject: { baseUrl: undefined } },
  [Symbol(state)]: {
    method: 'POST',
    localURLsOnly: false,
    unsafeRequest: false,
    body: { stream: undefined, source: null, length: null },
...etc
}

How do I correctly store this in buffers/arrays so the whitespaces remain intact?

#

would my array be like a keyed array? ```js
[
"Symbol(realm)": [
"settingsObject": ["baseUrl","undefined"]
],
"Symbol(state)": [
["method", "post"],
["localUrlsOnly", "false"]
//...
]
]

leaden marlin
#

Hi @opal pelican I'm taking over this thread

opal pelican
#

Hey Jack I appreciate it, I have given up on life, I just spent the last hour or two trying to figure out how to get the raw body when it was quite literally as simple as this:

export async function POST(req) {
    if (req.method === "POST") {
      let event;

      try {
//await req.text() not req.json()
        const rawBody = await req.text();

//Get headers
        const headersList = headers(req);
        const signature = headersList.get("stripe-signature");

//Create event
        event = stripe.webhooks.constructEvent(
          rawBody,
          signature,
          process.env.STRIPE_WEBHOOK_SIGNING_SECRET
        );
      } catch (err) {
        console.log(err);
        return new Response(JSON.stringify({ status: 500, error: err }));
      }

//Log success/error msg
      if (event.type === "checkout.session.completed") {
        console.log("Webhook Recieved Payment");
      } else {
        console.warn(`Unhandled Event Type: ${event.type}`);
      }

      return new Response({ success: true });
    }
}

I appreciate all the help y'all provide! Thank you

#

spent multiple hours looking for res.text not res.json, if anyone else uses next let them know