expressCart/lib/modules/shipping-basic.js

43 lines
1.3 KiB
JavaScript

const domesticShippingAmount = 10;
const internationalShippingAmount = 25;
const freeThreshold = 100;
const shippingFromCountry = 'Australia';
const calculateShipping = (amount, config, req) => {
// When set to instore shipping is not applicable.
if(config.paymentGateway === 'instore'){
// Update message and amount
req.session.shippingMessage = 'Instore pickup';
req.session.totalCartShipping = 0;
return;
}
if(amount >= freeThreshold){
req.session.shippingMessage = 'FREE shipping';
req.session.totalCartShipping = 0;
return;
}
// If there is no country set, we estimate shipping
if(!req.session.customerCountry){
req.session.shippingMessage = 'Estimated shipping';
req.session.totalCartShipping = domesticShippingAmount;
return;
}
// Check for international
if(req.session.customerCountry.toLowerCase() !== shippingFromCountry.toLowerCase()){
req.session.shippingMessage = 'International shipping';
req.session.totalCartShipping = internationalShippingAmount;
return;
}
// Domestic shipping
req.session.shippingMessage = 'Domestic shipping';
req.session.totalCartShipping = domesticShippingAmount;
};
module.exports = {
calculateShipping
};