2020-01-04 17:10:58 +10:00
|
|
|
const domesticShippingAmount = 10;
|
|
|
|
const internationalShippingAmount = 25;
|
2020-01-03 18:21:24 +10:00
|
|
|
const freeThreshold = 100;
|
2020-01-04 17:10:58 +10:00
|
|
|
const shippingFromCountry = 'Australia';
|
2020-01-03 18:21:24 +10:00
|
|
|
|
2020-01-04 17:10:58 +10:00
|
|
|
const calculateShipping = (amount, config, req) => {
|
2020-01-03 22:17:04 +10:00
|
|
|
// When set to instore shipping is not applicable.
|
|
|
|
if(config.paymentGateway === 'instore'){
|
2020-01-04 17:10:58 +10:00
|
|
|
// Update message and amount
|
|
|
|
req.session.shippingMessage = 'Instore pickup';
|
|
|
|
req.session.totalCartShipping = 0;
|
2020-01-04 17:26:43 +10:00
|
|
|
req.session.totalCartAmount = req.session.totalCartAmount + 0;
|
2020-01-04 17:10:58 +10:00
|
|
|
return;
|
2020-01-03 22:17:04 +10:00
|
|
|
}
|
2020-01-04 17:10:58 +10:00
|
|
|
|
2020-01-03 18:21:24 +10:00
|
|
|
if(amount >= freeThreshold){
|
2020-01-04 17:10:58 +10:00
|
|
|
req.session.shippingMessage = 'FREE shipping';
|
|
|
|
req.session.totalCartShipping = 0;
|
2020-01-04 17:26:43 +10:00
|
|
|
req.session.totalCartAmount = req.session.totalCartAmount + 0;
|
2020-01-04 17:10:58 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no country set, we estimate shipping
|
|
|
|
if(!req.session.customerCountry){
|
|
|
|
req.session.shippingMessage = 'Estimated shipping';
|
|
|
|
req.session.totalCartShipping = domesticShippingAmount;
|
2020-01-21 17:36:46 +10:00
|
|
|
req.session.totalCartAmount = amount + domesticShippingAmount;
|
2020-01-04 17:10:58 +10:00
|
|
|
return;
|
2020-01-03 18:21:24 +10:00
|
|
|
}
|
2020-01-04 17:10:58 +10:00
|
|
|
|
|
|
|
// Check for international
|
|
|
|
if(req.session.customerCountry.toLowerCase() !== shippingFromCountry.toLowerCase()){
|
|
|
|
req.session.shippingMessage = 'International shipping';
|
|
|
|
req.session.totalCartShipping = internationalShippingAmount;
|
2020-01-21 17:36:46 +10:00
|
|
|
req.session.totalCartAmount = amount + internationalShippingAmount;
|
2020-01-04 17:10:58 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Domestic shipping
|
|
|
|
req.session.shippingMessage = 'Domestic shipping';
|
|
|
|
req.session.totalCartShipping = domesticShippingAmount;
|
2020-01-21 17:36:46 +10:00
|
|
|
req.session.totalCartAmount = amount + domesticShippingAmount;
|
2020-01-03 18:21:24 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
calculateShipping
|
|
|
|
};
|