From a237e3574f87a732e3fe49e019c06cdaf9bb01e7 Mon Sep 17 00:00:00 2001 From: rassadin11 Date: Tue, 12 May 2026 23:43:22 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=BF=D0=BE=D1=85=D1=83=D0=B9=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/features/payment/api/paymentApi.ts | 69 +++++++++++++++----------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/src/features/payment/api/paymentApi.ts b/src/features/payment/api/paymentApi.ts index ff91ab0..5c2eb59 100644 --- a/src/features/payment/api/paymentApi.ts +++ b/src/features/payment/api/paymentApi.ts @@ -1,7 +1,38 @@ import { getCsrfToken } from '@shared/api/csrf' +import { refreshAccessToken, tokenStore } from '@shared/api/tokenStore' const PAYMENT_API_URL = 'https://app.payment.elcsa.ru' +async function doPaymentRequest( + path: string, + options: RequestInit, + allowRetry: boolean, +): Promise { + const csrf = await getCsrfToken() + + const res = await fetch(`${PAYMENT_API_URL}${path}`, { + ...options, + credentials: 'include', + headers: { + 'X-CSRF-Token': csrf, + ...options.headers, + }, + }) + + if (res.status === 401 && allowRetry) { + try { + await refreshAccessToken() + return doPaymentRequest(path, options, false) + } catch { + throw new Error('Unauthorized') + } + } + + const data = await res.json() + if (!res.ok) throw data + return data as T +} + export interface PaymentConfig { status_code: number usdt_exchange_rate: string @@ -23,26 +54,12 @@ export interface PaymentQuote { created_at: string } -export async function getPaymentConfig(): Promise { - const csrf = await getCsrfToken() - const res = await fetch(`${PAYMENT_API_URL}/payment/config`, { - credentials: 'include', - headers: { 'X-CSRF-Token': csrf }, - }) - const data = await res.json() - if (!res.ok) throw data - return data +export function getPaymentConfig(): Promise { + return doPaymentRequest('/payment/config', {}, true) } -export async function getPaymentQuote(usdtAmount: number): Promise { - const csrf = await getCsrfToken() - const res = await fetch(`${PAYMENT_API_URL}/payment/quote?usdt_amount=${usdtAmount}`, { - credentials: 'include', - headers: { 'X-CSRF-Token': csrf }, - }) - const data = await res.json() - if (!res.ok) throw data - return data +export function getPaymentQuote(usdtAmount: number): Promise { + return doPaymentRequest(`/payment/quote?usdt_amount=${usdtAmount}`, {}, true) } export interface CreateOrderPayload { @@ -79,18 +96,10 @@ export interface OrderResult { } } -export async function createOrder(payload: CreateOrderPayload): Promise { - const csrf = await getCsrfToken() - const res = await fetch(`${PAYMENT_API_URL}/order/create`, { +export function createOrder(payload: CreateOrderPayload): Promise { + return doPaymentRequest('/order/create', { method: 'POST', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - 'X-CSRF-Token': csrf, - }, + headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), - }) - const data = await res.json() - if (!res.ok) throw data - return data + }, true) }