|
1 | 1 | import helpers from '../../lib/handlerHelpers'; |
2 | 2 | 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'; |
3 | 6 | const { MEMBERSHIPS_TABLE } = require('../../constants/tables'); |
4 | 7 | export const getAll = async (event, ctx, callback) => { |
5 | 8 |
|
@@ -27,16 +30,37 @@ export const getAll = async (event, ctx, callback) => { |
27 | 30 | }; |
28 | 31 |
|
29 | 32 | 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 | + } |
30 | 50 |
|
31 | 51 | let response = helpers.createResponse(200, {}); |
32 | 52 | callback(null, response); |
33 | 53 | return null; |
34 | 54 |
|
35 | 55 | }; |
36 | 56 |
|
37 | | -export const payment = async(event, ctx, callback) => { |
| 57 | +export const config = { |
| 58 | + api: { |
| 59 | + bodyParser: false, |
| 60 | + }, |
| 61 | +}; |
38 | 62 |
|
39 | | - const stripe = require('stripe')('sk_test_51KOxOlBAxwbCreS7JRQtvZCnCgLmn8tjK7WPHDGjpw0s4vfVHLwbcrZZvQLmd5cY7zKRIsfj3pnEDDHTy3G81Tuf00v9ygIBrC'); |
| 63 | +export const payment = async(event, ctx, callback) => { |
40 | 64 | const session = await stripe.checkout.sessions.create({ |
41 | 65 | payment_method_types: ['card'], |
42 | 66 | line_items: [ |
|
0 commit comments