-
Notifications
You must be signed in to change notification settings - Fork 8
/
handler.js
30 lines (27 loc) · 924 Bytes
/
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
'use strict';
const stripe = require('stripe'),
eventbridge = require('./lib/eventbridge'),
sns = require('./lib/sns'),
AWS = require('aws-sdk'),
{ promisify } = require('util'),
secretName = process.env.ENDPOINT_SECRET
var client = new AWS.SecretsManager()
client.fetchSecret = promisify(client.getSecretValue)
module.exports.stripeWebhook = async event => {
let err = null
try
{
const signature = event.headers["Stripe-Signature"]
const secret = (await client.fetchSecret({ SecretId: secretName })).SecretString
const eventReceived = stripe.webhooks.constructEvent(event.body, signature, secret)
await eventbridge.sendToEventBridge(process.env.EVENT_BRIDGE, eventReceived)
}
catch (e)
{
err = e
await sns.notifyFailure(e.message)
}
const body = err ? JSON.stringify(err) : ""
const statusCode = err ? 500 : 200
return { statusCode, body }
};