-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
handler.js
50 lines (45 loc) · 1.45 KB
/
handler.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';
const ConfigFile = require('config'); // eslint-disable-line
module.exports.incoming = (event, context, callback) => {
const requestContextStage =
event.requestContext
? event.requestContext.stage
: 'test';
const stripeApiKey =
requestContextStage === 'test'
? ConfigFile.stripe.test_sk
: ConfigFile.stripe.live_sk;
const stripe = require('stripe')(stripeApiKey); // eslint-disable-line
try {
// Parse Stripe Event
const jsonData = JSON.parse(event.body); // https://stripe.com/docs/api#event_object
// Verify the event by fetching it from Stripe
console.log("Stripe Event: %j", jsonData); // eslint-disable-line
stripe.events.retrieve(jsonData.id, (err, stripeEvent) => {
const eventType = stripeEvent.type ? stripeEvent.type : '';
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Stripe webhook incoming!',
stage: requestContextStage,
}),
};
console.log("Event Type: %j", eventType); // eslint-disable-line
// Branch by event type
switch (eventType) {
case 'invoice.created':
// invoice.created event
break;
default:
break;
}
callback(null, response);
});
} catch (err) {
callback(null, {
statusCode: err.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: err.message || 'Internal server error',
});
}
};