28 lines
860 B
TypeScript
28 lines
860 B
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getCountryFromIp, calculateShippingFee } from '@/lib/geolocation'
|
|
import { getCurrencyForCountry } from '@/lib/currency'
|
|
|
|
// GET /api/shipping-fee - Get shipping fee based on user's IP location
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const countryCode = await getCountryFromIp(request)
|
|
const shippingFee = calculateShippingFee(countryCode)
|
|
const currency = getCurrencyForCountry(countryCode)
|
|
|
|
return NextResponse.json({
|
|
shipping_fee: shippingFee,
|
|
country_code: countryCode,
|
|
currency: currency,
|
|
})
|
|
} catch (error) {
|
|
console.error('Error calculating shipping fee:', error)
|
|
// Default to 40 EUR if detection fails
|
|
return NextResponse.json({
|
|
shipping_fee: 40,
|
|
country_code: null,
|
|
currency: 'EUR',
|
|
})
|
|
}
|
|
}
|
|
|