Files
cbd420/app/api/payments/currencies/route.ts
2025-12-21 17:36:44 +01:00

62 lines
1.9 KiB
TypeScript

import { NextResponse } from 'next/server'
import { getNowPaymentsConfig } from '@/lib/nowpayments'
import { ALLOWED_PAYMENT_CURRENCIES } from '@/lib/payment-currencies'
// GET /api/payments/currencies - Get available payment currencies from NOWPayments
export async function GET() {
try {
const nowPaymentsConfig = getNowPaymentsConfig()
// Fetch available currencies from NOWPayments
const response = await fetch(
`${nowPaymentsConfig.baseUrl}/v1/currencies?fixed_rate=true`,
{
method: 'GET',
headers: {
'x-api-key': nowPaymentsConfig.apiKey,
},
}
)
if (!response.ok) {
const error = await response.json()
console.error('NOWPayments currencies error:', error)
return NextResponse.json(
{ error: 'Failed to fetch available currencies', details: error },
{ status: 500 }
)
}
const data = await response.json()
// Filter currencies to only include the selected list
const currencies = (data.currencies || []).filter((c: any) => {
let currencyCode: string | null = null
// Handle object format (when fixed_rate=true)
if (typeof c === 'object' && c !== null && c.currency) {
currencyCode = String(c.currency).trim().toLowerCase()
}
// Handle string format (when fixed_rate=false)
else if (typeof c === 'string') {
currencyCode = c.trim().toLowerCase()
}
// Check if currency is in the allowed list
return currencyCode && ALLOWED_PAYMENT_CURRENCIES.includes(currencyCode as any)
})
// Return the filtered currencies array
return NextResponse.json({
currencies: currencies,
})
} catch (error) {
console.error('Error fetching currencies:', error)
return NextResponse.json(
{ error: 'Failed to fetch currencies' },
{ status: 500 }
)
}
}