APIs & Integrations

IamYIZ
Contributor

Correctly parsing webhook requests for both endpoints for both Hubspot and Stripe APIs

SOLVE

I am building an app on the marketplace in which I receive webhooks from the app and then make api calls to perform certain actions.

 

I am using the Hubspot API for most functions and the Stripe API to collect payment and verify payment status. The entire code is written in Node JS and I am running the express node js library to listen to endpoints and deal with requests.

 

This is the setup of my express

 

const express = require('express') 
const app = express()

app.use(express.raw({type: "application/json"})
app.use(express.json())

 

I am using express.raw because of the stripe API which requires the raw request to be called into some kind of Buffer. Now this is the call for an example endpoint for a non-Stripe call, such as getting account settings for Hubspot

 

app.post('/account-fetch', (req, res) => { 
const portalId = req.body.portalId
console.log(portalId)
setup.retrieveKey(portalId, "hubname")
.then((acc) => {
console.log(acc)
const component = {
"response": {
"accounts": [{
"accountId": '"' + portalId + '"',
"accountName": acc }]
} }
console.log(component)
res.status(200).send(component) })
.catch((err) => {
console.error(err)
res.status(400).send(err)
})
})

 

Now the issue is that while stripe needs it to have that express.raw call, it messes up everything else, because the Buffer is encoded and it just comes out as numbers.

 

I've also tried on the stripe endpoint:

app.post('/stripe/:type', express.raw({type: "application/json }),(req, res) => { 
//rest of my code
})

 

or not calling it at all, still no cigar.

 

All I get is a StripeSignatureVerificationError because it can't read the signature on the payload properly. I've tried placing that line just before the stripe endpoint, but it doesn't work.

 

Is there a way to either to convert the Buffer back into what it would be if I didn't do the express.raw call or a way to just make that call isolated for one endpoint?

0 Upvotes
1 Accepted solution
IamYIZ
Solution
Contributor

Correctly parsing webhook requests for both endpoints for both Hubspot and Stripe APIs

SOLVE

I solved it in the end. I used Routers.

 

So my end code was:

const express = require('express')
const app = express()
const stripeApp = express.Router()

stripeApp.use(express.raw({type:"*/*"}))

app.use('/stripe',stripeApp)
app.use(express.json())

 

and my end point was configured like this:

 

stripeApp.post('/:type', express.raw({type: "application/json"}),(req, res) => {
//my request handling code here
})

View solution in original post

0 Upvotes
2 Replies 2
IamYIZ
Solution
Contributor

Correctly parsing webhook requests for both endpoints for both Hubspot and Stripe APIs

SOLVE

I solved it in the end. I used Routers.

 

So my end code was:

const express = require('express')
const app = express()
const stripeApp = express.Router()

stripeApp.use(express.raw({type:"*/*"}))

app.use('/stripe',stripeApp)
app.use(express.json())

 

and my end point was configured like this:

 

stripeApp.post('/:type', express.raw({type: "application/json"}),(req, res) => {
//my request handling code here
})
0 Upvotes
CLahiff
Participant

Correctly parsing webhook requests for both endpoints for both Hubspot and Stripe APIs

SOLVE

Hi! Do you have experience with stripe and hubspot integrations? I am trying to get meta data and payment info into contact info in hubspot...really struggling!

0 Upvotes