Skip to content

Commit 411cbaf

Browse files
author
Your Name
committed
Stripe In Practice Course
1 parent aaa8a2d commit 411cbaf

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

server/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ STRIPE_PUBLIC_KEY="pk_test_MgTu31Ar1S9gLWcmCqnelpyD"
33
SERVICE_ACCOUNT_FILE_NAME="stripe-course-recording-ebbda60a91a3.json"
44
PROJECT_ID="stripe-course-recording"
55
FIRESTORE_DATABASE_URL="https://stripe-course-recording.firebaseio.com"
6+
STRIPE_WEBHOOK_SECRET=""

server/server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as express from 'express';
33
import {Application} from "express";
44
import {createCheckoutSession} from './checkout.route';
55
import {getUserMiddleware} from './get-user.middleware';
6+
import {stripeWebhooks} from './stripe-webhooks.route';
67

78

89

@@ -19,6 +20,9 @@ export function initServer() {
1920
app.route("/api/checkout").post(
2021
bodyParser.json(), getUserMiddleware, createCheckoutSession);
2122

23+
app.route("/stripe-webhooks").post(
24+
bodyParser.raw({type:'application/json'}), stripeWebhooks);
25+
2226
const PORT = process.env.PORT || 9000;
2327

2428
app.listen(PORT, () => {

server/stripe-webhooks.route.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

0 commit comments

Comments
 (0)