PayPal Recurring Payment IPN in PHP

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.


         Live Demo

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:

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>)
&lt;p style=&quot;text-align: justify;&quot;&gt;

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:

 '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
<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.

About sanjeev

Dreamer, Blogger and Thinker. I love to help people in solving their problems. You can also join me HERE

Related posts:

8 Responses so far.

  1. What all would I need to change to use the same price for the first payment and the recurring payment ? For example – I want to charge 4.99 and then recurring every month after that the same – the 4.99. I only need to use one single price instead of the two pricing options for initial payment and second payment.

Leave a Reply

Your email address will not be published. Required fields are marked *