Stripe Integration

This commit is contained in:
2024-08-20 23:27:07 +02:00
parent 056db7b199
commit 48bff89526
28 changed files with 728 additions and 21 deletions

View File

@@ -1,5 +1,10 @@
import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { KeycloakService } from 'keycloak-angular';
import { StripeService } from 'ngx-stripe';
import { switchMap } from 'rxjs';
import { environment } from '../../../environments/environment';
import { SharedModule } from '../../shared/shared/shared.module';
@Component({
@@ -10,8 +15,40 @@ import { SharedModule } from '../../shared/shared/shared.module';
styleUrl: './pricing.component.scss',
})
export class PricingComponent {
constructor(public keycloakService: KeycloakService) {}
register() {
this.keycloakService.register({ redirectUri: `${window.location.origin}/account` });
private apiBaseUrl = environment.apiBaseUrl;
private id: string | undefined = this.activatedRoute.snapshot.params['id'] as string | undefined;
constructor(public keycloakService: KeycloakService, private http: HttpClient, private stripeService: StripeService, private activatedRoute: ActivatedRoute) {}
ngOnInit() {
if (this.id) {
this.checkout(atob(this.id));
}
}
register(priceId?: string) {
if (priceId) {
this.keycloakService.register({ redirectUri: `${window.location.origin}/pricing/${btoa(priceId)}` });
} else {
this.keycloakService.register({ redirectUri: `${window.location.origin}/account` });
}
}
checkout(priceId) {
// Check the server.js tab to see an example implementation
this.http
.post(`${this.apiBaseUrl}/bizmatch/payment/create-checkout-session`, { priceId })
.pipe(
switchMap((session: any) => {
return this.stripeService.redirectToCheckout({ sessionId: session.id });
}),
)
.subscribe(result => {
// If `redirectToCheckout` fails due to a browser or network
// error, you should display the localized error message to your
// customer using `error.message`.
if (result.error) {
alert(result.error.message);
}
});
}
}