File tree 3 files changed +33
-0
lines changed
3 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -3,3 +3,4 @@ STRIPE_PUBLIC_KEY="pk_test_MgTu31Ar1S9gLWcmCqnelpyD"
3
3
SERVICE_ACCOUNT_FILE_NAME = " stripe-course-recording-ebbda60a91a3.json"
4
4
PROJECT_ID = " stripe-course-recording"
5
5
FIRESTORE_DATABASE_URL = " https://stripe-course-recording.firebaseio.com"
6
+ STRIPE_WEBHOOK_SECRET = " "
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import * as express from 'express';
3
3
import { Application } from "express" ;
4
4
import { createCheckoutSession } from './checkout.route' ;
5
5
import { getUserMiddleware } from './get-user.middleware' ;
6
+ import { stripeWebhooks } from './stripe-webhooks.route' ;
6
7
7
8
8
9
@@ -19,6 +20,9 @@ export function initServer() {
19
20
app . route ( "/api/checkout" ) . post (
20
21
bodyParser . json ( ) , getUserMiddleware , createCheckoutSession ) ;
21
22
23
+ app . route ( "/stripe-webhooks" ) . post (
24
+ bodyParser . raw ( { type :'application/json' } ) , stripeWebhooks ) ;
25
+
22
26
const PORT = process . env . PORT || 9000 ;
23
27
24
28
app . listen ( PORT , ( ) => {
Original file line number Diff line number Diff line change
1
+
2
+ import { Request , Response } from 'express' ;
3
+
4
+ const stripe = require ( 'stripe' ) ( process . env . STRIPE_SECRET_KEY ) ;
5
+
6
+
7
+ export async function stripeWebhooks ( req : Request , res :Response ) {
8
+
9
+ try {
10
+
11
+ const signature = req . headers [ "stripe-signature" ] ;
12
+
13
+ const event = stripe . webhooks . constructEvent (
14
+ req . body , signature , process . env . STRIPE_WEBHOOK_SECRET ) ;
15
+
16
+ if ( event . type == "checkout.session.completed" ) {
17
+ const session = event . data . object ;
18
+ console . log ( session ) ;
19
+ }
20
+
21
+ res . json ( { received :true } ) ;
22
+
23
+ }
24
+ catch ( err ) {
25
+ console . log ( 'Error processing webhook event, reason: ' , err ) ;
26
+ return res . status ( 400 ) . send ( `Webhook Error: ${ err . message } ` ) ;
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments