#fattmonster-webhooks

1 messages · Page 1 of 1 (latest)

mellow narwhal
#

hello! are you using the Stripe CLI webhook secret key?

brisk plover
#

Correct

mellow narwhal
#

can you share your webhook code?

brisk plover
#

node js

#

the errors im getting ^

#

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

digital valve
#

Hi @brisk plover are you using the webhook secret generated from the stripe listen command?

brisk plover
#

yes

#

i am also body parsing the response so i dont think that should be an issue

digital valve
brisk plover
#

let me try it out and get back to you

#

same error nothing changed

digital valve
#

Can you do a console.log(sig) and see what you get?

brisk plover
#

sure

#

un defined

digital valve
#

so signature is missing, can you do a console.log(request) ?

brisk plover
#

ok i am getting a response

#

its pretty big , gonna paste the entire thing here

#

unable to post the entire response its too huge

brisk plover
#

any updates @digital valve ?

digital valve
#

Thanks for the waiting. Discord is busy.

brisk plover
#

no worries

digital valve
#

Ah, you should get signature from request.headers['stripe-signature'], not request.header['stripe-signature']

#

A s is missing in your code.

brisk plover
#

oh my god

#

let me try this

#

ok progress but

#

getting this error now

digital valve
#

Can you send me the full source code?

brisk plover
#

sure

#

Please ignore the other routes

#

app.js

#

webhookRoute.js

#

webhookController.js

digital valve
#

Send me the text files, not screenshots. thanks.

brisk plover
#

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,
};

digital valve
#
app.use(express.json());
app.use(cors());

can you comment out these two lines?

brisk plover
#

Ok will try

#

do i need to restart the stripe listen command every time i change my source code

digital valve
#

no need

#

but you need to restart your server

brisk plover
#

Done

#

its working

#

thank you so much Jack

#

this was so helpful

#

Really appreciate the quick responses

digital valve
#

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.

brisk plover
#

got it makes sense