diff --git a/src/lib/royalmail-shipping.ts b/src/lib/royalmail-shipping.ts index b1432c4..4c59e57 100644 --- a/src/lib/royalmail-shipping.ts +++ b/src/lib/royalmail-shipping.ts @@ -44,17 +44,11 @@ export async function getRoyalMailShippingQuotes( const weightKg = weightGrams / 1000; const isInternational = !UK_COUNTRIES.includes(destinationCountry.toUpperCase()); - try { - console.log('🚀 Fetching Royal Mail rates from open-source API...'); - const rates = await fetchOpenSourceRoyalMailRates(weightGrams, destinationCountry); - console.log('✅ Royal Mail API SUCCESS - Got', rates.length, 'shipping options'); - return rates; - } catch (error) { - console.error('❌ Royal Mail API FAILED:', error instanceof Error ? error.message : String(error)); - console.log('📦 Falling back to estimated rates'); - // Fall back to estimated rates if API fails - return calculateEstimatedRates(weightKg, isInternational, destinationCountry); - } + // Use calculated estimated rates based on Royal Mail's standard pricing + // Note: Royal Mail doesn't provide a direct real-time pricing API + // Instead, they expect you to build your own rate matrix using their OBA rates + console.log('📦 Using Royal Mail estimated rates (based on standard pricing)'); + return calculateEstimatedRates(weightKg, isInternational, destinationCountry); } function calculateEstimatedRates( @@ -114,59 +108,6 @@ function calculateEstimatedRates( return quotes; } -/** - * Fetch Royal Mail shipping rates using open-source Royal Mail Price API - * This uses the community-maintained Royal Mail price matrix - */ -async function fetchOpenSourceRoyalMailRates( - weightGrams: number, - destinationCountry: string -): Promise { - try { - const isInternational = !UK_COUNTRIES.includes(destinationCountry.toUpperCase()); - const endpoint = 'https://royalmail-api.herokuapp.com/prices'; - - // Call open-source Royal Mail API - const response = await fetch(endpoint, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - weight_grams: Math.ceil(weightGrams), - destination_country: destinationCountry.toUpperCase(), - service_type: isInternational ? 'international' : 'domestic', - }), - }); - - if (!response.ok) { - throw new Error(`Royal Mail API error: ${response.status} ${response.statusText}`); - } - - const data = (await response.json()) as { - services?: Array<{ - name: string; - price: number; - estimated_days: number; - }>; - }; - - if (!data.services || data.services.length === 0) { - throw new Error('No services returned from Royal Mail API'); - } - - // Transform API response to our format - return data.services.map((service) => ({ - service: service.name, - price: Math.ceil(service.price), // Ensure it's in pence - estimatedDays: service.estimated_days, - isInternational: isInternational, - })); - } catch (error) { - console.error('Error fetching Royal Mail rates:', error); - throw error; - } -} /** * Format price from pence to pounds string