Skip to content

Commit 5ce90e1

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

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/app/home/home.component.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {Course} from '../model/course';
33
import {Observable} from 'rxjs';
44
import {map} from 'rxjs/operators';
55
import {CoursesService} from '../services/courses.service';
6+
import {CheckoutService} from '../services/checkout.service';
67

78

89
@Component({
@@ -21,7 +22,8 @@ export class HomeComponent implements OnInit {
2122
processingOngoing = false;
2223

2324
constructor(
24-
private coursesService: CoursesService) {
25+
private coursesService: CoursesService,
26+
private checkout: CheckoutService) {
2527

2628
}
2729

@@ -46,7 +48,14 @@ export class HomeComponent implements OnInit {
4648

4749

4850
subscribeToPlan() {
49-
51+
this.checkout.startSubscriptionCheckoutSession("STRIPE_MONTHLY")
52+
.subscribe(
53+
session => this.checkout.redirectToCheckout(session),
54+
err => {
55+
console.log("Error creating checkout session", err);
56+
this.processingOngoing = false;
57+
}
58+
);
5059
}
5160

5261
}

src/app/services/checkout.service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ export class CheckoutService {
3434
}, {headers})
3535
}
3636

37+
startSubscriptionCheckoutSession(pricingPlanId:string): Observable<CheckoutSession> {
38+
39+
const headers = new HttpHeaders().set("Authorization", this.jwtAuth);
40+
41+
return this.http.post<CheckoutSession>("/api/checkout", {
42+
pricingPlanId,
43+
callbackUrl: this.buildCallbackUrl()
44+
}, {headers})
45+
}
46+
3747
buildCallbackUrl() {
3848

3949
const protocol = window.location.protocol,

0 commit comments

Comments
 (0)