#bobo00011

1 messages · Page 1 of 1 (latest)

meager ridgeBOT
magic ingot
#

Hi, what's the problem exactly?

woeful spade
#

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.")
magic ingot
#

What's in the err?

woeful spade
#
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```
magic ingot
#

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)

woeful spade
#

hmmm but in docs

#

there is that I should use body-parser

#

bodyParser.raw

#

should I delete it?

magic ingot
#

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.

woeful spade
#

how can i check it?

magic ingot
#

You can share the complete code of your server.

woeful spade
#

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?

magic ingot
#

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.

woeful spade
#

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

magic ingot
#

Why you can't move the endpoint?

#

Can you move the app.use(express.json({})); below the webhook endpoint definition then?

woeful spade
#

hmm but why code from docs doesnt work

#

maybe there is other way?

magic ingot
#

It's hard for me to say since I don't see your code. You can just share the relevant part.

woeful spade
#
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);```
magic ingot
#

Put app.use("/webhooks", router_webhooks); on the top, before app.use(cors()); ...

woeful spade
#

hmm maybe there is other way?

#

what should I pass to constructEvent

#

??

magic ingot
magic ingot
#

What's the problem with moving it, exactly?

meager ridgeBOT