For a site I'm doing that allows paypal payments I need to respond to a POST from paypal, rePOST back to them adding a reponse code and then check their response for validity. Paypal has supplied a sample Perl script to do this but I would rather hadnle this in PHP (especially the database updates) since the rest of the site is in PHP (and since I haven't used Perl that much).
The sample Perl script is as follows;
#!/usr/local/bin/perl
#read the 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','https://www.paypal.com/cgi-bin/webscr'; $req->content_type('application/x-www-form-urlencoded');
$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 $receiver_email = $variable{'receiver_email'};
$item_name = $variable{'item_name'}; $item_number = $variable{'item_number'}; $custom = $variable{'custom'}; $payment_status=$variable 'payment_status'};
....
$payer_email = $variable{'payer_email'};
if ($res->content eq 'VERIFIED') {
check transaction for uniqueness
process payment }
elsif ($res->content eq 'INVALID') {
possible fraud }
else { # error}
Most of the script is straight forward and can be done in PHP. I know that I can use fsockopen() to do the respond to the POST, but I can't figure out how to check the response ($res->content). My only thoughts right now is to use this script and to rePOST back to a PHP page from this script after the script checks for 'VERIFIED' or 'INVALID'.
Does anyone have thoughts on this.
Thanks,
Larry