#fattmonster-webhooks
1 messages · Page 1 of 1 (latest)
Correct
can you share your webhook code?
node js
the errors im getting ^
stripe listen --forward-to=http://localhost:9000/api/webhook is what i ran in the stripe cli followed by stripe trigger event(various events)
stripe extension for vs code doesnt seem to work for me but thats not a burning issue i am okay to do it on cmd
Hi @brisk plover are you using the webhook secret generated from the stripe listen command?
Can you use express.raw instead of bodyparser? you can copy the example code from https://stripe.com/docs/webhooks/quickstart?lang=node
Can you do a console.log(sig) and see what you get?
so signature is missing, can you do a console.log(request) ?
ok i am getting a response
its pretty big , gonna paste the entire thing here
unable to post the entire response its too huge
any updates @digital valve ?
Thanks for the waiting. Discord is busy.
no worries
Ah, you should get signature from request.headers['stripe-signature'], not request.header['stripe-signature']
A s is missing in your code.
Can you send me the full source code?
sure
Please ignore the other routes
app.js
webhookRoute.js
webhookController.js
the endpoint im passing is http://localhost:9000/api/webhook
Send me the text files, not screenshots. thanks.
sure
app. js - >
const express = require("express");
const app = express();
const dotenv = require("dotenv");
const cors = require("cors");
const dbConnection = require("./config/dbConfig");
dotenv.config();
const industryRoute = require("./routes/industryRoute");
const merchantRoute = require("./routes/merchantRoute");
const webhookRoute = require("./routes/webhookRoute");
//--------------------------Define Middleweares------------------------
const errorHandler = require("./middleweares/errorHandler");
//-----------------------------App has to use JSON request------------------
app.use(express.json());
app.use(cors());
//------------------------------Database Connection------------------------
dbConnection.dbConnect();
//-----------------------------Use Router to define endpoint-----------------
app.use("/api/industry", industryRoute);
app.use("/api/merchant", merchantRoute);
app.use("/api", webhookRoute);
app.use(errorHandler);
app.listen(process.env.PORT || 9000, () => {
console.log(Backend server is running on ${process.env.PORT});
});
✅ webhookRoute.js - >
const express = require("express");
const router = express.Router();
const bodyparser = require("body-parser");
const webhookController = require("../controllers/webhookController");
router.post(
"/webhook",
express.raw({ type: "application/json" }),
webhookController.showData
);
module.exports = router;
webhookController.js - > ✅
const dotenv = require("dotenv");
dotenv.config;
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const showData = async (request, response) => {
let signingsecret =
"whsec_0cb3424aa5725b48ece6b8a41fd99a95c364874893f68258b84f77cdc6269c7d";
const payload = request.body;
const sig = request.headers["stripe-signature"];
//verifying stripe sig
let event;
try {
event = stripe.webhooks.constructEvent(payload, sig, signingsecret);
} catch (error) {
console.log(request);
console.log(sig);
// console.log(request.headers["stripe-signature"]);
console.log(error.message);
response.status(400).json({ success: false });
return;
}
console.log(event.type);
console.log(event.data.object);
console.log(event.data.object.id);
response.json({
success: true,
});
};
module.exports = {
showData,
};
app.use(express.json());
app.use(cors());
can you comment out these two lines?
Ok will try
do i need to restart the stripe listen command every time i change my source code
Done
its working
thank you so much Jack
this was so helpful
Really appreciate the quick responses
No problem. So basically app.use(express.json()); is coverting the request to json, and that's why the Stripe SDK is unable to get the raw content.
got it makes sense