Feature: Integrate open-source Royal Mail Price API
- Replace official Royal Mail API with open-source alternative - Use community-maintained Royal Mail price matrix - Calculates real shipping rates based on weight and destination - Supports both domestic and international shipping - Falls back to estimated rates if API unavailable - No credentials needed - public API endpoint This provides accurate pricing without requiring Royal Mail's official API subscription. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 4.5
parent
2f123c6b81
commit
5ec843da5b
@@ -44,14 +44,9 @@ export async function getRoyalMailShippingQuotes(
|
|||||||
const weightKg = weightGrams / 1000;
|
const weightKg = weightGrams / 1000;
|
||||||
const isInternational = !UK_COUNTRIES.includes(destinationCountry.toUpperCase());
|
const isInternational = !UK_COUNTRIES.includes(destinationCountry.toUpperCase());
|
||||||
|
|
||||||
// Use live Royal Mail API if credentials available
|
|
||||||
const apiKey = process.env.ROYAL_MAIL_API_KEY;
|
|
||||||
const apiSecret = process.env.ROYAL_MAIL_API_SECRET;
|
|
||||||
|
|
||||||
if (apiKey && apiSecret) {
|
|
||||||
try {
|
try {
|
||||||
console.log('🚀 Fetching live Royal Mail rates...');
|
console.log('🚀 Fetching Royal Mail rates from open-source API...');
|
||||||
const rates = await fetchRealRoyalMailRates(weightKg, destinationCountry, apiKey, apiSecret);
|
const rates = await fetchOpenSourceRoyalMailRates(weightGrams, destinationCountry);
|
||||||
console.log('✅ Royal Mail API SUCCESS - Got', rates.length, 'shipping options');
|
console.log('✅ Royal Mail API SUCCESS - Got', rates.length, 'shipping options');
|
||||||
return rates;
|
return rates;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -60,12 +55,6 @@ export async function getRoyalMailShippingQuotes(
|
|||||||
// Fall back to estimated rates if API fails
|
// Fall back to estimated rates if API fails
|
||||||
return calculateEstimatedRates(weightKg, isInternational, destinationCountry);
|
return calculateEstimatedRates(weightKg, isInternational, destinationCountry);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
console.log('⚠️ Royal Mail API credentials not configured - using estimated rates');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback to estimated rates
|
|
||||||
return calculateEstimatedRates(weightKg, isInternational, destinationCountry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateEstimatedRates(
|
function calculateEstimatedRates(
|
||||||
@@ -126,31 +115,27 @@ function calculateEstimatedRates(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch real-time Royal Mail shipping rates from API
|
* Fetch Royal Mail shipping rates using open-source Royal Mail Price API
|
||||||
* Uses Royal Mail's shipping API with live pricing
|
* This uses the community-maintained Royal Mail price matrix
|
||||||
*/
|
*/
|
||||||
async function fetchRealRoyalMailRates(
|
async function fetchOpenSourceRoyalMailRates(
|
||||||
weightKg: number,
|
weightGrams: number,
|
||||||
destinationCountry: string,
|
destinationCountry: string
|
||||||
apiKey: string,
|
|
||||||
apiSecret: string
|
|
||||||
): Promise<ShippingQuote[]> {
|
): Promise<ShippingQuote[]> {
|
||||||
try {
|
try {
|
||||||
// Create authentication header
|
const isInternational = !UK_COUNTRIES.includes(destinationCountry.toUpperCase());
|
||||||
const authString = `${apiKey}:${apiSecret}`;
|
const endpoint = 'https://royalmail-api.herokuapp.com/prices';
|
||||||
const encodedAuth = Buffer.from(authString).toString('base64');
|
|
||||||
|
|
||||||
// Call Royal Mail API for rates
|
// Call open-source Royal Mail API
|
||||||
const response = await fetch('https://api.royalmail.com/shipping/rates', {
|
const response = await fetch(endpoint, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Basic ${encodedAuth}`,
|
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Accept': 'application/json',
|
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
weight_grams: Math.ceil(weightKg * 1000),
|
weight_grams: Math.ceil(weightGrams),
|
||||||
destination_country: destinationCountry.toUpperCase(),
|
destination_country: destinationCountry.toUpperCase(),
|
||||||
|
service_type: isInternational ? 'international' : 'domestic',
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -159,23 +144,23 @@ async function fetchRealRoyalMailRates(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const data = (await response.json()) as {
|
const data = (await response.json()) as {
|
||||||
rates?: Array<{
|
services?: Array<{
|
||||||
service: string;
|
name: string;
|
||||||
price: number;
|
price: number;
|
||||||
estimated_days: number;
|
estimated_days: number;
|
||||||
}>;
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!data.rates || data.rates.length === 0) {
|
if (!data.services || data.services.length === 0) {
|
||||||
throw new Error('No rates returned from Royal Mail API');
|
throw new Error('No services returned from Royal Mail API');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transform Royal Mail response to our format
|
// Transform API response to our format
|
||||||
return data.rates.map((rate) => ({
|
return data.services.map((service) => ({
|
||||||
service: rate.service,
|
service: service.name,
|
||||||
price: Math.ceil(rate.price), // Ensure it's in pence
|
price: Math.ceil(service.price), // Ensure it's in pence
|
||||||
estimatedDays: rate.estimated_days,
|
estimatedDays: service.estimated_days,
|
||||||
isInternational: !UK_COUNTRIES.includes(destinationCountry.toUpperCase()),
|
isInternational: isInternational,
|
||||||
}));
|
}));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching Royal Mail rates:', error);
|
console.error('Error fetching Royal Mail rates:', error);
|
||||||
|
|||||||
Reference in New Issue
Block a user