## Posted By

iridium on 11/18/10

## Who likes this?

1 person have marked this snippet as a favorite

# Calculate Paypal Fee

/ Published in: PHP

This calculates the Paypal transaction fee for a supplied amount, and adds it to the final amount. We used this to charge the Paypal fee back to the user as a booking fee on ticket sales for an event.

The function will spit out an array containing: * The Sub Total amount supplied as subtotal * The Paypal Fee applied as paypalfee * The Grand Total, which is the two above added together, as grand_total

You have the option of rounding the fee up to the nearest whole number, or charging the user the exact transaction fee, just supply a second parameter in the function call as true.

The Paypal rates are set as variables inside the function itself, it's up to you to check what they are for your country and set them accordingly.

1. function paypalFees(\$sub_total, \$round_fee) {
2.
3. // Set Fee Rate Variables
4. \$fee_percent = '3.4'; // Paypal's percentage rate per transaction (3.4% in UK)
5. \$fee_cash = '0.20'; // Paypal's set cash amount per transaction (Ãƒï¿½Ã‚Â£0.20 in UK)
6.
7. // Calculate Fees
8. \$paypal_fee = (((\$sub_total / 100) * \$fee_percent) + \$fee_cash);
9.
10. if (\$round_fee == true) {
11. \$paypal_fee = ceil(\$paypal_fee);
12. }
13.
14. // Calculate Grand Total
15. \$grand_total = (\$sub_total + \$paypal_fee);
16.
17. // Tidy Up Numbers
18. \$sub_total = number_format(\$sub_total, 2, '.', ',');
19. \$paypal_fee = number_format(\$paypal_fee, 2, '.', ',');
20. \$grand_total = number_format(\$grand_total, 2, '.', ',');
21.
22. // Return Array
23. return array('grand_total'=>\$grand_total, 'paypal_fee'=>\$paypal_fee, 'sub_total'=>\$sub_total);
24.
25. }