#yuyu_webhooks

1 messages · Page 1 of 1 (latest)

proven parrotBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1280722240375226480

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

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.

mellow bridge
#

There was another guy helping me

#

but okay

#

uhhh last time we came to the conclusion that there was an error on my server but It wasnt that I figured out what it was, its something with the signature but Im not too sure what to do

#
const stripe = require("stripe")(process.env.STRIPE_PRIVATE_TEST);
const endpointSecret = process.env.STRIPE_WEBHOOK_TEST;

app.post(
  "/stripe_webhook",
  express.raw({ type: "application/json" }),
  async (request, response) => {
    const sig = request.headers["stripe-signature"];

    let event;

    try {
      // mock webhook events
      /*const payload = {
        id: "evt_test_webhook",
        object: "event",
      };

      const payloadString = JSON.stringify(payload, null, 2);
      const secret = "whsec_test_secret";

      const header = stripe.webhooks.generateTestHeaderString({
        payload: payloadString,
        secret,
      });

      event = stripe.webhooks.constructEvent(payloadString, header, secret);*/

      event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
    } catch (err) {
      console.log(`Webhook Error: ${err.message}`);
      response.status(400).send(`Webhook Error: ${err.message}`);
      return;
    }

    //console.log(event);

    switch (event.type) {
      case "checkout.session.completed":
        const checkoutSessionCompleted = event.data.object;
        console.log("Works");

        const userId = checkoutSessionCompleted.metadata.userId;
        let userProfile = await profileSchema.findOne({ _id: userId });
        if (userProfile) {
          const threeMonthsInMillis = 1000 * 60 * 60 * 24 * 90;
          const now = new Date();

          if (userProfile.premium) {
            const currentEndDate = new Date(userProfile.premiumEndDate);
            if (isNaN(currentEndDate)) {
              userProfile.premiumEndDate = new Date(
                now.getTime() + threeMonthsInMillis
              );
            } else {
              userProfile.premiumEndDate = new Date(
                Math.max(now.getTime(), currentEndDate.getTime()) +
                  threeMonthsInMillis
              );
            }
          } else {
            userProfile.premium = true;
            userProfile.premiumEndDate = new Date(
              now.getTime() + threeMonthsInMillis
            );
          }

          await userProfile.save();
        }
        break;
      default:
        console.log(`Unhandled event type ${event.type}`);
    }

    response.status(200).send("User Payed");
  }
);
#

this is my code and it logs console.log(Webhook Error: ${err.message});
but it doesnt go to case "checkout.session.completed":

#

This is what I keep getting:

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.

Learn more about webhook signing and explore webhook integration examples for various frameworks at https://github.com/stripe/stripe-node#webhook-signing

GitHub

Node.js library for the Stripe API. . Contribute to stripe/stripe-node development by creating an account on GitHub.

hard jasper
#

Did you have middleware elseware before this path that parses the request into another format? For example, app.use(express.json()) prior to your route

mellow bridge
#

Yes I have that above

hard jasper
#

That is the issue

#

Node will parse the request into JSON first before entering your webhook route

mellow bridge
#

oh

hard jasper
#

So the request is no longer in raw form

mellow bridge
#

so like this?

hard jasper
#

Can you try if this works?

mellow bridge
#

sure

#

deploy takes a minute

hard jasper
#

You have two app.use(express.json()) in your code

#

Please make sure both are placed after the route declaration

hard jasper
mellow bridge
#

Same 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.

Learn more about webhook signing and explore webhook integration examples for various frameworks at https://github.com/stripe/stripe-node#webhook-signing

GitHub

Node.js library for the Stripe API. . Contribute to stripe/stripe-node development by creating an account on GitHub.

hard jasper
#

Did you place both app.use(express.json()) after the route?

#

Or just one?

mellow bridge
#

oh mb

#

just one

#

I just commented the limit one out

hard jasper
#

How about commenting both out first to check if app.use(express.json()) is the cause of the problem?

mellow bridge
#

okay let me try that

#

oh its up

#

let me test it rq

#

WORKS

#

OMG

hard jasper
#

YAY!

#

Great to hear that it works!

mellow bridge
#

Dude i have been messing around with this the entire day omg

#

thank you so much!!!

hard jasper
#

No problem! Happy to help 😄

mellow bridge
#

Question

#

can I have only one express.jeson?

#

app.use(express.json({ limit: "50mb" }));

#

like this one instead of the other one?

hard jasper
#

Yup, one will be fine