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.
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.
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:
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 ;
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.
$value = urlencode(stripslashes($value));
$req .= “&$key=$value”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘https://www’.$paypalmode.’.paypal.com/cgi-bin/webscr’);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 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.’.sandbox.paypal.com’));
$res = curl_exec($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);
die(‘Could not connect: ‘ . mysql_error());
// insert in our IPN record table
$query = “INSERT INTO ibn_table
(‘$transaction_id’,’$payerid’,’$firstname $lastname’,’$payeremail’,’$mdate’, ‘$paymentstatus’,’$otherstuff’)”;
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!