Asked 1 month ago by CometOrbiter816
How can I accurately calculate the gross invoice amount including Stripe and application fees in JavaScript?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by CometOrbiter816
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I am using connected accounts on Stripe with JavaScript and need to display to the user the total amount charged for an invoice, starting from a net amount. Specifically, given a net invoice (e.g., $1500), I need to add both the Stripe fee (a fixed fee plus a percentage commission) and my application fee (a percentage commission) so that the total matches exactly what Stripe will charge.
My current script is as follows:
JAVASCRIPTconst stripeCommissionRate = 0.029; const stripeFixedFee = 0.30; const myFeeRate = 0.0146; function calculateFees(netAmount) { // Calculate gross amount to do calculations (reverse engineering from Stripe's math) let grossAmount = (netAmount + stripeFixedFee) / (1 - stripeCommissionRate - myFeeRate); // Calculate individual fees let stripeFee = stripeFixedFee + (grossAmount * stripeCommissionRate); let myFee = grossAmount * myFeeRate; // Round individual fees stripeFee = parseFloat(stripeFee.toFixed(2)); myFee = parseFloat(myFee.toFixed(2)); // Calculate total fees let totalFees = stripeFee + myFee; // Calculate final gross amount from rounded amounts grossAmount = netAmount + stripeFee + myFee; return { grossAmount: grossAmount, stripeFee: stripeFee, myFee: myFee, totalFees: totalFees, }; }
The calculation doesn’t always match Stripe’s exact charges. For example:
grossAmount: 1568.69
stripeFee: 45.79
myAppFee: 22.90
totalFees: 68.69
grossAmount: 1045.90
stripeFee: 30.63
myAppFee: 15.27
totalFees: 45.90
grossAmount: 52.58 <-- on Stripe it is 52.59
stripeFee: 1.83
myAppFee: 0.77
totalFees: 2.59 <-- on Stripe it is 2.60
grossAmount: 313.97 <-- on Stripe it is 313.98
stripeFee: 9.41 <-- on Stripe it is 9.40
myAppFee: 4.58
totalFees: 13.99 <-- on Stripe it is 13.98
Whenever I adjust the formula to fix one case, it ends up breaking another working example. I suspect there may be special rounding rules or a different calculation method to closely match Stripe's internal fee processing. Any insights or recommendations to resolve these discrepancies?
Comments 0
•Answers 4
•Views 92
Answer by TychoAI Agent • 1 month ago
Below is one common approach that has helped others match Stripe’s own calculations:
Instead of doing all your math in dollars (floating point), many developers convert the amounts to cents (integers) and then “solve” for the correct gross amount using an iterative (or search) algorithm. This approach mimics how Stripe works internally—always doing fee math on integer cent amounts and applying rounding rules on each fee component. One strategy is:
The following example demonstrates this methodology using JavaScript:
JAVASCRIPTconst stripeCommissionRate = 0.029; const stripeFixedFeeCents = 30; // $0.30 in cents const myFeeRate = 0.0146; // Helper function: rounds to nearest integer (cent) function roundToCents(amount) { return Math.round(amount); } function calculateFees(netAmountDollars) { // Convert net amount from dollars to cents. const netCents = Math.round(netAmountDollars * 100); // Initial gross estimate. let grossCents = Math.ceil((netCents + stripeFixedFeeCents) / (1 - stripeCommissionRate - myFeeRate)); // Iterate until we find a gross value where: // netCents + stripeFeeCents + myFeeCents === grossCents. while (true) { // Compute the fee components on the gross. const stripeFeeCents = roundToCents(stripeFixedFeeCents + grossCents * stripeCommissionRate); const myFeeCents = roundToCents(grossCents * myFeeRate); if (netCents + stripeFeeCents + myFeeCents === grossCents) { // Matching gross found; break out. return { grossAmount: (grossCents / 100).toFixed(2), stripeFee: (stripeFeeCents / 100).toFixed(2), myFee: (myFeeCents / 100).toFixed(2), totalFees: ((stripeFeeCents + myFeeCents) / 100).toFixed(2) }; } // If not, increment grossCents by one cent and try again. grossCents++; } } // Examples: console.log(calculateFees(1500.00)); // Expected: gross ~1568.68, fees ~68.68 console.log(calculateFees(1000.00)); // Expected: gross ~1045.90, fees ~45.90 console.log(calculateFees(49.99)); // Expected: gross ~52.59, fees ~2.60 console.log(calculateFees(299.98)); // Expected: gross ~313.98, fees ~13.98
This method should now match what Stripe charges for any net amount when using connected accounts.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by VenusianAdventurer004 • 3 months ago
After contacting Stripe support, for what they said, and the examples they provided, this was the solution, in case it helps someone else.
JAVASCRIPTconst stripeCommissionRate = 0.029; const stripeFixedFee = 0.30; const myFeeRate = 0.0146; function calculateFees(netAmount) { // Calculate gross amount to do calculations let grossAmount = (netAmount + stripeFixedFee) / (1 - stripeCommissionRate - myFeeRate); // Calculate individual fees let stripeFee = stripeFixedFee + (grossAmount * stripeCommissionRate); let myFee = grossAmount * myFeeRate; // Round individual fees stripeFee = parseFloat(stripeFee.toFixed(2)); cookieFee = parseFloat(cookieFee.toFixed(2)); // Calculate total fees let totalFees = stripeFee + myFee; // Calculate final gross amount from rounded amounts grossAmount = netAmount + stripeFee + myFee; grossAmount = parseFloat(grossAmount.toFixed(2)); return { grossAmount: grossAmount, stripeFee: stripeFee, myFee: myFee, totalFees: totalFees, }; }
No comments yet.
Answer by OrbitalScholar490 • 3 months ago
A fee can very likely vary depending on different factors.
To me it sounds like this might be useful to you Payment Intent Object.
A PaymentIntent guides you through the process of collecting a payment
from your customer. We recommend that you create exactly one
PaymentIntent for each order or customer session in your system.
Send this before and check out the response attribute application_fee_amount
No comments yet.
Answer by SaturnianWatcher310 • 3 months ago
You should contact Stripe support for guidance about fees: https://support.stripe.com/contact
However, your own calculation rounding appears inconsistent:
Failing example 1 stripeFee: 1.83, myAppFee: 0.77, totalFees: 2.59
Your own calculation has 1.83+0.77 which should be 2.60, so why are you arriving at 2.59? You should verify your addition rounds before summing consistently.
No comments yet.
No comments yet.