O'Reilly logo

eBay Hacks, 2nd Edition by David A. Karp

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #101. Process PayPal Payments Automatically

Use Instant Payment Notification to fulfill orders without human intervention.

When a bidder pays for an auction with PayPal or sends a PayPal payment from your online store [Hack #100] , PayPal notifies you with a single email. Since the last thing any busy seller wants to do is deal with a bunch of emails, PayPal offers the free Instant Payment Notification (IPN) feature.

Setting Up IPN

The premise is pretty simple: as soon as a payment is received, PayPal contacts your server and submits all the details of the transaction to your script. Your script then processes and stores the data in whatever way you see fit.

To start using IPN, all you need to do is enable the feature and specify the URL of your script. Log into PayPal and go to My Account Profile Instant Payment Notification Preferences. Click Edit to change the current settings.

The Code

The following Perl script* does everything required to accept IPN notifications; all you need to do is modify the $workdir variable to reflect a valid path on your server:

 #!/usr/bin/perl
 $workdir = "/usr/local/home";
read (STDIN, $query, $ENV{'CONTENT_LENGTH'}); 
$query .= '&cmd=_notify-validate'; use LWP::UserAgent; $ua = new LWP::UserAgent; $req = new HTTP::Request 'POST','http://www.paypal.com/cgi-bin/webscr'; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required