@@ -8,6 +8,7 @@ interface RequestInfo {
8
8
courseId : string ;
9
9
callbackUrl : string ;
10
10
userId :string ;
11
+ pricingPlanId :string ;
11
12
}
12
13
13
14
export async function createCheckoutSession ( req : Request , res : Response ) {
@@ -16,6 +17,7 @@ export async function createCheckoutSession(req: Request, res: Response) {
16
17
17
18
const info : RequestInfo = {
18
19
courseId : req . body . courseId ,
20
+ pricingPlanId : req . body . pricingPlanId ,
19
21
callbackUrl : req . body . callbackUrl ,
20
22
userId : req [ "uid" ]
21
23
} ;
@@ -38,17 +40,25 @@ export async function createCheckoutSession(req: Request, res: Response) {
38
40
if ( info . courseId ) {
39
41
checkoutSessionData . courseId = info . courseId ;
40
42
}
43
+ else {
44
+ checkoutSessionData . pricingPlanId = info . pricingPlanId ;
45
+ }
41
46
42
47
await purchaseSession . set ( checkoutSessionData ) ;
43
48
44
49
const user = await getDocData ( `users/${ info . userId } ` ) ;
45
50
46
- let sessionConfig ;
51
+ let sessionConfig ,
52
+ stripeCustomerId = user ? user . stripeCustomerId : undefined ;
47
53
48
54
if ( info . courseId ) {
49
55
const course = await getDocData ( `courses/${ info . courseId } ` ) ;
50
56
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 ) ;
52
62
}
53
63
54
64
console . log ( sessionConfig ) ;
@@ -67,6 +77,18 @@ export async function createCheckoutSession(req: Request, res: Response) {
67
77
68
78
}
69
79
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
+
70
92
function setupPurchaseCourseSession ( info : RequestInfo , course , sessionId : string ,
71
93
stripeCustomerId :string ) {
72
94
const config = setupBaseSessionConfig ( info , sessionId , stripeCustomerId ) ;
0 commit comments