Process PayPal Payments Automatically

Use Instant Payment Notification to fulfill orders without human intervention.

When a bidder pays for an auction with PayPal or completes an order from your online store (see [Hack #79]), 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 Script

The following Perl script[15] 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.

Tip

This script requires the LWP::UserAgent Perl module by Alan E. Derhaag, available at http://search.cpan.org/perldoc?LWP::UserAgent, necessary to facilitate communication with the PayPal server. See [Hack #17] for installation instructions.

#!/usr/bin/perl
$workdir = "/usr/local/home";

read (STDIN, $query, $ENV{'CONTENT_LENGTH'});     [1]
$query .= '&cmd=_notify-validate';     [2] use LWP::UserAgent; $ua = new LWP::UserAgent; $req = ...

Get eBay Hacks 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.