This commit is contained in:
sdarbinyan
2026-06-02 01:46:12 +04:00
parent 63b0e18396
commit c6bc05560e
3 changed files with 128 additions and 113 deletions

View File

@@ -1,48 +1,41 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable, timer } from 'rxjs';
import { catchError, map, retry } from 'rxjs/operators';
import { map, retry } from 'rxjs/operators';
import { Category, Item, Subcategory } from '../models';
import { environment } from '../../environments/environment';
type PaymentBackend = 'websession' | 'qr' | 'cart';
interface PaymentRequestItem {
itemID: number;
price: number;
name: string;
quantity?: number;
}
export interface PaymentRequest {
export interface QrCreateRequest {
qrtype: 'QRDynamic';
amount: number;
currency: string;
siteuserID: string;
siteorderID: string;
redirectUrl: string;
telegramUsername: string;
items: PaymentRequestItem[];
currency: 'RUB';
partnerqrID: string;
qrDescription?: string;
Userid?: string;
Reference?: string;
RedirectUrl?: string;
}
export interface PaymentCreateResponse {
export interface QrCreateResponse {
qrId?: string;
qrID?: string;
nspkID?: string;
nspkId?: string;
nspkurl?: string;
status?: string;
qrStatus?: string;
qrExpirationDate?: string;
payload?: string;
Payload?: string;
qrUrl?: string;
partnerqrID?: string | number;
partnerID?: string | number;
partnerId?: string | number;
PartnerID?: string | number;
}
export interface PaymentCreateResult {
backend: PaymentBackend;
response: PaymentCreateResponse;
}
export interface PaymentStatusResponse {
export interface QrStatusResponse {
status?: string;
additionalInfo: string;
paymentPurpose: string;
amount: number;
@@ -400,74 +393,40 @@ export class ApiService {
return this.http.post<{ message: string }>(`${this.baseUrl}/items/${itemID}/questiion`, body);
}
createPayment(paymentData: PaymentRequest, sessionId?: string): Observable<PaymentCreateResult> {
const directQrPayment$ = this.http.post<PaymentCreateResponse>(`${this.baseUrl}/qr`, paymentData).pipe(
map(response => ({ backend: 'qr' as const, response }))
);
const legacyCartPayment$ = this.http.post<PaymentCreateResponse>(`${this.baseUrl}/cart`, paymentData).pipe(
map(response => ({ backend: 'cart' as const, response }))
);
const directPayment$ = directQrPayment$.pipe(
catchError(() => legacyCartPayment$)
);
createPayment(payload: QrCreateRequest, headers?: { authorizationKey?: string; userIdValue?: string }): Observable<QrCreateResponse> {
let httpHeaders = new HttpHeaders();
if (!sessionId) {
return directPayment$;
if (headers?.authorizationKey) {
httpHeaders = httpHeaders.set('authorization-key', headers.authorizationKey);
}
if (headers?.userIdValue) {
httpHeaders = httpHeaders.set('userid-value', headers.userIdValue);
}
return this.http.post<PaymentCreateResponse>(`${this.baseUrl}/websession/${sessionId}/qr`, {}).pipe(
map(response => ({ backend: 'websession' as const, response })),
catchError(() => directPayment$)
return this.http.post<QrCreateResponse>(`${this.baseUrl}/qr`, payload, { headers: httpHeaders });
}
checkPaymentStatus(partnerQrId: string, qrId: string): Observable<QrStatusResponse> {
return this.http.get<QrStatusResponse>(
`${this.baseUrl}/qr/dynamic/${encodeURIComponent(partnerQrId)}/${encodeURIComponent(qrId)}`
);
}
checkPaymentStatus(qrId: string, options?: { sessionId?: string; backend?: PaymentBackend }): Observable<PaymentStatusResponse> {
const legacyStatus$ = this.http.get<PaymentStatusResponse>(`${this.baseUrl}/qr/payment/${qrId}`);
if (options?.backend === 'websession' && options.sessionId) {
return this.http.get<PaymentStatusResponse>(`${this.baseUrl}/websession/${options.sessionId}/${qrId}`).pipe(
catchError(() => legacyStatus$)
);
}
if (options?.backend === 'qr' || options?.backend === 'cart') {
return legacyStatus$;
}
if (options?.sessionId) {
return this.http.get<PaymentStatusResponse>(`${this.baseUrl}/websession/${options.sessionId}/${qrId}`).pipe(
catchError(() => legacyStatus$)
);
}
return legacyStatus$;
resolvePaymentQrId(response: QrCreateResponse): string {
return response.qrId ?? response.qrID ?? response.nspkID ?? response.nspkId ?? '';
}
resolvePaymentQrId(response: PaymentCreateResponse): string {
return response.qrId ?? response.qrID ?? '';
resolvePaymentLink(response: QrCreateResponse): string {
return response.nspkurl ?? response.Payload ?? response.payload ?? response.qrUrl ?? '';
}
resolvePaymentQrUrl(response: PaymentCreateResponse): string {
if (response.qrUrl) {
return response.qrUrl;
resolvePaymentQrUrl(response: QrCreateResponse): string {
const paymentLink = this.resolvePaymentLink(response);
if (paymentLink) {
return `https://api.qrserver.com/v1/create-qr-code/?size=256x256&margin=8&data=${encodeURIComponent(paymentLink)}`;
}
const qrId = this.resolvePaymentQrId(response);
const partnerId = response.partnerID ?? response.partnerId ?? response.PartnerID;
if (!qrId) {
return '';
}
if (partnerId != null) {
return `${this.baseUrl}/qr/dynamic/${encodeURIComponent(String(partnerId))}/${encodeURIComponent(qrId)}`;
}
return `${this.baseUrl}/qr/static/${encodeURIComponent(qrId)}`;
}
resolvePaymentLink(response: PaymentCreateResponse): string {
return response.payload ?? response.Payload ?? this.resolvePaymentQrUrl(response);
return response.qrUrl ?? '';
}
submitPurchaseEmail(emailData: {