Skip to content

Commit

Permalink
Move example from README.md to 'examples/webhook-signing' folder
Browse files Browse the repository at this point in the history
  • Loading branch information
jlomas-stripe committed May 10, 2017
1 parent aedf0c7 commit 3ca82fe
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 51 deletions.
52 changes: 1 addition & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,57 +132,7 @@ charge.lastResponse.statusCode

Stripe can optionally sign the webhook events it sends to your endpoint, allowing you to validate that they were not sent by a third-party. You can read more about it [here](https://stripe.com/docs/webhooks#signatures).

Here's an example of how to use it with Express:

```js
const Express = require('express');
const bodyParser = require('body-parser')

const Stripe = require('stripe')('sk_test_...');

const webhookSecret = 'whsec_...';

// We use a router here so we can use a separate `bodyParser` instance that
// adds the raw POST data to the `request`
const router = Express.Router();

router.use(bodyParser.json({
verify: function(request, response, buffer) {
request.rawBody = buffer.toString();
},
}));

router.post('/webhooks', function(request, response) {
var 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);
}

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);
});

// 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!')
});
```
You can find an example of how to use this with [Express](https://expressjs.com/) in the [`examples/webhook-signing`](examples/webhook-signing) folder.

## More Information

Expand Down
60 changes: 60 additions & 0 deletions examples/webhook-signing/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

const Stripe = require('stripe');
const Express = require('express');
const bodyParser = require('body-parser')

/**
* You'll need to make sure this is externally accessible. ngrok (https://ngrok.com/)
* makes this really easy.
*
* To run this file, just provide your Secret API Key and Webhook Secret, like so:
* 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 stripe = Stripe(apiKey);

// We use a router here so it can use its own `bodyParser` instance to add
// the raw POST data to the `request`
const router = Express.Router();

router.use(bodyParser.json({
verify: function(request, response, buffer) {
request.rawBody = buffer.toString();
},
}));

router.post('/webhooks', function(request, response) {
var 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);
}

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);
});

// 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!')
});
15 changes: 15 additions & 0 deletions examples/webhook-signing/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "webhook-signing-example-express",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": { },
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"morgan": "^1.8.1",
"stripe": "^4.18.0"
}
}

0 comments on commit 3ca82fe

Please sign in to comment.