Skip to content

Instantly share code, notes, and snippets.

@qutek
Created February 3, 2018 06:20
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save qutek/c3954950798ae14d6caabd6ba15b302b to your computer and use it in GitHub Desktop.
Save qutek/c3954950798ae14d6caabd6ba15b302b to your computer and use it in GitHub Desktop.
[Calculate Stripe Fee] Calculate stripe fee to ask customer to cover stripe fee #javascript #stripe
/**
* Calculate stripe fee from amount
* so you can charge stripe fee to customers
* lafif <hello@lafif.me>
*/
var fees = {
USD: { Percent: 2.9, Fixed: 0.30 },
GBP: { Percent: 2.4, Fixed: 0.20 },
EUR: { Percent: 2.4, Fixed: 0.24 },
CAD: { Percent: 2.9, Fixed: 0.30 },
AUD: { Percent: 2.9, Fixed: 0.30 },
NOK: { Percent: 2.9, Fixed: 2 },
DKK: { Percent: 2.9, Fixed: 1.8 },
SEK: { Percent: 2.9, Fixed: 1.8 },
JPY: { Percent: 3.6, Fixed: 0 },
MXN: { Percent: 3.6, Fixed: 3 }
};
function calcFee(amount, currency) {
var _fee = fees[currency];
var amount = parseFloat(amount);
var total = (amount + parseFloat(_fee.Fixed)) / (1 - parseFloat(_fee.Percent) / 100);
var fee = total - amount;
return {
amount: amount,
fee: fee.toFixed(2),
total: total.toFixed(2)
};
}
var charge_data = calcFee(100, 'USD');
alert('You should ask: ' + charge_data.total + ' to customer, to cover ' + charge_data.fee + ' fee from ' + charge_data.amount );
console.log(charge_data);
@emanuelet
Copy link

emanuelet commented Oct 21, 2019

I tested the code and it doesn't return the correct amounts that are actually calculated by Stripe.
Here's my revised code:

var amount = parseFloat(amount)
var fee = amount * (parseFloat(_fee.Percent) / 100) + parseFloat(_fee.Fixed)
var total = amount + fee

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