Paypal Instant Payment Notification (IPN)

This is continuation of the previous post how to setup Paypal Express Checkout in your website. In my experience, you do not necessarily need to setup Paypal Instant Payment Notification (IPN), but if want to automate tasks and keep track of customer actions back in their PayPal account, you should create and setup an IPN listener script.

How IPN works

Lets say, a customer buys a product from your website and your database captures the transaction, but the fund is pending, only after few days buyer realizes this and authorizes the payment in his PayPal account, how will you update this in your database? will you wait for PayPal to send you email or have your IPN script track this user action instantly? I wouldn't worry if it's just 1 or 2 products, but imagine dealing with hundreds of customers.PayPal can send $_POST variables to your script, containing various information about your customer transaction, your script should collect these values and use them to update or insert database records. Your IPN script works in background and has no direct contact with the customer, you just have to point IPN Url to this script from your Paypal IPN settings.

MySQL table

In this tutorial, we will just store information received from PayPal using IPN listener script, but you can program your own script to do lot more. Let's say we want to create a list of history by inserting received info in the database, we need to create a table similar to below:[cc lang="mysql"] CREATE TABLE IF NOT EXISTS 'ibn_table' ( 'id' int(11) NOT NULL AUTO_INCREMENT, 'itransaction_id' varchar(60) NOT NULL, 'ipayerid' varchar(60) NOT NULL, 'iname' varchar(60) NOT NULL, 'iemail' varchar(60) NOT NULL, 'itransaction_date' datetime NOT NULL, 'ipaymentstatus' varchar(60) NOT NULL, 'ieverything_else' text NOT NULL, PRIMARY KEY ('id') ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; [/cc]

Listener Script

PHP script ipn_paypal.php below stores any incoming message from Paypal in MySql table. Just put this file somewhere in your website and point IPN Url to this script from your Paypal IPN settings.To test this script in Paypal sandbox mode, just create an account in Paypal Developer website, once you are logged in to the site, click 'test tools' on sidebar, and click "Instant Payment Notification (IPN) Simulator" and enter this script URL in "IPN handler URL" field, choose Web Accept from "Transaction type" dropdown.[cc lang="php"] $value) { $value = urlencode(stripslashes($value)); $req .= "&$key=$value"; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://www'.$paypalmode.''); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_POSTFIELDS, $req); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www'.$paypalmode.'')); $res = curl_exec($ch); curl_close($ch);if (strcmp ($res, "VERIFIED") == 0) { $transaction_id = $_POST['txn_id']; $payerid = $_POST['payer_id']; $firstname = $_POST['first_name']; $lastname = $_POST['last_name']; $payeremail = $_POST['payer_email']; $paymentdate = $_POST['payment_date']; $paymentstatus = $_POST['payment_status']; $mdate= date('Y-m-d h:i:s',strtotime($paymentdate)); $otherstuff = json_encode($_POST);$conn = mysql_connect($dbhost,$dbusername,$dbpassword); if (!$conn) { die('Could not connect: ' . mysql_error()); }mysql_select_db($dbname, $conn);// insert in our IPN record table $query = "INSERT INTO ibn_table (itransaction_id,ipayerid,iname,iemail,itransaction_date, ipaymentstatus,ieverything_else) VALUES ('$transaction_id','$payerid','$firstname $lastname','$payeremail','$mdate', '$paymentstatus','$otherstuff')";if(!mysql_query($query)) { //mysql error..! } mysql_close($conn);} } ?> [/cc] That's it! you just wait and let script store any information received in the database. Remember you can extend this script functionality to do lot more. Good luck!
  • Good example but you really need to provide a working example and downloadable scripts to make this of real benefit. There are variables referred to which are not defined and sections of the script missing.
  • Hi the code works in sandbox mode with IPN Simulator. But in real with -> $paypalmode = ”; //Sandbox for testing or empty ”. it does not work.... please help.with Grea Thanks MTSmash
  • Hey im using your sanwebe cart. My problem is the transaction still succeeds even if the buyer has no money. In paypal, the status says it was completed. And sometimes, the transaction is not displayed in the history in paypal transaction. I dont need those token i think, all i need is the product list they bought. So i just put a header in the first if(SUCCESS). Can someone help me. I have a deadline in 2 days, and its killin me.
New question is currently disabled!