WebMW - 11 months ago 52

Javascript Question

Sounds like an easy question, I know. I am literally stumped.

Here are two websites that have made the simple calculator:

http://thefeecalculator.com

http://ppcalc.com

I'm needing to calculate these PayPal's fees in my PHP application.

Here's what I got, say we wanted to be paid $30 "even" for membership fees. This is the function I'm using to calculate what we should charge to make up for it using PayPal's calculations (also described on both of the sites above): 2.9% of the transaction + $0.30.

`function memfees()`

{

$calc = round((30 * .029) + 30 + .30,2);

return $calc;

}

Test it here: http://matt.mw/junk/php/memfees.php

As you can see, it says 31.17.

Both of the other websites say it should be 31.20.

If I try $130.00, mine calculates to $134.07 and both of theirs calculate to $134.19.

What am I doing wrong?

Why am I using a Round function? Percentages cause some decimals to go past the hundredths place, and PayPal generates an error if there is more then 2 digits after the decimal. I thought people normally round money, (e.g. $31.6342 will be $31.63) but if that is the case, what am I doing wrong in my function? The amount that is off is further if there are large payments. This leads me to believe that something is wrong with the percentage portion.

Could you offer some help on this?

Thank you for your time.

Answer

Your function does seem strange. To break it down, PayPal is charging a fixed rate of $.30, and adding a transaction percentage fee of 2.9%.

The formula for this is to add the $.30 and then divide by the percentage difference (100% - 2.9%), which will give you the amount prior to the actual reduction by PayPal.

```
function memfees($amount)
{
$amount += .30;
return $amount / (1 - .029);
}
```

You can round and float that as you wish.

Source (Stackoverflow)