Listening to stripe Webhooks using functions

856 views
Skip to first unread message

shahar wider

unread,
May 2, 2018, 10:25:48 AM5/2/18
to Firebase Google Group
Hey,
I've been trying to listen to Stripe webhooks with firebase functions:


here is my code:
app.use(bodyParser.raw({ type: '*/*' }));

app.post('*', (req, res) => {
const sig = req.headers["stripe-signature"];
try {
const event = stripe.webhooks.constructEvent(req.body, sig, stripeWHEndpointSecret);
console.log(event);

}
catch (err) {
console.log(util.inspect(err));
res.status(400).end();
}
res.json({received: true});
});

export const stripeWebhooksListener = functions.https.onRequest(app);
 
and I keep getting this error:
SyntaxError: Unexpected token o in JSON at position 1

Now i understand it's a problem with parsing the req.body as it arrives in chunks probably. but, I thought that using the Express with body-parser should solve it.
Any help will be appreciated.



Stripe official documentation on how to do it: https://stripe.com/docs/webhooks/signatures
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
var stripe = require("stripe")("token");

// You can find your endpoint's secret in your webhook settings
const endpointSecret = 'secret';

// This example uses Express to receive webhooks
const app = require('express')();

// Retrieve the raw body as a buffer and match all content types
app.use(require('body-parser').raw({type: '*/*'}));

app.post('/webhook/example', (req, res) => {
  let sig = req.headers["stripe-signature"];

  try {
    let event = stripe.webhooks.constructEvent(req.body, sig, endpointSecret);
    // Do something with event
  }
  catch (err) {
    res.status(400).end()
  }

  // Return a response
  res.json({received: true});
});

app.listen(8000, () => console.log('Running on port 8000'));



Kato Richardson

unread,
May 2, 2018, 1:16:27 PM5/2/18
to Firebase Google Group
Hello Shahar,

Did you check out our Functions example using Stripe? Seems like the best place to start.

☼, Kato


--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/fe407fc5-73ad-4f1d-9b37-9ee198e2c97e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

shahar wider

unread,
May 3, 2018, 10:05:44 AM5/3/18
to Firebase Google Group
Yeah, I've seen it... it has no reference to webhooks.
Shahar.

Renaud Tarnec

unread,
May 3, 2018, 10:05:44 AM5/3/18
to Firebase Google Group
Hello Shahar,

Have you seen my answer to your corresponding SO question? https://stackoverflow.com/questions/50137546/listening-to-stripe-webhooks-using-firebase-functions-node-js

Renaud
Reply all
Reply to author
Forward
0 new messages