#bobo00011
1 messages · Page 1 of 1 (latest)
Hi, what's the problem exactly?
i make payment, i am using checkout
and after this, webhook run
and I get error at my server
console.log("Webhook signature verification failed.")
What's in the err?
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```
What if you remove the body parser middleware for this endpoint?
From my experince, this will only work if you define your webhook endpoint before you app.use(bodyParser)
hmmm but in docs
there is that I should use body-parser
bodyParser.raw
should I delete it?
I don't know since I don't see your complete code. The fact is, what you provide, per error message, is an Object, not a String, so it is parsed at some point. You need to find out where exactly.
how can i check it?
You can share the complete code of your server.
i have too much code xd
i have to pass string or buffer
yes?
not object?
maybe this
app.use(express.json({}));
app.use(express.urlencoded({extended: true}));
i have only these middlewares hmmm
express app
???
why body-parser doesnt work?
What I mentioned before:
From my experince, this will only work if you define your webhook endpoint before you
app.use(bodyParser)
Or express.json({})
Because by the time it will get to you endpoint it will already be parsed.
and what shoould i pass
hmmm
to this
event = stripe.webhooks.constructEvent(abc, sig, process.env.STRIPE_WEBHOOK_SECRET);
hmm :/
i dont know if I can move endpoint
before
I use it for whole app
Why you can't move the endpoint?
Can you move the app.use(express.json({})); below the webhook endpoint definition then?
It's hard for me to say since I don't see your code. You can just share the relevant part.
import { Router } from "express";
import stripe from "./payments";
import prisma from "./db";
import bodyParser from "body-parser";
const router_webhooks = Router();
/**
* Webhook Stripe payment
*/
router_webhooks.post("/payments", bodyParser.raw({type: "application/json"}), (req, res, next) => {
const payload = req.body;
const sig = req.headers["stripe-signature"];
let event = null;
try {
event = stripe.webhooks.constructEvent(req.rawBody, sig, process.env.STRIPE_WEBHOOK_SECRET);
} catch (err) {
console.log(err);
console.log("Webhook signature verification failed.")
return res.status(400).send(`Webhook Error: ${err.message}`);
}
if(event.type === "checkout.session.completed"){
const session = event.data.object;
console.log("completed");
prisma.car_announcements.update({
where: {
id: session.metadata.announcement_id
},
data: {
status: "PAID"
}
})
.then((result) => {
// update entry in payments (status)
res.status(200).end();
})
.catch((err) => {
next(err);
})
} else {
console.log(`Unhandled event type ${event.type}`);
res.status(200).end();
}
});
export default router_webhooks;```
const app = express();
const PgStore = connectPgSimple(session);
app.use(cors());
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use(session({
...
}));
app.use("/api", protect, router); // this is subrouter
app.use("/webhooks", router_webhooks);```
Put app.use("/webhooks", router_webhooks); on the top, before app.use(cors()); ...
No, that's the only way.
The inside of your handler function is correct. What creates the problem is where you define the endpoint.
What's the problem with moving it, exactly?