Fixed checkout logic
This commit is contained in:
1 parent
693f49e1b0
commit
7c193d8ad7
1 file changed
+10
-14
@@ -4,6 +4,8 @@ import {DeviceDetectorService} from '../shared/services/device-detector.service'
|
|||||||
import {Title} from '@angular/platform-browser';
|
import {Title} from '@angular/platform-browser';
|
||||||
import {isPlatformBrowser, NgIf, NgFor, NgStyle} from '@angular/common';
|
import {isPlatformBrowser, NgIf, NgFor, NgStyle} from '@angular/common';
|
||||||
import {TranslatePipe, TranslateService} from '@ngx-translate/core';
|
import {TranslatePipe, TranslateService} from '@ngx-translate/core';
|
||||||
|
import {HttpClient} from '@angular/common/http';
|
||||||
|
import {Observable, tap} from 'rxjs';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-services',
|
selector: 'app-services',
|
||||||
@@ -19,6 +21,7 @@ import {TranslatePipe, TranslateService} from '@ngx-translate/core';
|
|||||||
|
|
||||||
export class ServicesComponent implements OnInit, OnDestroy, AfterViewInit {
|
export class ServicesComponent implements OnInit, OnDestroy, AfterViewInit {
|
||||||
private readonly elementId = 'vanta-services-bg';
|
private readonly elementId = 'vanta-services-bg';
|
||||||
|
private apiUrl = '/api/stripe/create-checkout-session';
|
||||||
isMobile: boolean = false;
|
isMobile: boolean = false;
|
||||||
activeTab: string[] = [];
|
activeTab: string[] = [];
|
||||||
|
|
||||||
@@ -35,6 +38,7 @@ export class ServicesComponent implements OnInit, OnDestroy, AfterViewInit {
|
|||||||
private deviceService: DeviceDetectorService,
|
private deviceService: DeviceDetectorService,
|
||||||
private titleService: Title,
|
private titleService: Title,
|
||||||
private translate: TranslateService,
|
private translate: TranslateService,
|
||||||
|
private http: HttpClient,
|
||||||
@Inject(PLATFORM_ID) private platformId: Object
|
@Inject(PLATFORM_ID) private platformId: Object
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@@ -150,24 +154,16 @@ export class ServicesComponent implements OnInit, OnDestroy, AfterViewInit {
|
|||||||
this.vantaService.resizeVanta(this.elementId);
|
this.vantaService.resizeVanta(this.elementId);
|
||||||
}
|
}
|
||||||
|
|
||||||
checkout(priceId: string): void {
|
checkout(priceId: string): Observable<any> {
|
||||||
fetch('http://localhost:7020/api/stripe/create-checkout-session', {
|
return this.http.post<any>(this.apiUrl, { priceId }).pipe(
|
||||||
method: 'POST',
|
tap((response) => {
|
||||||
headers: { 'Content-Type': 'application/json' },
|
if (response.url) {
|
||||||
body: JSON.stringify({ priceId })
|
window.location.href = response.url;
|
||||||
})
|
|
||||||
.then(res => res.json())
|
|
||||||
.then(data => {
|
|
||||||
if (data.url) {
|
|
||||||
window.location.href = data.url;
|
|
||||||
} else {
|
} else {
|
||||||
alert('Checkout session failed.');
|
alert('Checkout session failed.');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(err => {
|
);
|
||||||
console.error('Stripe error:', err);
|
|
||||||
alert('Error creating checkout session.');
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getActivePriceId(service: any, activeTab: string): string | null {
|
getActivePriceId(service: any, activeTab: string): string | null {
|
||||||
|
|||||||
Reference in new issue
Block a user