APIs & Integrations

IamYIZ
投稿者

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

解決

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?

1件の承認済みベストアンサー
IamYIZ
解決策
投稿者

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

解決

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 いいね!
2件の返信
IamYIZ
解決策
投稿者

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

解決

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 いいね!
CLahiff
参加者

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

解決

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 いいね!