Skip to content

Commit b97df86

Browse files
author
sli
committed
Implemented Webhook verification
1 parent 4934b72 commit b97df86

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

services/memberships/handler.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import helpers from '../../lib/handlerHelpers';
22
import db from '../../lib/db';
3+
const stripe = require('stripe')('sk_test_51KOxOlBAxwbCreS7JRQtvZCnCgLmn8tjK7WPHDGjpw0s4vfVHLwbcrZZvQLmd5cY7zKRIsfj3pnEDDHTy3G81Tuf00v9ygIBrC');
4+
// development endpoint secret - switch to live secret key in production
5+
const endpointSecret = 'whsec_TYSFr29HQ4bIPu649lgkxOrlPjrDOe2l';
36
const { MEMBERSHIPS_TABLE } = require('../../constants/tables');
47
export const getAll = async (event, ctx, callback) => {
58

@@ -27,16 +30,37 @@ export const getAll = async (event, ctx, callback) => {
2730
};
2831

2932
export const webhook = async(event, ctx, callback) => {
33+
const sig = event.headers['Stripe-Signature'];
34+
let eventData;
35+
console.log(event.body);
36+
37+
// Stripe returns an error if verification fails
38+
try {
39+
eventData = stripe.webhooks.constructEvent(event.body, sig, endpointSecret);
40+
} catch(err) {
41+
return helpers.createResponse(400, {
42+
message: `Webhook Error: ${err.message}`
43+
});
44+
}
45+
46+
// Handle the checkout.session.completed event
47+
if (eventData.type == 'checkout.session.completed') {
48+
console.log(eventData.data);
49+
}
3050

3151
let response = helpers.createResponse(200, {});
3252
callback(null, response);
3353
return null;
3454

3555
};
3656

37-
export const payment = async(event, ctx, callback) => {
57+
export const config = {
58+
api: {
59+
bodyParser: false,
60+
},
61+
};
3862

39-
const stripe = require('stripe')('sk_test_51KOxOlBAxwbCreS7JRQtvZCnCgLmn8tjK7WPHDGjpw0s4vfVHLwbcrZZvQLmd5cY7zKRIsfj3pnEDDHTy3G81Tuf00v9ygIBrC');
63+
export const payment = async(event, ctx, callback) => {
4064
const session = await stripe.checkout.sessions.create({
4165
payment_method_types: ['card'],
4266
line_items: [

0 commit comments

Comments
 (0)