Created
July 15, 2019 12:24
-
-
Save anton-isaykin/114a3e8d74e5a362ed5a5398f382e39d to your computer and use it in GitHub Desktop.
Fee to cover calculations
This file contains hidden or 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
| // AMEX payments percent of total count payments | |
| export const AMEX_PERCENT = 0.1; | |
| // AMEX 3.5% fee | |
| const AMEX_FEE = 0.035; | |
| // USD/USD | |
| const USD_RATE = 1; | |
| // fixed stripe fee 30c | |
| const FIXED_STRIPE_FEE = 0.3; | |
| /** | |
| * Calculate fee amount | |
| * @param {number} amount - Donation amount | |
| * @param {number} rate - Selected currency to USD ratio | |
| * @param {number} [digits=2] - Number of significant digits after the point | |
| * @param {number} [stripeFee=290] - Stripe processing fee percent multiplied by 10000 (0.029 * 10000 === 200) | |
| * @param {number} [applicationFee=200] - Application fee percent multiplied by 10000 (0.02 * 10000 === 200) | |
| * @param {boolean} [isCompanyCurrency=true] - Identifier of currency if it is different from default company currency | |
| * @returns {number} | |
| */ | |
| export function publicFeePercent(amount, rate, digits = 2, stripeFee = 290, applicationFee = 200, isCompanyCurrency = true) { | |
| amount = amount / Math.pow(10, digits); | |
| stripeFee = stripeFee / 10000; | |
| applicationFee = applicationFee / 10000; | |
| if (rate !== USD_RATE) { | |
| // add 3% to currency rate for not USD currency | |
| // to escape miss converting | |
| rate = rate * 1.03; | |
| } | |
| if (!isCompanyCurrency) { | |
| // add 1% to stripe fee for non-default company currency | |
| stripeFee = stripeFee + 0.01; | |
| } | |
| const totalPercent = applicationFee + AMEX_FEE * AMEX_PERCENT + stripeFee * (1 - AMEX_PERCENT); | |
| const amountWithFee = (amount + (FIXED_STRIPE_FEE * rate)) / (1 - totalPercent); | |
| const percent = amountWithFee / amount - 1; | |
| return Math.round(percent * 100) / 100; | |
| } | |
| export function roundFee(fee) { | |
| if (fee < 500) { | |
| fee = Math.ceil(fee / 10) * 10; | |
| } | |
| if (fee > 500) { | |
| fee = Math.ceil(fee / 100) * 100; | |
| } | |
| return fee; | |
| } | |
| export default function publicFee(amount, stripeFee, applicationFee) { | |
| const percent = publicFeePercent(amount, stripeFee, applicationFee); | |
| return roundFee(Math.ceil(amount * percent)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment