Last active
December 7, 2017 00:33
-
-
Save amitnarayanan/5d16ea908f9205d6022254b1fdc3da94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
console.log('Loading tax calculator function...'); | |
exports.handler = (event, context, callback) => { | |
console.log('Received event: ', JSON.stringify(event, null, 2)); | |
try { | |
let productPrice, taxRate, surchargeRate = null; | |
if (event.body !== null && event.body !== undefined) { | |
let body = JSON.parse(event.body) | |
productPrice = body.productPrice; | |
taxRate = body.taxRate; | |
surchargeRate = body.surchargeRate; | |
} | |
else { | |
productPrice = event.productPrice; | |
taxRate = event.taxRate; | |
surchargeRate = event.surchargeRate; | |
} | |
console.log(`Product price: $${productPrice}`); | |
console.log(`Tax rate: ${taxRate}%`); | |
console.log(`Surcharge rate: ${surchargeRate}%`); | |
let tax = productPrice * (taxRate / 100.00); | |
let surcharge = productPrice * (surchargeRate / 100.00); | |
let finalPrice = productPrice + tax + surcharge; | |
console.log(`Final price with tax: $${finalPrice}`); | |
let returnValue = { | |
price: finalPrice, | |
state: "CA" | |
} | |
var response = { | |
statusCode: 200, | |
headers: { | |
"x-custom-header" : "Some custom header value" | |
}, | |
body: JSON.stringify(returnValue) | |
}; | |
// On success, invoke the callback like so (2 arguments) | |
// first one being null. | |
callback(null, response); // Return calculated tax | |
} | |
catch(e) { | |
console.log(e); | |
// On failure, invoke the callback like so (a single argument) | |
// with a helpful error message. | |
callback('ERROR: Something went wrong!'); | |
} | |
console.log("Done calculating tax."); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment