Skip to content

Instantly share code, notes, and snippets.

@taeber
Created November 14, 2012 02:50
Show Gist options
  • Save taeber/4069988 to your computer and use it in GitHub Desktop.
Save taeber/4069988 to your computer and use it in GitHub Desktop.
PHP implementation of PayPal fee calculator
<?php
/// Start of a PayPal fee calculator that I'll probably never finish. :-[
/*
Given:
total(cost, fee) := cost + fee(cost)
fee(cost) := F + P*cost
F := $0.30
P := 2.9%
Find:
Total amount to charge so that the customer pays all fees.
paid - fees >= cost
fees := fee(paid) = F + P*paid
paid - (F + P - paid)
paid*(1-P) - F >= cost
paid >= (cost + F)/(1-P)
*/
/**
* Calculates the total amount to charge based a customer based on a given
* sub-total amount.
*/
function total($subtotal, $fees)
{
// total := subtotal + fees
return bcadd($subtotal, $fees($subtotal));
}
function paypal_fees($subtotal, $flat_fee = "0.30", $variable_rate = "0.029")
{
// fees := F + P * subtotal
// subtotal + F
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment