Fix: Revert to reliable estimated rates (open-source API unavailable)
- Remove non-functional open-source Royal Mail API integration - Revert to proven estimated rates based on Royal Mail standard pricing - Royal Mail doesn't provide direct real-time pricing API - Merchants expected to build their own rate matrix using OBA rates - Estimated rates are reliable and can be manually updated as needed Checkout now uses consistent, working shipping rates. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 4.5
parent
5ec843da5b
commit
617a5acbb2
@@ -44,18 +44,12 @@ 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
|
||||
// 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(
|
||||
weightKg: number,
|
||||
@@ -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<ShippingQuote[]> {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user