forked from stripe/stripe-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress.js
More file actions
38 lines (30 loc) · 1.13 KB
/
express.js
File metadata and controls
38 lines (30 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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/)
* 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 webhookSecret = process.env.WEBHOOK_SECRET;
const app = express();
// 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'];
let event;
try {
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}`);
}
// Do something with event
console.log('Success:', event.id);
// Return a response to acknowledge receipt of the event
res.json({received: true});
});
app.listen(3000, function() {
console.log('Example app listening on port 3000!')
});