C
custodian
I've been using a paypal ipn script for a few weeks and everything has
been fine, though I continually receive a "Premature end of script
headers" in my server logs. My concern of this was not immediate as the
script was still working as it should.
I'm now installing another shopping cart that relys on PHP and MySQL
which required me to install some additional items. In doing so I went
from Perl 5.8.6 to 5.8.7. Prior to this I was also using LWP which was
required by the script. I'm still getting the same error in the server
logs of "Premature end of script headers", but the output file used to
show the transaction (because it was working), now I receive
Transaction Error to the output file. I'm trying to determine if the
problem is with the perl code, or paypal. Can anyone see an error in
this that I am not seeing.
#!/usr/bin/perl
# program start
use CGI;
require "cartconfig.pl";
# 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','http://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;
$FORM{$name} = $value;
$count++;
}
if ($res->is_error) {
$response="HTTP Error - Verify Payment Manually";
$paid="N";
}
if ($res->content eq 'VERIFIED' && uc($FORM{'receiver_email'}) eq
uc($PayPal_email)) {
$date=&get_date;
$response="PayPal Payment $FORM{'payment_status'} $date
Transaction ID: $FORM{'txn_id'}";
if ($FORM{'payment_status'} eq "Completed") {$paid="Y";} else
{$paid="N";}
}
else {
$response="PayPal Transaction Error";
$paid="N";
}
open(IN,"$order_file");
while(<IN>) {
chomp;
@field=split(/\|/,$_);
if ($field[0] eq $FORM{'item_number'}) {
$field[4]=$response;
}
$output.=join("|",@field);
$output.="\n";
}
close(IN);
open(OUT,">$order_file");
print OUT $output;
close(OUT);
#open (OUT,">ipn_test.txt");
#print OUT "$response\n\n";
#foreach $key (%FORM) {
# print OUT "$key: $FORM{$key}\n";
#}
#close(OUT);
The cartconfig.pl variables are mere email and return pages, which have
all been verified and working.
$ perl -c paypal_ipn.cgi
paypal_ipn.cgi syntax OK
Any suggestions? Is thi the perl code or do I need to look into apache
or paypal themselves?
Thanks!
Henry
been fine, though I continually receive a "Premature end of script
headers" in my server logs. My concern of this was not immediate as the
script was still working as it should.
I'm now installing another shopping cart that relys on PHP and MySQL
which required me to install some additional items. In doing so I went
from Perl 5.8.6 to 5.8.7. Prior to this I was also using LWP which was
required by the script. I'm still getting the same error in the server
logs of "Premature end of script headers", but the output file used to
show the transaction (because it was working), now I receive
Transaction Error to the output file. I'm trying to determine if the
problem is with the perl code, or paypal. Can anyone see an error in
this that I am not seeing.
#!/usr/bin/perl
# program start
use CGI;
require "cartconfig.pl";
# 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','http://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;
$FORM{$name} = $value;
$count++;
}
if ($res->is_error) {
$response="HTTP Error - Verify Payment Manually";
$paid="N";
}
if ($res->content eq 'VERIFIED' && uc($FORM{'receiver_email'}) eq
uc($PayPal_email)) {
$date=&get_date;
$response="PayPal Payment $FORM{'payment_status'} $date
Transaction ID: $FORM{'txn_id'}";
if ($FORM{'payment_status'} eq "Completed") {$paid="Y";} else
{$paid="N";}
}
else {
$response="PayPal Transaction Error";
$paid="N";
}
open(IN,"$order_file");
while(<IN>) {
chomp;
@field=split(/\|/,$_);
if ($field[0] eq $FORM{'item_number'}) {
$field[4]=$response;
}
$output.=join("|",@field);
$output.="\n";
}
close(IN);
open(OUT,">$order_file");
print OUT $output;
close(OUT);
#open (OUT,">ipn_test.txt");
#print OUT "$response\n\n";
#foreach $key (%FORM) {
# print OUT "$key: $FORM{$key}\n";
#}
#close(OUT);
The cartconfig.pl variables are mere email and return pages, which have
all been verified and working.
$ perl -c paypal_ipn.cgi
paypal_ipn.cgi syntax OK
Any suggestions? Is thi the perl code or do I need to look into apache
or paypal themselves?
Thanks!
Henry