Files
qr_vitanova/src/app/pages/create-page/create-page.ts
2026-05-04 23:56:38 +04:00

105 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Component, inject, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Router, RouterLink } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { FastcheckService } from '../../fastcheck.service';
import { FASTCHECK_API } from '../../api';
import { TranslatePipe } from '../../translate/translate.pipe';
interface CreateFastcheckResponse {
fastcheck: string;
expiration: string;
code: string;
Status: boolean;
}
type PaymentMethod = 'sbp' | 'wechat' | 'visa' | 'master';
type Currency = 'RUB' | 'CNY' | 'USD' | 'EUR' | 'AMD';
@Component({
selector: 'app-create-page',
imports: [FormsModule, RouterLink, TranslatePipe],
templateUrl: './create-page.html',
styleUrl: './create-page.scss'
})
export class CreatePage {
private http = inject(HttpClient);
private store = inject(FastcheckService);
private router = inject(Router);
amount = signal<number>(10);
note = signal<string>('');
error = signal<string>('');
loading = signal<boolean>(false);
payment = signal<PaymentMethod>('sbp');
currency = signal<Currency>('RUB');
/** sessionID for the Authorization header. Comes from ?session=... or websession. */
private get sessionId(): string {
return new URLSearchParams(window.location.search).get('session') ?? '';
}
selectPayment(method: PaymentMethod, enabled: boolean): void {
if (!enabled) return;
this.payment.set(method);
}
selectCurrency(c: Currency, enabled: boolean): void {
if (!enabled) return;
this.currency.set(c);
}
createCheck(): void {
const val = this.amount();
if (!val || val <= 0) {
this.error.set('Введите корректную сумму');
return;
}
this.error.set('');
this.loading.set(true);
const headers: Record<string, string> = {};
if (this.sessionId) {
headers['Authorization'] = JSON.stringify({ sessionID: this.sessionId });
}
this.http
.post<CreateFastcheckResponse>(
`${FASTCHECK_API}/fastcheck`,
{ amount: val, currency: this.currency() },
{ headers }
)
.subscribe({
next: (res) => {
this.loading.set(false);
if (res?.fastcheck) {
this.store.setCreated({
fastcheck: res.fastcheck,
code: res.code,
amount: val,
expiration: res.expiration
});
this.router.navigate(['/']);
} else {
this.error.set('Не удалось создать платёж.');
}
},
error: () => {
this.loading.set(false);
this.error.set('Ошибка при создании платежа. Попробуйте ещё раз.');
}
});
}
onAmountChange(value: number): void {
this.amount.set(value);
if (value > 0) this.error.set('');
}
onNoteChange(value: string): void {
this.note.set(value);
}
}