#!/usr/bin/perl
# read post from PayPal system and add 'cmd'
read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query .= '&cmd=_notify-validate';
# post back to PayPal system to validate
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$req = new HTTP::Request 'POST','[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr';[/URL]
# note: if you have SSL encryption Enabled, use <[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr>[/URL] above
$req->content_type('application/x-[URL unfurl="true"]www-form-urlencoded');[/URL]
$req->content($query);
$res = $ua->request($req);
# split posted variables into pairs
@pairs = split(/&/, $query);
$count = 0;
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$variable{$name} = $value;
$count++;
}
# assign posted variables to local variables
# note: additional IPN variables also available -- see IPN documentation
$item_name = $variable{'item_name'};
$receiver_email = $variable{'receiver_email'};
$item_number = $variable{'item_number'};
$invoice = $variable{'invoice'};
$a = $variable{'custom'};
$payment_status = $variable{'payment_status'};
$payment_gross = $variable{'payment_gross'};
$txn_id = $variable{'txn_id'};
$payer_email = $variable{'payer_email'};
$amount = $variable{'amount'};
$on0 = $variable{'option_name1'};
$os0 = $variable{'option_selection1'};
$status = $res->content;
$note = $variable{'memo'};
if ($res->is_error) {
# HTTP error
}
elsif ($res->content eq 'VERIFIED') {
# check the payment_status=Completed
# check that txn_id has not been previously processed
# check that receiver_email is an email address in your PayPal account
# process payment
# print to screen the following if the IPN POST was VERIFIED
print "content-type: text/plain\n\nOK\n";
print "<html><head><title>IPN Screendump</title></head>\n";
print "<body>your email address is <b>$payer_email</b>\n";
print "<br>you paid <b>$payment_gross</b>\n";
print "<br>you paid for <b>$item_number</b>\n";
print "<br>the color of your order was <b>$os0</b>\n";
print "<br>the value of custom was <b>$a</b>\n";
print "<br>the status was<b>$status</b>\n";
print "<br>the note said <b>$note</b>\n";
print "<br>the transaction id was <b>$txn_id</b>\n";
print "<br>the payment status was<b>$payment_status</b>\n";
print "</body></html>\n";
}
elsif ($res->content eq 'INVALID') {
# log for manual investigation
# print to screen the following if the IPN POST was INVALID
print "content-type: text/plain\n\nOK\n";
print "<html><head><title>IPN Screendump</title></head>\n";
print "<br>the status was<b>$status</b>\n";
print "</body></html>\n";
}
else {
# error
}