#yuyu_webhooks
1 messages · Page 1 of 1 (latest)
👋 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.
- yuyu_webhooks, 6 hours ago, 31 messages
- yuyu_webhooks, 8 hours ago, 7 messages
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
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
That is the issue
Node will parse the request into JSON first before entering your webhook route
So the request is no longer in raw form
so like this?
Can you try if this works?
You have two app.use(express.json()) in your code
Please make sure both are placed after the route declaration
One with limit, and one without
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
How about commenting both out first to check if app.use(express.json()) is the cause of the problem?
Dude i have been messing around with this the entire day omg
thank you so much!!!
No problem! Happy to help 😄
Question
can I have only one express.jeson?
app.use(express.json({ limit: "50mb" }));
like this one instead of the other one?
Yup, one will be fine