-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
33 lines (27 loc) · 1002 Bytes
/
index.js
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
const bodyParser = require('body-parser');
const express = require('express');
const config = require('./lib/config');
const stripe = require('stripe')(config.stripeSecretKey);
const { proxyWebhookRequest } = require('./lib/proxy');
const app = express();
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
// verify webhook request
if (config.stripeVerifyWebhookSignature) {
const signature = request.headers['stripe-signature'];
try {
stripe.webhooks.constructEvent(
request.body,
signature,
config.stripeEndpointSecret
);
} catch (err) {
response.sendStatus(400);
return;
}
}
// proxy request if valid
proxyWebhookRequest(request);
// immediately return 200 to Stripe
response.sendStatus(200);
});
app.listen(config.port, () => console.log(`Proxy listening for requests on port: ${config.port}`));