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(10); note = signal(''); error = signal(''); loading = signal(false); payment = signal('sbp'); currency = signal('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 = {}; if (this.sessionId) { headers['Authorization'] = JSON.stringify({ sessionID: this.sessionId }); } this.http .post( `${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); } }