Skip to content

Instantly share code, notes, and snippets.

@deckarts
Last active July 14, 2020 18:23
Show Gist options
  • Save deckarts/65418b44eeb789ff8e5fc0df1f900421 to your computer and use it in GitHub Desktop.
Save deckarts/65418b44eeb789ff8e5fc0df1f900421 to your computer and use it in GitHub Desktop.
((arg) => {
const USD = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2 })
if (isNaN(Number(arg)) || arg < 0) { return; } //fail test the initial value
let amt = Number(arg);
function fee (num) { return num * .029 + .3; }
do { amt += .01; } while (amt - fee(amt) < arg);
console.log(`
Add a ${USD.format(fee(amt))} processing fee to collect ${USD.format(arg)}
Total then becomes ${USD.format(Number(arg) + fee(amt))}
`);
})(process.argv[2]); //initial value
@deckarts
Copy link
Author

deckarts commented Apr 21, 2020

Ever wanted to calculate processing fees in order to collect a certain amount from a client when they're paying by credit card? Assuming the most common fees of 2.9% plus $0.30 per transaction, like PayPal, this Javascript can be used with a Node runtime environment and an argument such as $ node calculatepp.js 1795, or with an alias like $ alias paypal="node ~/path/to/calculatepp.js". Write that alias in your (rc) runtime commands file and you have it available at your next (or source refreshed) shell session prompt with $ paypal 1795.

=> Add a $53.92 processing fee to collect $1,795.00
=> Total then becomes $1,848.92

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment