Generating Payment Plans using Paypal and PHP
Paypal subscriptions are rather handy allowing you to take regular amounts of money from a user in exchange for goods/services one of the lesser known features of subscriptions is the ability to determine how many “cycles” paypal should take the subscription through before stopping. This allows you to create subscriptions which cost xx for xx months that sounds almost like a payment plan?
How is a payment plan different from a subscription?
I’m sure there is some legal definition of a payment plan but in this context, I’m talking about a fixed price which is then split into equal payments over a given period. For example a payment plan might look something like this: Total value of £200 with 4 payments of £50 normally a payment plan is for a good or service or something that comes to an end.
Payment Plan – Legal Note
IAMNAL – But there is a huge flaw with using Paypal as a means to create a cheap payment plan for a physical good. The user has the right to cancel a Paypal subscription at any time nothing you can do about it, once cancelled it would be up to you to chase them for outstanding money. Also be aware in the UK you do need a consumer credit license to offer credit now I do not know if the fact the user can cancel the payment means you would be exempt but I suspect it would be hard to stand up in court. Use this idea cautiously.
Creating a Payment Plan
Ok so let’s assume we have an item priced at £250 we want to offer a payment plan of £20 month payments. Because we are taking micropayments there is a greater cost involved so it makes sense to charge an additional percentage (administration fee) remember this can’t be paypal fee totals as that would be against Paypals TOS.
The best way to see what’s happening is to look at some code, this function is receiving 3 variables, the original cost, the cost of an installment and the percentage we are going to charge.
function generate_paypal_button($cost,$ins,$per){
/* add our percentage if you want to display percentage info $per % $perrcentage_cost actual value
$total_cost is total with % */
$percentage_cost = $cost * $per /100;
$total_cost = $cost + $percentage_cost;
//determine number of installments ($total_ins)and whats left ($last)
$total_ins = 1; //this will be going up
$last = $total_cost;
while($last > $ins)
{
$last -= $ins;
$total_ins++;
}
//Last is the amount outstanding but paypal wont let us add at the end, so we have to add it to the start
$total = $total_ins - 1; //deduct an installment for our first one
$first = $last + $ins; //our additional installment the normal installment price + $last
generate_paypal_button_code($first, $total, $ins);
//debug uncomment for the cool total
/* echo 'Cost: $'. $cost .' Total Cost (with '.$per.'%): $'. $total_cost .' with '. $total .' instalments
of $'. $ins .' and fist installment of $'.$first; */
}
Notice we are creating a first payment which is a standard installment charge plus all the remaining money that would be owed. Because of the way Paypal works we cannot add the remainder at the end of the installments so we need to do it at the beginning. Since we have combined the first installment we must remember to remove 1 installment from the total that we are going to run for (as it will have happened prior to reaching this total)
The generate_paypal_button_code function is simply creating the button
function generate_paypal_button_code($first, $total_ins, $ins){
//In the real world this would be coming from a db, and indeed this would probably be a class or something more generic
$paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
$paypal_email = 'joe@example.com';
$paypal_item_name = 'Test Installment Plan';
$paypal_notify_url = 'http://www.example.com';
$paypal_return = 'http://www.example.com';
echo '
<form action="'. $paypal_url .'" method="post" name="paypal_form" id="paypal_form">
<input type="hidden" name="cmd" value="_xclick-subscriptions" />
<input type="hidden" name="business" value="'.$paypal_email .'" />
<input type="hidden" name="item_name" value="'. $paypal_item_name .'" />
<input type="hidden" name="no_shipping" value="1" />
<input type="hidden" name="no_note" value="1" />
<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="lc" value="US" />
<input type="hidden" name="a1" value="'.$first .'" />
<input type="hidden" name="p1" value="1" />
<input type="hidden" name="t1" value="M" />
<input type="hidden" name="a3" value="'. $ins .'" />
<input type="hidden" name="p3" value="1" />
<input type="hidden" name="t3" value="M" />
<input type="hidden" name="recur_times" value="1">
<input type="hidden" name="src" value="1" />
<input type="hidden" name="sra" value="1" />
<input type="hidden" name="srt" value="'. $total_ins .'" />
<input type="hidden" name="notify_url" value="'. $paypal_notify_url .'" />
<input type="hidden" name="return" value="'. $paypal_return .'" />
<input type="hidden" name="callback_script" value="<script></script>" />
<input type="hidden" name="rm" value="2" />
<input type="submit" name="submit" id="submit">
</form>';
}
The bits of importance to us are the a1,p1,t1 inputs these generate a “trial” which we are using to generate our first payment of a single installment plus the remaining money.
<input type="hidden" name="srt" value="'. $total_ins .'" />
The SRT input allows us to declare the number of cycles we want to pass through, this is the number of regular payments to be made, when you reach the SRT limit Paypal stops taking the money.
If your stuck for how to handle paypal payments on a site then you might find this tutorial from Olaf useful: Paypal IPN Integration
Uses for Payment Plans
Well the obvious use perhaps is a drip fed membership site (Your Members perhaps), where people have limited but growing access to the site while they are still paying off their membership when they reach the end of their payment plan they gain complete access and any physical product (dvd perhaps) is shipped to them.
How would you use payment plans?


