Skip to content

Commit 9158c15

Browse files
author
Your Name
committed
Stripe In Practice Course
1 parent a865070 commit 9158c15

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

server/checkout.route.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ export async function createCheckoutSession(req: Request, res: Response) {
4141

4242
await purchaseSession.set(checkoutSessionData);
4343

44+
const user = await getDocData(`users/${info.userId}`);
45+
4446
let sessionConfig;
4547

4648
if (info.courseId) {
4749
const course = await getDocData(`courses/${info.courseId}`);
48-
sessionConfig = setupPurchaseCourseSession(info, course, purchaseSession.id);
50+
sessionConfig = setupPurchaseCourseSession(info, course,
51+
purchaseSession.id, user ? user.stripeCustomerId : undefined);
4952
}
5053

5154
console.log(sessionConfig);
@@ -64,8 +67,9 @@ export async function createCheckoutSession(req: Request, res: Response) {
6467

6568
}
6669

67-
function setupPurchaseCourseSession(info: RequestInfo, course, sessionId: string) {
68-
const config = setupBaseSessionConfig(info, sessionId);
70+
function setupPurchaseCourseSession(info: RequestInfo, course, sessionId: string,
71+
stripeCustomerId:string) {
72+
const config = setupBaseSessionConfig(info, sessionId, stripeCustomerId);
6973
config.line_items = [
7074
{
7175
name: course.titles.description,
@@ -79,14 +83,19 @@ function setupPurchaseCourseSession(info: RequestInfo, course, sessionId: string
7983
}
8084

8185

82-
function setupBaseSessionConfig(info: RequestInfo, sessionId: string) {
86+
function setupBaseSessionConfig(info: RequestInfo, sessionId: string,
87+
stripeCustomerId:string) {
8388
const config: any = {
8489
payment_method_types: ['card'],
8590
success_url: `${info.callbackUrl}/?purchaseResult=success&ongoingPurchaseSessionId=${sessionId}`,
8691
cancel_url: `${info.callbackUrl}/?purchaseResult=failed`,
8792
client_reference_id: sessionId
8893
};
8994

95+
if (stripeCustomerId) {
96+
config.customer = stripeCustomerId;
97+
}
98+
9099
return config;
91100
}
92101

server/stripe-webhooks.route.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ async function onCheckoutSessionCompleted(session) {
3737
const {userId, courseId} = await getDocData(`purchaseSessions/${purchaseSessionId}`);
3838

3939
if (courseId) {
40-
await fulfillCoursePurchase(userId, courseId, purchaseSessionId);
40+
await fulfillCoursePurchase(userId, courseId, purchaseSessionId, session.customer);
4141
}
4242
}
4343

4444
async function fulfillCoursePurchase(userId:string, courseId:string,
45-
purchaseSessionId:string) {
45+
purchaseSessionId:string,
46+
stripeCustomerId:string) {
4647

4748
const batch = db.batch();
4849

@@ -54,6 +55,10 @@ async function fulfillCoursePurchase(userId:string, courseId:string,
5455

5556
batch.create(userCoursesOwnedRef, {});
5657

58+
const userRef = db.doc(`users/${userId}`);
59+
60+
batch.set(userRef, {stripeCustomerId}, {merge: true});
61+
5762
return batch.commit();
5863

5964
}

0 commit comments

Comments
 (0)