feat: похуйу

This commit is contained in:
2026-05-12 23:43:22 +03:00
parent b7900fcdf3
commit a237e3574f

View File

@@ -1,7 +1,38 @@
import { getCsrfToken } from '@shared/api/csrf' import { getCsrfToken } from '@shared/api/csrf'
import { refreshAccessToken, tokenStore } from '@shared/api/tokenStore'
const PAYMENT_API_URL = 'https://app.payment.elcsa.ru' const PAYMENT_API_URL = 'https://app.payment.elcsa.ru'
async function doPaymentRequest<T>(
path: string,
options: RequestInit,
allowRetry: boolean,
): Promise<T> {
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<T>(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 { export interface PaymentConfig {
status_code: number status_code: number
usdt_exchange_rate: string usdt_exchange_rate: string
@@ -23,26 +54,12 @@ export interface PaymentQuote {
created_at: string created_at: string
} }
export async function getPaymentConfig(): Promise<PaymentConfig> { export function getPaymentConfig(): Promise<PaymentConfig> {
const csrf = await getCsrfToken() return doPaymentRequest('/payment/config', {}, true)
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 async function getPaymentQuote(usdtAmount: number): Promise<PaymentQuote> { export function getPaymentQuote(usdtAmount: number): Promise<PaymentQuote> {
const csrf = await getCsrfToken() return doPaymentRequest(`/payment/quote?usdt_amount=${usdtAmount}`, {}, true)
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 interface CreateOrderPayload { export interface CreateOrderPayload {
@@ -79,18 +96,10 @@ export interface OrderResult {
} }
} }
export async function createOrder(payload: CreateOrderPayload): Promise<OrderResult> { export function createOrder(payload: CreateOrderPayload): Promise<OrderResult> {
const csrf = await getCsrfToken() return doPaymentRequest('/order/create', {
const res = await fetch(`${PAYMENT_API_URL}/order/create`, {
method: 'POST', method: 'POST',
credentials: 'include', headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': csrf,
},
body: JSON.stringify(payload), body: JSON.stringify(payload),
}) }, true)
const data = await res.json()
if (!res.ok) throw data
return data
} }