Easily Integrate Paypal IPN in PHP

We have already published a post on integrating PayPal with PHP. In this post we will see how to use PayPal IPN in PHP. PayPal IPN (instant payment notification) allows you to integrate PayPal payments with web application backend. As the name suggests it sends instant notification of the payment processed so that you can use the data for further use. An example would be when you want to allow users to download something immediately after successful payment. When a user sends money to your PayPal, you will get instant response/notification from PayPal and then you can save it in your website’s backend (database) for further use. Another use would be to set up a recurring payment system so that IPN script updates the transaction table automatically for you.


Let’s break this into simple steps to make it easier:

  • User click on buy button and makes payment.
  • PayPal processes that payment and sends back you response with transaction detail to a PHP page in your website (IPN script).
  • User will see payment successful page.
Live Demo


Now, in order to make IPN payments work you need to enable IPN in the seller’s PayPal account settings. Login to your PayPal account and go to profile option and enable it. Also you need to enter IPN script URL in settings:

paypal IPN 1

In our demo we have used sandbox account you just need to replace the email id’s. First create a transaction table in your database to store the transaction details sent by PayPal:

paypal ipn2

below is the SQL query to create the transaction table.

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>payer_id</code> varchar(50) NOT NULL,
 <code>payment_status</code> varchar(100) NOT NULL,
 <code>payment_type</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 we have two main files index.php which will show a “Pay Now” button to make purchase and inform paypal about the action, then another file we have is ipn.php which would receive response from paypal and store the transaction detail and status in database table.

Below is code for ipn.php:

class PayPal_IPN{
function infotuts_ipn($im_debut_ipn) {

define('SSL_P_URL', 'https://www.paypal.com/cgi-bin/webscr');
 define('SSL_SAND_URL', 'https://www.sandbox.paypal.com/cgi-bin/webscr');
 $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
 if (!preg_match('/paypal\.com$/', $hostname)) {
 $ipn_status = 'Validation post isn\'t from PayPal';
 if ($im_debut_ipn == true) {
 // mail test

return false;

// parse the paypal URL
 $paypal_url = ($_REQUEST['test_ipn'] == 1) ? SSL_SAND_URL : SSL_P_URL;
 $url_parsed = parse_url($paypal_url);

 $post_string = '';
 foreach ($_REQUEST as $field => $value) {
 $post_string .= $field . '=' . urlencode(stripslashes($value)) . '&';
 $post_string.="cmd=_notify-validate"; // append ipn command
 // get the correct paypal url to post request to
 $paypal_mode_status = $im_debut_ipn; //get_option('im_sabdbox_mode');
 if ($paypal_mode_status == true)
 $fp = fsockopen('ssl://www.sandbox.paypal.com', "443", $err_num, $err_str, 60);
 $fp = fsockopen('ssl://www.paypal.com', "443", $err_num, $err_str, 60);

$ipn_response = '';

if (!$fp) {
// could not open the connection. If loggin is on, the error message
// will be in the log.
 $ipn_status = "fsockopen error no. $err_num: $err_str";
 if ($im_debut_ipn == true) {
 echo 'fsockopen fail';
 return false;
 } else {
// Post the data back to paypal
 fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n");
 fputs($fp, "Host: $url_parsed[host]\r\n");
 fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
 fputs($fp, "Content-length: " . strlen($post_string) . "\r\n");
 fputs($fp, "Connection: close\r\n\r\n");
 fputs($fp, $post_string . "\r\n\r\n");

// loop through the response from the server and append to variable
 while (!feof($fp)) {
 $ipn_response .= fgets($fp, 1024);
 fclose($fp); // close connection

// Invalid IPN transaction. Check the $ipn_status and log for details.
 if (!preg_match("/VERIFIED/s", $ipn_response)) {
 $ipn_status = 'IPN Validation Failed';

if ($im_debut_ipn == true) {
 echo 'Validation fail';
 return false;
 } else {
 $ipn_status = "IPN VERIFIED";
 if ($im_debut_ipn == true) {
 echo 'SUCCESS';


return true;
 function ipn_response($request){
 mail("sanjeev00733@gmail.com","My subject",print_r($request,true));
 if ($this->infotuts_ipn($im_debut_ipn)) {

 // if paypal sends a response code back let's handle it
 if ($im_debut_ipn == true) {
 $sub = 'PayPal IPN Debug Email Main';
 $msg = print_r($request, true);
 $aname = 'infotuts';
 //mail send

// process the membership since paypal gave us a valid +
function issetCheck($post,$key){
return $return;
 function insert_data($request){

$create_date=date('Y-m-d H:i:s');
$payment_date=date('Y-m-d H:i:s');

mysqli_query($con,"INSERT INTO infotuts_transection_tbl (item_name,payer_email,first_name,last_name,amount,currency,country,txn_id,txn_type,payer_id,payment_status,payment_type,create_date,payment_date)
VALUES ('$item_name','$payer_email','$first_name','$last_name','$amount','$currency','$country','$txn_id','$txn_type','$payer_id','$payment_status','$payment_type','$create_date','$payment_date')");

 $obj = New PayPal_IPN();

<p style="text-align: justify;">

You can download the source code and host it on your server to see it working. Its ready to use code just add your Paypal merchant email ID and set product prize etc, then you are all good to go and start receiving payment. In next tutorial we will be adding feature of recurring payment. And guys we do have a good news, we’ll be launching premium scripts very soon. Scripts would be available for a low cost and all future updates for those scripts would be free to buyers. Let us know if you have a good idea and want us to develop a script/code for it. Share this post on how to Integrate Paypal IPN in PHP with your friend 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:

10 Responses so far.

  1. Same here I am afraid – no entry in database and no message in ipn.php. If I add an else condition to ” if ($this->infotuts_ipn($im_debut_ipn)) { ” then it prints out the error message. Any idea how we can debug this? Best Bis

  2. I think the reason it is not working is that I am using localhost rather than a live server. Everything else works though and even the server response is sent to my thanks_page but the notify_url does not work.

  3. Glad to say it is working now for me – thanks again for this post! Just a quick note to say that the download contains a typo (“payment_metod” should be “payment_type”) and a missing table entry (“payer_id”) in the “dbconnect.php” file but as long as you copy and paste from the code above it should be fine.

    1. Hi. IPN file is called as i have just do normal insert entry is my database. But some how response from Paypal not store in database. I have changed that (“payment_metod” to “payment_type”) but it still not work for me. Can you please give me that ipn.php file.


  4. It took me some time and effort to get it working; however, it is working and doing what I need now. Thanks//

Comments are closed.