feat: похуйу
This commit is contained in:
@@ -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<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 {
|
||||
status_code: number
|
||||
usdt_exchange_rate: string
|
||||
@@ -23,26 +54,12 @@ export interface PaymentQuote {
|
||||
created_at: string
|
||||
}
|
||||
|
||||
export async function getPaymentConfig(): Promise<PaymentConfig> {
|
||||
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<PaymentConfig> {
|
||||
return doPaymentRequest('/payment/config', {}, true)
|
||||
}
|
||||
|
||||
export async function getPaymentQuote(usdtAmount: number): Promise<PaymentQuote> {
|
||||
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<PaymentQuote> {
|
||||
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<OrderResult> {
|
||||
const csrf = await getCsrfToken()
|
||||
const res = await fetch(`${PAYMENT_API_URL}/order/create`, {
|
||||
export function createOrder(payload: CreateOrderPayload): Promise<OrderResult> {
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user