Calculate payment fee from Stripe / PayPal - strange output in function

Viewed 1611

I have the below function that calculates the payment fee that will go to the payment provider (say Stripe or PayPal).

/**
 * Calculate payment provider fee. As described in this article: https://support.stripe.com/questions/can-i-charge-my-stripe-fees-to-my-customers
 *
 * 
 * @param  integer $amount  The amount you wish to charge
 * @param  float  $fixed   The fixed fee on the gateway
 * @param  float  $percent The percent fee on the gateway (0.029 = 2.9%)
 * @return integer          The fee that the gateway will charge
 */

function calcPaymentFee($amount, $fixed = 0.30, $percent = 0.029) {
    $a = $amount+$fixed;
    return round($a / (1 - $percent) - $amount, 3);
}

I has been developed based on this article from Stripe:

However, as you increase the total amount you ask Stripe to charge, you’re also increasing the Stripe fee, because Stripe’s pricing is a flat fee plus a percentage of the total charge amount. So you’ll need to figure out what gross amount you need to charge such that, after Stripe deducts its fee, you receive some predictable net amount.

This turns out to be a math problem that isn’t as simple as it sounds, but to save you some calculation:

enter image description here

I believe my function should be working when working with lower percentages (i.e. 2.9%, 5%, etc) - I tested output according to this tool. But as the percentage increases, the output seems weird, and I think the function is somehow messed up, or I don't properly understand how the fee should be charged. Either case could be imaginable.

Output:

calcPaymentFee(100, 0, 0.029) // 2.9%: 2.987 (seems correct, this tool gives same output: http://thefeecalculator.com/stripe/) 
calcPaymentFee(100, 0, 0.1) // 10%: 11.111 (seems correct)
calcPaymentFee(100, 0, 0.5) // 50%: 100.0 (?!? - makes no sense)

I don't really understand why the payment fee would be 100. I would imagine it to be something like 50%, if I pass in "0.5" as the third parameter. Maybe I misunderstand something, but it seems odd.

What I want to achieve

I wish to calculate the fee that Stripe or PayPal will charge, and then put this fee on the customer.

So if I sell something for 100$, and the Stripe fee is 2.9%, I will charge the customer 102.9$.

What I currently don't understand, is if I want sell something for 100$, where I have set the fee to be 50%, I am told to charge 200$.

2 Answers
Related