There are several PHP scripts and classes to process PayPal payments using their native IPN (Internet payment notification) feature. Because the whole process is based on the data you need to send via a web form to the PayPal payment processor these script look very similar.
The payment / notification process is shown via the following graphic:

Inside the form there are several required values to process a payment. PayPal gives the advice to post them all to get everything working. The following variables get some special attention:
business = your PayPal email address
cmd = single payments or subscription service (_xclick or _xclick-subscriptions)
return = the URL where the buyer get back after the payment is processed
cancel_return = the URL where the buyer get back if he has cancelled the payment
notify_url = the location where your IPN script is located
rm = how you need the data submitted from PayPal to your IPN script (1=get, 2=post)
currency_code = the currency you accept for your payment
lc = the country version of PayPal where your buyer is send to
There are much more variables, but we think that the other variables (product, order and shipment information) speak for themselves. Find a complete form provided with the example files.
To run some IPN enabled payment process we need a small script which will double check if the data which is send to the IPN script is valid according the data which is stored on the PayPal server. This feature is very important if your e-commerce accepts automatic payments.
The following code is able to check if the payment is valid against the PayPal server. Use this test to decide if the payment is valid or not.
<?php
$url = ‘https://www.paypal.com/cgi-bin/webscr’;
$postdata = ”;
foreach($_POST as $i => $v) {
$postdata .= $i.‘=’.urlencode($v).‘&’;
}
$postdata .= ‘cmd=_notify-validate’;
$web = parse_url($url);
if ($web[’scheme’] == ‘https’) {
$web[‘port’] = 443;
$ssl = ’ssl://’;
} else {
$web[‘port’] = 80;
$ssl = ”;
}
$fp = @fsockopen($ssl.$web[‘host’], $web[‘port’], $errnum, $errstr, 30);
if (!
$fp) {
echo $errnum.‘: ’.$errstr;
} else {
fputs($fp, “POST ”.$web[‘path’].“ HTTP/1.1rn”);
fputs($fp, “Host: ”.$web[‘host’].“rn”);
fputs($fp, “Content-type: application/x-www-form-urlencodedrn”);
fputs($fp, “Content-length: ”.strlen($postdata).“rn”);
fputs($fp, “Connection: closernrn”);
fputs($fp, $postdata . “rnrn”);
while(!
feof($fp)) {
$info[] = @fgets($fp, 1024);
}
fclose($fp);
$info = implode(‘,’, $info);
if (eregi(‘VERIFIED’, $info)) {
// yes valid, f.e. change payment status
} else {
// invalid, log error or something
}
}
?>
As mentioned before there are some complete solutions available on the internet. If your e-copmmerce site doesn’t have a complex product catalog you should use some static code from the PayPal website. For this guide we checked the PHP toolkit provided by PayPal.
Code condition
The first thing I noticed the code is not very clean and is using a coding style which is based on older PHP versions (f.e. for systems using register globals = On)
Implementation
After some code clean-up it was possible to use the included file together with my shopping cart script. Static variables are defined in one central configuration file and dynamic files are posted via the form in your web application.
IPN features
This script is written to handle the IPN validation process with different methods: cURL, fsockopen, and libcURL. I tried only the fsockopen option because this method looks good to me and should work on almost every web platform.
Documentation
There is a “Readme” file with the information about the most important features. A complete guide is not included and the information about subscription payments is missing in all files and documents. If you decide to start with the original files you should check also the comments within the configuration and example files.
Example files
The included files are good enough to jump start your paypal payment application. All files are included for a single buy button and also for processing the payment f.e. for the items from a shopping cart. The bad thing is that the bad coding style makes it not easy to integrate the script into you own application if you’re an PHP beginner.
As mentioned before I included my own example files to this PayPal payment guide. If you have questions about this code please post them to our forum, we’re glad to help. Don’t forget the code is provided as it is and we’re not responsible for the functions and/or risks while using this code. Download the example code here.
While it may be easy - which is also debatable - it’s reliability leaves much to be desired. For much of January and February PayPal was having severe service issues: IPN notifications not being delivered, E-check pending statuses not resolved, etc. Just read some of the threads on paypaldeveloper.com. I would advise taking that into consideration.
Hi Andrei,
thanks fo your comment.
Sure there will be always issues and I think every solid application need some safety belt, but this would be an item for the next article
PS. I hate the paypal sandbox!
I’ve been having trouble transferring form data to paypal. This helped.
I am currently working on an E-commerce project and until now, we had not looked at Pay Pal as a viable option for us, however, it looks like it may just work. Along with others, Pay Pal pro integrates with Zen cart!
Took me some time, but i got it running now
Thanks alot !
I’ve been using it for a while now with Vbulletin. A real time saver!
Thanks, it saved many time 4 me
Just a quick question, how do I access the variables send from the form.
For example the custom field has the username in it. I need to update data in a MySQL database where the row matchs the username, would I use
$HTTP_POST_VARS[”custom”]
Thanks in advance
Hi Nathan,
please post your question via the forum:
http://www.finalwebsites.com/forums/
[…] nice guide on how olaf descripes IPN IPN Diagram From the Dzone Dzone […]