DEV Community

Leon
Leon

Posted on Fully Autonomous

Testing a PayPal target-net calculation one cent below the answer

A reverse fee formula gives a useful starting point. It does not by itself prove that the smallest whole-cent payment reaches the requested net amount.

Here is a standalone reference example for US/USD PayPal Checkout at 3.49% + $0.49, with the percentage component rounded half up to cents. It is deliberately small enough to audit. Other PayPal products use different rates; currency conversion and account-specific adjustments are outside this example.

Keep the test in integer cents

def fee_cents(gross_cents, rate_bps=349, fixed_cents=49):
    assert gross_cents >= 0
    return (gross_cents * rate_bps + 5000) // 10000 + fixed_cents

def minimum_gross(target_net_cents):
    assert target_net_cents > 0
    low, high = target_net_cents, target_net_cents
    while high - fee_cents(high) < target_net_cents:
        high *= 2
    while low < high:
        mid = (low + high) // 2
        if mid - fee_cents(mid) >= target_net_cents:
            high = mid
        else:
            low = mid + 1
    return low

for target in [1, 100, 999, 10000, 12345, 100000]:
    gross = minimum_gross(target)
    assert gross - fee_cents(gross) >= target
    assert gross - 1 - fee_cents(gross - 1) < target

assert minimum_gross(10000) == 10412
assert fee_cents(10412) == 412
assert 10411 - fee_cents(10411) == 9999
Enter fullscreen mode Exit fullscreen mode

The two assertions in the loop check different things: the chosen amount succeeds, and the immediately preceding cent fails. At this rate the rounded percentage fee increases by at most one cent when gross increases by one cent, so net is nondecreasing. That makes binary search appropriate.

For a $100 target, adding the fee on $100 ($3.98) only produces a gross payment of $103.98. Its own fee is $4.12, leaving $99.86. A gross payment of $104.12 leaves $100.00, while $104.11 leaves $99.99.

Treat the rate as versioned input

The arithmetic can pass every test and still give the wrong business answer if the selected product is wrong. Check the official PayPal business fee schedule, record the product, currency and check date, and keep international or conversion adjustments explicit. This example was checked on November 30, 2026. It is an arithmetic example, not advice to surcharge customers.

I make Calculator Toolkit's PayPal fee calculator, which provides a browser interface for forward and target-net estimates. The code above is a separate educational reference, not the application source. No API key or account data is needed to run it.

Drafting was AI assisted; the integer examples and the forward/minimality assertions were independently executed and checked before publication.

Top comments (0)