for the Webhook i always receive this Status Code from Stripe
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
I looked into it over a day now and cant figure out where the mistake is in my Code.
Here is the Code:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const express = require("express");
const stripe = require("stripe")(functions.config().stripe.secret);
// Initialize Firebase Admin SDK
admin.initializeApp();
// Initialize Firestore
const db = admin.firestore();
// Create an Express app
const app = express();
// Middleware to capture raw body for Stripe webhook
app.use(express.raw({type: "application/json"}));
// Stripe webhook endpoint
app.post("/webhook", async (req, res) => {
const signature = req.headers["stripe-signature"];
const eps = functions.config().stripe.webhook_secret;
let event;
try {
event = stripe.webhooks.constructEvent(req.body, signature, eps);
} catch (err) {
console.error("⚠️Webhook signature verification failed:", err.message);
return res.status(400).send(`Webhook Error: ${err.message}`);
}
// Handle the event
switch (event.type) {
case "checkout.session.completed": {
const session = event.data.object;
const userUid = session.client_reference_id;
console.log("Checkout session completed:", session);
if (session.payment_status === "paid") {
try {
await db.doc(`restaurant/${userUid}/branch/payment`).set(
{
paid: true,
},
{merge: true},
);
console.log(
`Firestore updated for ${userUid} with payment information`,
);
return res.json({received: true});
} catch (error) {
console.error("Error updating Firestore:", error);
return res.status(500).send(`Update Error: ${error.message}`);
}
} else {
console.log("Payment status is not "paid".");
return res.status(400).send("Payment status is not paid.");
}
}
default: {
console.log(`Unhandled event type ${event.type}.`);
return res.status(400).send(`Unhandled event type ${event.type}.`);
}
}
});
// Use express.json() for other routes if needed
app.use(express.json());
// Export the Express app as an HTTP function
exports.stripeWebhook = functions.https.onRequest(app);
I cant figure out what is wrong i hope for the final help here 🙂
The Status Code to return 200 instead of 400
dgrabovac40googlemailcom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.