#yuyu_webhooks

1 messages ¡ Page 1 of 1 (latest)

somber kettleBOT
#

👋 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/1280601209186811917

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

hoary finch
#
const stripe = require("stripe")(process.env.STRIPE_PRIVATE_KEY);
const endpointSecret = process.env.STRIPE_WEBHOOK_SECRET;

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

    let event;

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

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

        // Retrieve the user ID from the session metadata
        const userId = checkoutSessionCompleted.metadata.userId;

        // Fetch user profile
        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;
      // ... handle other event types
      default:
        console.log(`Unhandled event type ${event.type}`);
    }

    // Return a 200 response to acknowledge receipt of the event
    response.send();
  }
);
#

but its supposed to be a post request right? then why is it trying to get the request?

placid mango
#

Hello
Based on the status shown in the response, it seems like your server returns a 302 code which means it is redirecting to a different URL

hoary finch
placid mango
#

The GET seems to be coming from your own server

hoary finch
#

yes

#

Its coming from my side but Im not sure why, I thought it was supposed to be a post request for the webhooks

#

Alert is a 404 page etc

#

but its a get

#

yk what I mean?

#

im super confused xd

placid mango
#

You'd need to add logging in your code to see why the POST to /stripe_webhook results in a redirect to GET /alert

You must have some fallback logic where if something fails, you redirect to /alert and that causes your server to return 302

hoary finch
#

this ig

placid mango
#

like console.log statements to first check if the POST request is reaching your server or not. If it is, then what route is it trying to reach etc

#

What happens if you comment out that line?

hoary finch
#

oh

#

let me see

#

I have this logging

#

this log

#

I could log the request

#

okay pushed it

#

@placid mango I got this now

placid mango
#

Yeah it seems like something is wrong with your server side code.
I'd recommend looking at our docs and trying to debug on your own.

There isn't anything wrong with Stripe SDK or webhook delivery. It is your server-side code that's routing the requests incorrectly