Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 3 min read

Why Naive Reverse-Pricing Breaks on Marketplace Fees (and How I Solved It in Zero-Dep TypeScript)

If you have ever built a seller margin calculator or freelance quote tool, you have probably written this formula at least once: const requiredPrice = (desiredNet + fixedFee) / (1 - percentageFee); It looks clea

If you have ever built a seller margin calculator or freelance quote tool, you have probably written this formula at least once:

const requiredPrice = (desiredNet + fixedFee) / (1 - percentageFee);

It looks cleanβ€”and for a pure linear fee schedule, it is exact.

The problem is that real-world marketplace and payment fee schedules are rarely continuous linear functions. While building a browser-only financial toolkit (ClearMargin) covering 23 marketplaces and all 51 US tax jurisdictions, I ran into three real fee structures where that algebraic one-liner quietly produces the wrong quote.

1. Non-Monotonic Fee Cliffs (The Poshmark $15 Trap)

On Poshmark, sales under $15.00 incur a flat $2.95 commission, while sales $15.00 and above incur a 20% commission.

That creates a weird discontinuity right at the boundary:

  • Sell at $14.99 β†’ $2.95 fee β†’ seller nets $12.04
  • Sell at $15.00 β†’ $3.00 fee β†’ seller nets $12.00

You literally charge the buyer more and keep less.

If a seller asks "What is the lowest price I should list to net $12.02?", the naive 12.02 / 0.80 formula answers $15.03β€”even though $14.97 already nets $12.02 ($14.97 - $2.95).

2. Piecewise Minimums & Caps (Freelancer.com & Stripe ACH)

Two more common piecewise kinks break linear inversion:

  • Minimum floor: Freelancer.com charges max(10%, $5.00) on fixed-price jobs under $50.
  • Hard fee cap: Stripe ACH Direct Debit charges 0.8%, capped at $5.00 above $625. A naive 1000 / (1 - 0.008) tells you to invoice $1,008.06 to net $1,000, when $1,005.00 already nets $1,000.00 exactly.

The Fix: Binary Search + Below-Cliff Boundary Probing

Instead of maintaining fragile algebraic inverses for 23 different platforms (each with optional add-ons like Etsy Offsite Ads or promoted listings), I kept one single forward function calculatePlatformFees() as the source of truth and inverted it numerically:

  1. Bracket & binary search (64 iterations) to find the tight upper bound where netProfit(P) >= target.
  2. Round up to the penny (Math.ceil(hi * 100) / 100).
  3. Probe below discontinuous cliffs: if a platform tier has an underAmountThreshold (like Poshmark’s $15), check whether the maximum price strictly below the cliff ($14.99) already satisfies >= target; if so, run a second binary search strictly inside [0, belowCliffPrice] and take the lower valid price.
  4. 1-cent step-down check so floating-point ceiling never overshoots by $0.01.

Here is the core cliff-handling branch from src/lib/platformEngine.ts:

if (
  tier.underAmountThreshold !== undefined &&
  tier.underAmountFlatFee !== undefined
) {
  const belowCliffPrice = Math.max(
    0.01,
    Math.round((tier.underAmountThreshold - shippingBuyer - 0.01) * 100) / 100
  );
  if (belowCliffPrice > 0 && evalPrice(belowCliffPrice).netProfit >= target) {
    let cLo = 0;
    let cHi = belowCliffPrice;
    for (let i = 0; i < 48; i++) {
      const mid = (cLo + cHi) / 2;
      if (evalPrice(mid).netProfit >= target) cHi = mid;
      else cLo = mid;
    }
    const lowerValid = Math.ceil(cHi * 100) / 100;
    if (lowerValid < candidate && evalPrice(lowerValid).netProfit >= target) {
      candidate = lowerValid;
    }
  }
}

Because both the forward calculation and the reverse solver run through the exact same calculatePlatformFees() engine, every platformβ€”including add-ons, COGS, and shippingβ€”gets verified against the forward breakdown in CI.

Open-Sourcing the Engine (MIT, Zero Dependencies)

Financial calculators live or die on trust. Rather than keeping the formulas hidden inside minified frontend bundles, I extracted the entire calculation engine and 2026 datasets into a standalone, zero-dependency ESM TypeScript package:

npm install clearmargin-tax-engine

Quick Examples

Reverse pricing on Poshmark ($12.02 target) & Stripe ACH ($1,000 target):

import { PLATFORM_MAP, solveRequiredListingPrice } from 'clearmargin-tax-engine';

const posh = solveRequiredListingPrice(PLATFORM_MAP.poshmark, 12.02, {});
// requiredSalePrice: 14.97, platformFee: 2.95, netProfit: 12.02

const stripeAch = solveRequiredListingPrice(PLATFORM_MAP.stripe, 1000, {
  tierId: 'ach-direct',
});
// requiredSalePrice: 1005, processingFee: 5, netProfit: 1000

2026 IRS Schedule SE + 51-Jurisdiction State Tax Engine (92.35% SE factor, $184,500 Social Security wage cap, 50% SE deduction, Section 199A 20% QBI, and $0.70/mile gig mileage rate):

import { calculateFederalAndState1099 } from 'clearmargin-tax-engine';

const ca = calculateFederalAndState1099({
  grossRevenue: 120000,
  businessExpenses: 12000,
  stateSlug: 'california',
  filingStatus: 'single',
  applyQbiDeduction: true,
});
// netProfit: $108,000 | SE Tax: $15,259.91 | Fed: $9,543.53 | CA State: $5,346.12 | Net: $77,850.44
// Compared to Texas ($0 state tax β†’ $83,196.56 net): a $5,346.12/yr difference

You can also see that comparison interactively on California vs Texas 1099 Tax Comparison.

Feedback & PRs Welcome

Every dataset is strictly for planning estimation (not CPA or legal advice) and links directly to primary IRS/SSA documentation and official marketplace fee schedules in src/data/officialSources.ts.

If you spot a mid-year platform fee tweak, a state conformity edge case, or another non-monotonic fee schedule worth modeling, issues and PRs on GitHub are very welcome!

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.