Skip to content

Commit

Permalink
Merge pull request #599 from stripe/update-webhook-signing-example
Browse files Browse the repository at this point in the history
Updated webhook signing example
  • Loading branch information
paulasjes-stripe authored Apr 29, 2019
2 parents e110a2b + 5c1494a commit c7c1858
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 60 deletions.
70 changes: 16 additions & 54 deletions examples/webhook-signing/express.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const Stripe = require('stripe');
const Express = require('express');
const stripe = require('stripe')(process.env.STRIPE_API_KEY);
const express = require('express');
const bodyParser = require('body-parser');

/**
* You'll need to make sure this is externally accessible. ngrok (https://ngrok.com/)
Expand All @@ -11,66 +10,29 @@ const Express = require('express');
* STRIPE_API_KEY=sk_test_XXX WEBHOOK_SECRET=whsec_XXX node express.js
*/

const apiKey = process.env.STRIPE_API_KEY;
const webhookSecret = process.env.WEBHOOK_SECRET;
const app = express();

const stripe = Stripe(apiKey);

const router = Express.Router();

// Add the raw text body of the request to the `request` object
function addRawBody(req, res, next) {
req.setEncoding('utf8');

var data = '';

req.on('data', function(chunk) {
data += chunk;
});

req.on('end', function() {
req.rawBody = data;

next();
});
}
// Stripe requires the raw body to construct the event
app.post('/webhooks', bodyParser.raw({type: 'application/json'}), (req, res) => {
const sig = req.headers['stripe-signature'];

/**
* You can either `use()` addRawBody on the Router...
*/
// router.use(addRawBody);

/**
* ...or add it directly as middleware to the route.
*/
router.post('/webhooks', addRawBody, function(request, response) {
var event;
let event;

try {
// Try adding the Event as `request.event`
event = stripe.webhooks.constructEvent(
request.rawBody,
request.headers['stripe-signature'],
webhookSecret
);
} catch (e) {
// If `constructEvent` throws an error, respond with the message and return.
console.log('Error', e.message);

return response.status(400).send('Webhook Error:' + e.message);
event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
} catch (err) {
// On error, return the error message
return res.status(400).send(`Webhook Error: ${err.message}`);
}

console.log('Success', event.id);
// Do something with event
console.log('Success:', event.id);

// Event was 'constructed', so we can respond with a 200 OK
response.status(200).send('Signed Webhook Received: ' + event.id);
// Return a response to acknowledge receipt of the event
res.json({received: true});
});

// You could either create this app, or just return the `Router` for use in an
// existing Express app - up to you!

const app = Express();
app.use(router);
app.listen(3000, function() {
console.log('Example app listening on port 3000!')
});
10 changes: 4 additions & 6 deletions examples/webhook-signing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
"name": "webhook-signing-example-express",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": { },
"main": "express.js",
"scripts": {},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"morgan": "^1.8.1",
"stripe": "^4.18.0"
"express": "^4.16.4",
"stripe": "^6.30.0"
}
}

0 comments on commit c7c1858

Please sign in to comment.