Add files via upload

This commit is contained in:
Egor
2026-01-18 05:33:46 +03:00
committed by GitHub
parent 339ac12b4d
commit 8d9257d904
2 changed files with 50 additions and 42 deletions

View File

@@ -1,6 +1,8 @@
// Currency exchange rate API // Currency exchange rate API
// Uses free exchangerate.host API // Uses free exchangerate.host API
import axios from 'axios'
interface ExchangeRates { interface ExchangeRates {
USD: number USD: number
CNY: number CNY: number
@@ -33,39 +35,38 @@ export const currencyApi = {
try { try {
// Try primary API (exchangerate.host) - get RUB based rates // Try primary API (exchangerate.host) - get RUB based rates
const response = await fetch( const response = await axios.get<{
'https://api.exchangerate.host/latest?base=RUB&symbols=USD,CNY,IRR' success?: boolean
) rates?: { USD?: number; CNY?: number; IRR?: number }
}>('https://api.exchangerate.host/latest', {
params: { base: 'RUB', symbols: 'USD,CNY,IRR' },
})
if (response.ok) { if (response.data.success && response.data.rates) {
const data = await response.json() // API returns how much of each currency equals 1 RUB
if (data.success && data.rates) { // We need the inverse: how many RUB equals 1 of each currency
// API returns how much of each currency equals 1 RUB const rates: ExchangeRates = {
// We need the inverse: how many RUB equals 1 of each currency USD: response.data.rates.USD ? 1 / response.data.rates.USD : FALLBACK_RATES.USD,
const rates: ExchangeRates = { CNY: response.data.rates.CNY ? 1 / response.data.rates.CNY : FALLBACK_RATES.CNY,
USD: data.rates.USD ? 1 / data.rates.USD : FALLBACK_RATES.USD, IRR: response.data.rates.IRR ? 1 / response.data.rates.IRR : FALLBACK_RATES.IRR,
CNY: data.rates.CNY ? 1 / data.rates.CNY : FALLBACK_RATES.CNY,
IRR: data.rates.IRR ? 1 / data.rates.IRR : FALLBACK_RATES.IRR,
}
cachedRates = { rates, timestamp: Date.now() }
return rates
} }
cachedRates = { rates, timestamp: Date.now() }
return rates
} }
// Try backup API (open.er-api.com) // Try backup API (open.er-api.com)
const backupResponse = await fetch('https://open.er-api.com/v6/latest/RUB') const backupResponse = await axios.get<{
rates?: { USD?: number; CNY?: number; IRR?: number }
}>('https://open.er-api.com/v6/latest/RUB')
if (backupResponse.ok) { if (backupResponse.data.rates) {
const backupData = await backupResponse.json() const rates: ExchangeRates = {
if (backupData.rates) { USD: backupResponse.data.rates.USD ? 1 / backupResponse.data.rates.USD : FALLBACK_RATES.USD,
const rates: ExchangeRates = { CNY: backupResponse.data.rates.CNY ? 1 / backupResponse.data.rates.CNY : FALLBACK_RATES.CNY,
USD: backupData.rates.USD ? 1 / backupData.rates.USD : FALLBACK_RATES.USD, IRR: backupResponse.data.rates.IRR ? 1 / backupResponse.data.rates.IRR : FALLBACK_RATES.IRR,
CNY: backupData.rates.CNY ? 1 / backupData.rates.CNY : FALLBACK_RATES.CNY,
IRR: backupData.rates.IRR ? 1 / backupData.rates.IRR : FALLBACK_RATES.IRR,
}
cachedRates = { rates, timestamp: Date.now() }
return rates
} }
cachedRates = { rates, timestamp: Date.now() }
return rates
} }
// Return fallback rates if both APIs fail // Return fallback rates if both APIs fail

View File

@@ -1,4 +1,6 @@
export interface MiniappCreatePaymentPayload { import axios, { AxiosError } from 'axios'
export interface MiniappCreatePaymentPayload {
initData: string initData: string
method: string method: string
amountKopeks?: number | null amountKopeks?: number | null
@@ -16,21 +18,26 @@ export const miniappApi = {
createPayment: async ( createPayment: async (
payload: MiniappCreatePaymentPayload payload: MiniappCreatePaymentPayload
): Promise<MiniappCreatePaymentResponse> => { ): Promise<MiniappCreatePaymentResponse> => {
const res = await fetch('/miniapp/payments/create', { try {
method: 'POST', const response = await axios.post<MiniappCreatePaymentResponse>(
headers: { 'Content-Type': 'application/json' }, '/miniapp/payments/create',
body: JSON.stringify({ {
initData: payload.initData || '', initData: payload.initData || '',
method: payload.method, method: payload.method,
amountKopeks: payload.amountKopeks ?? null, amountKopeks: payload.amountKopeks ?? null,
option: payload.option ?? null, option: payload.option ?? null,
}), },
}) {
const data = await res.json().catch(() => ({})) headers: { 'Content-Type': 'application/json' },
if (!res.ok) { }
const message = (data && (data.detail || data.message)) || 'Failed to create payment' )
throw new Error(String(message)) return response.data
} catch (error) {
const axiosError = error as AxiosError<{ detail?: string; message?: string }>
const message = axiosError.response?.data?.detail
|| axiosError.response?.data?.message
|| 'Failed to create payment'
throw new Error(message)
} }
return data as MiniappCreatePaymentResponse
}, },
} }