Return to Snippet

Revision: 36160
at December 4, 2013 02:20 by iridium


Updated Code
function paypalFees($sub_total, $round_fee) {
	
	// Set Fee Rate Variables
	$fee_percent = '3.4'; // Paypal's percentage rate per transaction (3.4% in UK)
	$fee_cash    = '0.20'; // Paypal's set cash amount per transaction (�£0.20 in UK)
	
	// Calculate Fees
	$paypal_fee = ((($sub_total / 100) * $fee_percent) + $fee_cash);
	
	if ($round_fee == true) {
		$paypal_fee = ceil($paypal_fee);
	}
	
	// Calculate Grand Total
	$grand_total = ($sub_total + $paypal_fee);
	
	// Tidy Up Numbers
	$sub_total   = number_format($sub_total, 2, '.', ',');
	$paypal_fee  = number_format($paypal_fee, 2, '.', ',');
	$grand_total = number_format($grand_total, 2, '.', ',');
	
	// Return Array
	return array('grand_total'=>$grand_total, 'paypal_fee'=>$paypal_fee, 'sub_total'=>$sub_total);
	
}

Revision: 36159
at December 4, 2013 02:19 by iridium


Updated Code
function paypalFees($sub_total, $round_fee) {
	
	// Set Fee Rate Variables
	$fee_percent = '3.4'; // Paypal's percentage rate per transaction (3.4% in UK)
	$fee_cash    = '0.20'; // Paypal's set cash amount per transaction (£0.20 in UK)
	
	// Calculate Fees
	$paypal_fee = ((($sub_total / 100) * $fee_percent) + $fee_cash);
	
	if ($round_fee == true) {
		$paypal_fee = ceil($paypal_fee);
	}
	
	// Calculate Grand Total
	$grand_total = ($sub_total + $paypal_fee);
	
	// Tidy Up Numbers
	$sub_total   = number_format($sub_total, 2, '.', ',');
	$paypal_fee  = number_format($paypal_fee, 2, '.', ',');
	$grand_total = number_format($grand_total, 2, '.', ',');
	
	// Return Array
	return array('grand_total'=>$grand_total, 'paypal_fee'=>$paypal_fee, 'sub_total'=>$sub_total);
	
}

Revision: 36158
at November 18, 2010 23:16 by iridium


Updated Code
function paypalFees($sub_total, $round_fee) {
	
	// Set Fee Rate Variables
	$fee_percent = '3.4'; // Paypal's percentage rate per transaction (3.4% in UK)
	$fee_cash    = '0.20'; // Paypal's set cash amount per transaction (£0.20 in UK)
	
	// Calculate Fees
	$paypal_fee = ((($sub_total / 100) * $fee_percent) + $fee_cash);
	
	if ($round_fee == true) {
		$paypal_fee = ceil($paypal_fee);
	}
	
	// Calculate Grand Total
	$grand_total = ($sub_total + $paypal_fee);
	
	// Tidy Up Numbers
	$sub_total   = number_format($sub_total, 2, '.', ',');
	$paypal_fee  = number_format($paypal_fee, 2, '.', ',');
	$grand_total = number_format($grand_total, 2, '.', ',');
	
	// Return Array
	return array('grand_total'=>$grand_total, 'paypal_fee'=>$paypal_fee, 'sub_total'=>$sub_total);
	
}

Revision: 36157
at November 18, 2010 23:16 by iridium


Updated Code
function paypalFees($sub_total, $round_fee) {
	
	// Set Fee Rate Variables
	$fee_percent = '3.4'; // Paypal's percentage rate per transaction (3.4% in UK)
	$fee_cash    = '0.20'; // Paypal's set cash amount per transaction (£0.20 in UK)
	
	// Calculate Fees
	$paypal_fee = ((($sub_total / 100) * $fee_percent) + $fee_cash);
	
	if ($round_fee == true) {
		$paypal_fee = ceil($paypal_fee);
	}
	
	// Calculate Grand Total
	$grand_total = ($sub_total + $paypal_fee);
	
	// Tidy Up Numbers
	$sub_total   = number_format($sub_total, 2, '.', ',');
	$paypal_fee  = number_format($paypal_fee, 2, '.', ',');
	$grand_total = number_format($grand_total, 2, '.', ',');
	
	// Return Array
	return array('grand_total'=>$grand_total, 'paypal_fee'=>$paypal_fee, 'sub_total'=>$sub_total);
	
}

Revision: 36156
at November 18, 2010 23:15 by iridium


Updated Code
function paypalFees($sub_total, $round_fee) {
	
	// Set Fee Rate Variables
	$fee_percent = '3.4'; // Paypal's percentage rate per transaction (3.4% in UK)
	$fee_cash    = '0.20'; // Paypal's set cash amount per transaction (£0.20 in UK)
	
	// Calculate Fees
	$paypal_fee = ((($sub_total / 100) * $fee_percent) + $fee_cash);
	
	if ($round_fee == true) {
		$paypal_fee = ceil($paypal_fee);
	}
	
	// Calculate Grand Total
	$grand_total = ($sub_total + $paypal_fee);
	
	// Tidy Up Numbers
	$sub_total   = number_format($sub_total, 2, '.', ',');
	$paypal_fee  = number_format($paypal_fee, 2, '.', ',');
	$grand_total = number_format($grand_total, 2, '.', ',');
	
	// Return Array
	return array('grand_total'=>$grand_total, 'paypal_fee'=>$paypal_fee, 'sub_total'=>$sub_total);
	
}

Revision: 36155
at November 18, 2010 23:14 by iridium


Updated Code
function paypalFees($sub_total, $round_fee) {
	
	// Set Fee Rate Variables
	$fee_percent = '3.4'; // Paypal's percentage rate per transaction (3.4% in UK)
	$fee_cash    = '0.20'; // Paypal's set cash amount per transaction (���£0.20 in UK)
	
	// Calculate Fees
	$paypal_fee = ((($sub_total / 100) * $fee_percent) + $fee_cash);
	
	if ($round_fee == true) {
		$paypal_fee = ceil($paypal_fee);
	}
	
	// Calculate Grand Total
	$grand_total = ($sub_total + $paypal_fee);
	
	// Tidy Up Numbers
	$sub_total   = number_format($sub_total, 2, '.', ',');
	$paypal_fee  = number_format($paypal_fee, 2, '.', ',');
	$grand_total = number_format($grand_total, 2, '.', ',');
	
	// Return Array
	return array('grand_total'=>$grand_total, 'paypal_fee'=>$paypal_fee, 'sub_total'=>$sub_total);
	
}

Revision: 36154
at November 18, 2010 23:14 by iridium


Updated Code
function paypalFees($sub_total, $round_fee) {
	
	// Set Fee Rate Variables
	$fee_percent = '3.4'; // Paypal's percentage rate per transaction (3.4% in UK)
	$fee_cash    = '0.20'; // Paypal's set cash amount per transaction (�£0.20 in UK)
	
	// Calculate Fees
	$paypal_fee = ((($sub_total / 100) * $fee_percent) + $fee_cash);
	
	if ($round_fee == true) {
		$paypal_fee = ceil($paypal_fee);
	}
	
	// Calculate Grand Total
	$grand_total = ($sub_total + $paypal_fee);
	
	// Tidy Up Numbers
	$sub_total   = number_format($sub_total, 2, '.', ',');
	$paypal_fee  = number_format($paypal_fee, 2, '.', ',');
	$grand_total = number_format($grand_total, 2, '.', ',');
	
	// Return Array
	return array('grand_total'=>$grand_total, 'paypal_fee'=>$paypal_fee, 'sub_total'=>$sub_total);
	
}

Revision: 36153
at November 18, 2010 23:13 by iridium


Updated Code
function paypalFees($sub_total, $round_fee) {

	/* Usage Example 

	
	// Set Fee Rate Variables
	$fee_percent = '3.4'; // Paypal's percentage rate per transaction (3.4% in UK)
	$fee_cash    = '0.20'; // Paypal's set cash amount per transaction (£0.20 in UK)
	
	// Calculate Fees
	$paypal_fee = ((($sub_total / 100) * $fee_percent) + $fee_cash);
	
	if ($round_fee == true) {
		$paypal_fee = ceil($paypal_fee);
	}
	
	// Calculate Grand Total
	$grand_total = ($sub_total + $paypal_fee);
	
	// Tidy Up Numbers
	$sub_total   = number_format($sub_total, 2, '.', ',');
	$paypal_fee  = number_format($paypal_fee, 2, '.', ',');
	$grand_total = number_format($grand_total, 2, '.', ',');
	
	// Return Array
	return array('grand_total'=>$grand_total, 'paypal_fee'=>$paypal_fee, 'sub_total'=>$sub_total);
	
}

Revision: 36152
at November 18, 2010 23:11 by iridium


Initial Code
function paypalFees($sub_total, $round_fee) {
	
	// Set Fee Rate Variables
	$fee_percent = '3.4'; // Paypal's percentage rate per transaction (3.4% in UK)
	$fee_cash    = '0.20'; // Paypal's set cash amount per transaction (£0.20 in UK)
	
	// Calculate Fees
	$paypal_fee = ((($sub_total / 100) * $fee_percent) + $fee_cash);
	
	if ($round_fee == true) {
		$paypal_fee = ceil($paypal_fee);
	}
	
	// Calculate Grand Total
	$grand_total = ($sub_total + $paypal_fee);
	
	// Tidy Up Numbers
	$sub_total   = number_format($sub_total, 2, '.', ',');
	$paypal_fee  = number_format($paypal_fee, 2, '.', ',');
	$grand_total = number_format($grand_total, 2, '.', ',');
	
	// Return Array
	return array('grand_total'=>$grand_total, 'paypal_fee'=>$paypal_fee, 'sub_total'=>$sub_total);
	
}

Initial URL


Initial Description
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 sub_total
*   The Paypal Fee applied as paypal_fee
*   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.

Initial Title
Calculate Paypal Fee

Initial Tags


Initial Language
PHP