#subhrajeet99
1 messages · Page 1 of 1 (latest)
Hello! 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.
- subhrajeet99, 37 minutes ago, 37 messages
How can I help?
Hello again Jack
I am implementing webhooks in my app, but facing some errors, it may be due to some parsing error
This is the code snippet-
const handleStripeWebhook = async (req, res) => {
console.log(This is request body, req.body);
const sig = req.headers['stripe-signature'];
console.log(`This is signature`, sig);
let event;
console.log(`This is event`, event);
try {
event = stripeInstance.webhooks.constructEvent(req.body, sig, endpointSecret);
} catch (err) {
res.status(400).send(`Webhook Error: ${err.message}`);
return;
}
// Handle the specific event type
switch (event.type) {
case 'checkout.session.completed':
const checkoutSessionCompleted = event.data.object;
console.log("Checkout session completed", checkoutSessionCompleted)
break;
case 'payment_intent.payment_failed':
const paymentIntentPaymentFailed = event.data.object;
console.log("Payment intent failed", paymentIntentPaymentFailed)
break;
case 'customer.subscription.created':
const customerSubscriptionCreated = event.data.object;
console.log("Subscription was successfully created for customer", customerSubscriptionCreated)
break;
case 'payment_intent.succeeded':
const paymentIntentSucceeded = event.data.object;
console.log("Payment Intent was successfully created for customer", paymentIntentSucceeded)
break;
// Add cases for other events you want to handle
default:
console.log(`Unhandled event type: ${event.type}`);
}
res.status(200).json({ received: true });
}
What's the error message?
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.
Are you using any middleware that pre-process the request?
I am using these middlewares-
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/uploads", express.static("uploads")); //for image uploads
app.use(bodyParser.raw({ type: 'application/json' }));
In my index.js file
And this is the route-
app.post('/webhook', handleStripeWebhook);
Remove all the middleware and try again
ok let me try
Now it gives a different error-
Webhook Error: No webhook payload was provided.
Ok now I fixed it