api change
This commit is contained in:
@@ -1,10 +1,62 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||
import { Observable, timer } from 'rxjs';
|
||||
import { map, retry } from 'rxjs/operators';
|
||||
import { catchError, 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 {
|
||||
amount: number;
|
||||
currency: string;
|
||||
siteuserID: string;
|
||||
siteorderID: string;
|
||||
redirectUrl: string;
|
||||
telegramUsername: string;
|
||||
items: PaymentRequestItem[];
|
||||
}
|
||||
|
||||
export interface PaymentCreateResponse {
|
||||
qrId?: string;
|
||||
qrID?: string;
|
||||
qrStatus?: string;
|
||||
qrExpirationDate?: string;
|
||||
payload?: string;
|
||||
Payload?: string;
|
||||
qrUrl?: string;
|
||||
partnerID?: string | number;
|
||||
partnerId?: string | number;
|
||||
PartnerID?: string | number;
|
||||
}
|
||||
|
||||
export interface PaymentCreateResult {
|
||||
backend: PaymentBackend;
|
||||
response: PaymentCreateResponse;
|
||||
}
|
||||
|
||||
export interface PaymentStatusResponse {
|
||||
additionalInfo: string;
|
||||
paymentPurpose: string;
|
||||
amount: number;
|
||||
code: string;
|
||||
createDate: string;
|
||||
currency: string;
|
||||
order: string;
|
||||
paymentStatus: string;
|
||||
qrId: string;
|
||||
transactionDate: string;
|
||||
transactionId: number;
|
||||
qrExpirationDate: string;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
@@ -348,51 +400,74 @@ export class ApiService {
|
||||
return this.http.post<{ message: string }>(`${this.baseUrl}/items/${itemID}/questiion`, body);
|
||||
}
|
||||
|
||||
// Payment - SBP Integration via websession QR
|
||||
createPayment(sessionId: string): Observable<{
|
||||
qrId: string;
|
||||
qrStatus: string;
|
||||
qrExpirationDate: string;
|
||||
Payload: string;
|
||||
qrUrl: string;
|
||||
}> {
|
||||
return this.http.post<{
|
||||
qrId: string;
|
||||
qrStatus: string;
|
||||
qrExpirationDate: string;
|
||||
Payload: string;
|
||||
qrUrl: string;
|
||||
}>(`${this.baseUrl}/websession/${sessionId}/qr`, {});
|
||||
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$)
|
||||
);
|
||||
|
||||
if (!sessionId) {
|
||||
return directPayment$;
|
||||
}
|
||||
|
||||
return this.http.post<PaymentCreateResponse>(`${this.baseUrl}/websession/${sessionId}/qr`, {}).pipe(
|
||||
map(response => ({ backend: 'websession' as const, response })),
|
||||
catchError(() => directPayment$)
|
||||
);
|
||||
}
|
||||
|
||||
checkPaymentStatus(sessionId: string, qrId: string): Observable<{
|
||||
additionalInfo: string;
|
||||
paymentPurpose: string;
|
||||
amount: number;
|
||||
code: string;
|
||||
createDate: string;
|
||||
currency: string;
|
||||
order: string;
|
||||
paymentStatus: string;
|
||||
qrId: string;
|
||||
transactionDate: string;
|
||||
transactionId: number;
|
||||
qrExpirationDate: string;
|
||||
}> {
|
||||
return this.http.get<{
|
||||
additionalInfo: string;
|
||||
paymentPurpose: string;
|
||||
amount: number;
|
||||
code: string;
|
||||
createDate: string;
|
||||
currency: string;
|
||||
order: string;
|
||||
paymentStatus: string;
|
||||
qrId: string;
|
||||
transactionDate: string;
|
||||
transactionId: number;
|
||||
qrExpirationDate: string;
|
||||
}>(`${this.baseUrl}/websession/${sessionId}/${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: PaymentCreateResponse): string {
|
||||
return response.qrId ?? response.qrID ?? '';
|
||||
}
|
||||
|
||||
resolvePaymentQrUrl(response: PaymentCreateResponse): string {
|
||||
if (response.qrUrl) {
|
||||
return response.qrUrl;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
submitPurchaseEmail(emailData: {
|
||||
|
||||
Reference in New Issue
Block a user