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.

 paypal-ipn-detail

         Live Demo

[sociallocker] DOWNLOAD CODE[/sociallocker]

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 `infotuts_transection_tbl` (
 `TID` int(11) NOT NULL AUTO_INCREMENT,
 `item_name` varchar(255) NOT NULL,
 `payer_email` varchar(150) NOT NULL,
 `first_name` varchar(150) NOT NULL,
 `last_name` varchar(150) NOT NULL,
 `amount` float NOT NULL,
 `currency` varchar(50) NOT NULL,
 `country` varchar(50) NOT NULL,
 `txn_id` varchar(100) NOT NULL,
 `txn_type` varchar(100) NOT NULL,
 `payment_status` varchar(100) NOT NULL,
 `payment_metod` varchar(100) NOT NULL,
 `create_date` datetime NOT NULL,
 `payment_date` datetime NOT NULL,
 PRIMARY KEY (`TID`)
)
<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'=>'sanjeev00733-facilitator@gmail.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.

8 thoughts on “PayPal Recurring Payment IPN in PHP”

  1. Pingback: PayPal Recurring Payment IPN in PHP | SOFTWARELIST

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

Comments are closed.