Skip to content

Commit 0055af0

Browse files
committed
Stripe Course
1 parent 0892539 commit 0055af0

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

firestore.rules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ service cloud.firestore {
2828

2929
}
3030

31+
match /purchaseSessions/{purchaseId} {
32+
allow read: if resource.data.userId == request.auth.uid;
33+
}
34+
3135
}
3236

3337
}

src/app/services/checkout.service.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ import {Injectable} from '@angular/core';
22
import {HttpClient} from '@angular/common/http';
33
import {Observable} from 'rxjs';
44
import {CheckoutSession} from '../model/checkout-session.model';
5+
import {AngularFirestore} from '@angular/fire/firestore';
6+
import {filter, first} from 'rxjs/operators';
57

68

79
@Injectable({
810
providedIn: 'root'
911
})
1012
export class CheckoutService {
1113

12-
constructor(private http: HttpClient) {
14+
constructor(
15+
private http: HttpClient,
16+
private afs: AngularFirestore) {
1317

1418
}
1519

@@ -31,5 +35,12 @@ export class CheckoutService {
3135
}
3236

3337

34-
38+
waitForPurchaseToComplete(ongoingPurchaseSessionId: string): Observable<any> {
39+
return this.afs.doc<any>(`purchaseSessions/${ongoingPurchaseSessionId}`)
40+
.valueChanges()
41+
.pipe(
42+
filter(purchase => purchase.status == "completed"),
43+
first()
44+
)
45+
}
3546
}

src/app/stripe-checkout/stripe-checkout.component.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,19 @@ export class StripeCheckoutComponent implements OnInit {
2626

2727
if (result == "success") {
2828

29+
const ongoingPurchaseSessionId = this.route.snapshot.queryParamMap.get('ongoingPurchaseSessionId');
30+
31+
this.checkout.waitForPurchaseToComplete(ongoingPurchaseSessionId)
32+
.subscribe(
33+
() => {
34+
this.waiting = false;
35+
this.message = "Purchase SUCCESSFUL, redirecting...";
36+
setTimeout(() => this.router.navigateByUrl("/courses"), 3000);
37+
});
2938
}
3039
else {
3140
this.waiting = false;
32-
this.message = "Purchase canceled or failed, redirecting...";
41+
this.message = "Purchase CANCELED or FAILED, redirecting...";
3342
setTimeout(() => this.router.navigateByUrl("/courses"), 3000);
3443
}
3544

0 commit comments

Comments
 (0)