We have already published a post on integrating PayPal Payment Gateway in PHP and how to integrate PayPal IPN in PHP. In this post we will see how to use PayPal IPN in PHP to setup recurring payments. Consider a case where you offer service on a subscription basis. An example would be, user pays you every month a fixed amount in order to continuously use the service so you can automate this and user will be subscribed for the recurring payment. Please check our previous tutorial to learn more about how PayPal IPN works as in this post we will add some extra code to add the functionality of recurring payments. Upon every successful recurring payments your transaction table would automatically update.
Let take an example where you offer some SEO service and charge user $10 for first month and then $7 for every two months. We can set these things in our script so that user can automatically subscribe for the service and allow PayPal to charge $7 in every 2 months. User will get notification from PayPal about the recurring payment and transaction table in your DB would also get updated at the same time.
We have the same transaction table in our DB to store transaction info, below is the SQL code for it:
</p> CREATE TABLE IF NOT EXISTS <code>infotuts_transection_tbl</code> ( <code>TID</code> int(11) NOT NULL AUTO_INCREMENT, <code>item_name</code> varchar(255) NOT NULL, <code>payer_email</code> varchar(150) NOT NULL, <code>first_name</code> varchar(150) NOT NULL, <code>last_name</code> varchar(150) NOT NULL, <code>amount</code> float NOT NULL, <code>currency</code> varchar(50) NOT NULL, <code>country</code> varchar(50) NOT NULL, <code>txn_id</code> varchar(100) NOT NULL, <code>txn_type</code> varchar(100) NOT NULL, <code>payment_status</code> varchar(100) NOT NULL, <code>payment_metod</code> varchar(100) NOT NULL, <code>create_date</code> datetime NOT NULL, <code>payment_date</code> datetime NOT NULL, PRIMARY KEY (<code>TID</code>) ) <p style="text-align: justify;">
Now the main change is in index.php file where we have added some code to set the recurring about and first month amount that would be charged from user. You can also define the time/interval after which recurring amount would be paid by the user. Below is the array that contains all the info in variables:
</p> $data=array( 'merchant_email'=>'email@example.com', 'product_name'=>'Demo Product', 'f_amount'=>10, // trail Period Amount 'f_cycle'=>'W', // trail Period M=montrh,Y=year ,D=Days, W='week' 'f_period'=>1, // trail Cycle 's_amount'=>7, // Second` Amount 's_cycle'=>'M', // Second Period M=montrh,Y=year ,D=Days, W='week' 's_period'=>2, // Second Cycle 'currency_code'=>'USD', 'thanks_page'=>"http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'thank.php', 'notify_url'=>"http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'ipn.php', 'cancel_url'=>"http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], 'paypal_mode'=>true, 'currency_symbole'=>'$' ); <p style="text-align: justify;">
Rest code is almost same as our last tutorial on Integrating Easily Integrate Paypal IPN in PHP so you can follow that. Just download the code and run in on your live server. You can use sandbox PayPal account and set s_period variable to 1 and s_cycle to D (Days) so that you can test it well in a days time. Hope this would help you guys. Share this simple and easy tutorial with your friends and followers.