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'; ...

Get eBay Hacks, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.