Setup Stripe webhook

hi

I like to move my stripe-webhook to mongodb. Currently I have a nodejs server where I can get the raw body from the request like (because stripe needs the raw request body)

app.post(‘/webhook’, bodyParser.raw({ type: ‘application/json’ }), (request, response)

Is it possible to get the raw body request somehow in a mongodb webhook?

thx

1 Like

Do these docs help - https://docs.mongodb.com/realm/functions/json-and-bson/ ?
Sounds like you may want to use one of the parse functions.

hm sadly not. Non of them were successfully.

the raw body on my nodejs application was like:

<Buffer 7b 0a 20 20 22 63 72 65 61 74 65 64 22 3a 20 31 33 32 36 38 35 33 34 37 38 2c 0a 20 20 22…

and stripe error is like:

“Webhook Error: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? GitHub - stripe/stripe-node: Node.js library for the Stripe API.

Hi @rouuuge.

For some general examples of working with Stripe from Realm. you could take a look at this eCommerce app that I built a while back: GitHub - mongodb-appeng/eCommerce-Realm: The backend portion of the MongoDB eCommerce reference app For example, this function creates a Stripe checkout session: eCommerce-Realm/source.js at main · mongodb-appeng/eCommerce-Realm · GitHub

The frontend (Vue.js) app is here: GitHub - mongodb-appeng/eCommerce: An example eCommerce store built on MongoDB Atlas and MongoDB Stitch

3 Likes

hi andrew, thx for the example! Good to know that at least the stripe api seems to work! But for me its not really a option, because I use the UI-Forms directly from Stripe directly: Stripe Checkout | Stripe Documentation

to get data into mongodb I need their webhook. Of course I could run a own nodejs server like now. But I would like ot move as much code as possible to the same place.

1 Like

Did anyone know how to get the raw request body?

Stripe Webhook does need it, I have the same error as @rouuuge

Hi,

You need to parse the Stripe webhook event as raw data. Usually, everyone have JSON parser before any router defined:

app.use(express.json());
app.use(express.urlencoded());

Express executes code from top to bottom. That means that express.json() will be called before express.raw({ type: 'application/json'}) defined in the /webhook endpoint.

So, all you have to do is to move /webhook endpoint before defining express.json() parser.

app.post('/webhook', express.raw({ type: 'application/json' }), (req, res) => {
   ...
});

app.use(express.json());
app.use(express.urlencoded());

Thanks, that works for NodeJS directly, sorry I meant on Realm Functions. There looks like no way to get the raw response data on Realm Functions.

1 Like
1 Like

Hi… im having the same problem. Were you able to solve this using realm functions?