-
Notifications
You must be signed in to change notification settings - Fork 0
/
stripe-webhook.js
58 lines (46 loc) · 1.42 KB
/
stripe-webhook.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
51
52
53
54
55
56
57
58
import handler from "./libs/handler-lib";
import checkoutSessionCompleted from "./stripe/webhooks/checkout.session.completed";
const webhooks = {
"checkout.session.completed": checkoutSessionCompleted,
};
// TODO
// Validate Stripe Webhook Signature
// https://stripe.com/docs/webhooks/signatures#verify-official-libraries
export const main = handler(async (event, context) => {
const stripeWebhook = event.body ? JSON.parse(event.body) : null;
const parsedStripeWebhook =
stripeWebhook && stripeWebhook.data && stripeWebhook.data.object;
if (!stripeWebhook || !parsedStripeWebhook) {
return null;
}
if (!webhooks[stripeWebhook.type]) {
return null;
}
console.log(stripeWebhook.type);
await webhooks[stripeWebhook.type](parsedStripeWebhook);
return {
status: 200,
message: "Webhook received.",
};
// // const body = JSON.parse(event.body);
// console.log(body);
// // Handle Stripe Account Lifecycle and Billing Events
// // Load our secret key from the environment variables
// const stripe = stripePackage(process.env.stripeSecretKey);
// console.log(stripe); // prevent linting errors
// try {
// console.log(body);
// return {
// status: 200,
// message: "Webhook received.",
// // return value (200)
// };
// } catch (e) {
// return {
// status: false,
// error: {
// message: e.message,
// },
// };
// }
});