Skip to content

Commit c46937a

Browse files
author
Your Name
committed
Stripe In Practice Course
1 parent 5ce90e1 commit c46937a

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

server/checkout.route.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ interface RequestInfo {
88
courseId: string;
99
callbackUrl: string;
1010
userId:string;
11+
pricingPlanId:string;
1112
}
1213

1314
export async function createCheckoutSession(req: Request, res: Response) {
@@ -16,6 +17,7 @@ export async function createCheckoutSession(req: Request, res: Response) {
1617

1718
const info: RequestInfo = {
1819
courseId: req.body.courseId,
20+
pricingPlanId: req.body.pricingPlanId,
1921
callbackUrl: req.body.callbackUrl,
2022
userId: req["uid"]
2123
};
@@ -38,17 +40,25 @@ export async function createCheckoutSession(req: Request, res: Response) {
3840
if (info.courseId) {
3941
checkoutSessionData.courseId = info.courseId;
4042
}
43+
else {
44+
checkoutSessionData.pricingPlanId = info.pricingPlanId;
45+
}
4146

4247
await purchaseSession.set(checkoutSessionData);
4348

4449
const user = await getDocData(`users/${info.userId}`);
4550

46-
let sessionConfig;
51+
let sessionConfig,
52+
stripeCustomerId = user ? user.stripeCustomerId : undefined;
4753

4854
if (info.courseId) {
4955
const course = await getDocData(`courses/${info.courseId}`);
5056
sessionConfig = setupPurchaseCourseSession(info, course,
51-
purchaseSession.id, user ? user.stripeCustomerId : undefined);
57+
purchaseSession.id, stripeCustomerId);
58+
}
59+
else if (info.pricingPlanId) {
60+
sessionConfig = setupSubscriptionSession(info, purchaseSession.id,
61+
stripeCustomerId, info.pricingPlanId);
5262
}
5363

5464
console.log(sessionConfig);
@@ -67,6 +77,18 @@ export async function createCheckoutSession(req: Request, res: Response) {
6777

6878
}
6979

80+
function setupSubscriptionSession(info: RequestInfo, sessionId: string,stripeCustomerId,
81+
pricingPlanId) {
82+
83+
const config = setupBaseSessionConfig(info, sessionId, stripeCustomerId);
84+
85+
config.subscription_data = {
86+
items: [{plan: pricingPlanId}]
87+
};
88+
89+
return config;
90+
}
91+
7092
function setupPurchaseCourseSession(info: RequestInfo, course, sessionId: string,
7193
stripeCustomerId:string) {
7294
const config = setupBaseSessionConfig(info, sessionId, stripeCustomerId);

0 commit comments

Comments
 (0)