Skip to content

Commit 3da88b7

Browse files
Merge pull request stripe#599 from stripe/update-webhook-signing-example
Updated webhook signing example
2 parents a98f9b1 + b4ff134 commit 3da88b7

File tree

2 files changed

+20
-60
lines changed

2 files changed

+20
-60
lines changed

examples/webhook-signing/express.js

+16-54
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
'use strict';
2-
3-
const Stripe = require('stripe');
4-
const Express = require('express');
1+
const stripe = require('stripe')(process.env.STRIPE_API_KEY);
2+
const express = require('express');
3+
const bodyParser = require('body-parser');
54

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

14-
const apiKey = process.env.STRIPE_API_KEY;
1513
const webhookSecret = process.env.WEBHOOK_SECRET;
14+
const app = express();
1615

17-
const stripe = Stripe(apiKey);
18-
19-
const router = Express.Router();
20-
21-
// Add the raw text body of the request to the `request` object
22-
function addRawBody(req, res, next) {
23-
req.setEncoding('utf8');
24-
25-
var data = '';
26-
27-
req.on('data', function(chunk) {
28-
data += chunk;
29-
});
30-
31-
req.on('end', function() {
32-
req.rawBody = data;
33-
34-
next();
35-
});
36-
}
16+
// Stripe requires the raw body to construct the event
17+
app.post('/webhooks', bodyParser.raw({type: 'application/json'}), (req, res) => {
18+
const sig = req.headers['stripe-signature'];
3719

38-
/**
39-
* You can either `use()` addRawBody on the Router...
40-
*/
41-
// router.use(addRawBody);
42-
43-
/**
44-
* ...or add it directly as middleware to the route.
45-
*/
46-
router.post('/webhooks', addRawBody, function(request, response) {
47-
var event;
20+
let event;
4821

4922
try {
50-
// Try adding the Event as `request.event`
51-
event = stripe.webhooks.constructEvent(
52-
request.rawBody,
53-
request.headers['stripe-signature'],
54-
webhookSecret
55-
);
56-
} catch (e) {
57-
// If `constructEvent` throws an error, respond with the message and return.
58-
console.log('Error', e.message);
59-
60-
return response.status(400).send('Webhook Error:' + e.message);
23+
event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
24+
} catch (err) {
25+
// On error, return the error message
26+
return res.status(400).send(`Webhook Error: ${err.message}`);
6127
}
6228

63-
console.log('Success', event.id);
29+
// Do something with event
30+
console.log('Success:', event.id);
6431

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

69-
// You could either create this app, or just return the `Router` for use in an
70-
// existing Express app - up to you!
71-
72-
const app = Express();
73-
app.use(router);
7436
app.listen(3000, function() {
7537
console.log('Example app listening on port 3000!')
7638
});

examples/webhook-signing/package.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
"name": "webhook-signing-example-express",
33
"version": "1.0.0",
44
"description": "",
5-
"main": "index.js",
6-
"scripts": { },
5+
"main": "express.js",
6+
"scripts": {},
77
"author": "",
88
"license": "ISC",
99
"dependencies": {
10-
"body-parser": "^1.17.1",
11-
"express": "^4.15.2",
12-
"morgan": "^1.8.1",
13-
"stripe": "^4.18.0"
10+
"express": "^4.16.4",
11+
"stripe": "^6.30.0"
1412
}
1513
}

0 commit comments

Comments
 (0)