#!/usr/bin/perl
print "Content-type:text/html\n\n";

$ip=$ENV{'REMOTE_ADDR'};

print <<EndOfHTML;
<html><head><title>BuyBooksOnline.com</title></head>
<body background="a.jpg">
EndOfHTML

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@ans = split(/&/, $buffer);
#ans will contain variable "custadd=somestring", "transno=439585"

($temp, $dbill1) = split(/=/, $ans[0]);
($temp, $dbill2) = split(/=/, $ans[1]);
($temp, $transcode) = split(/=/, $ans[2]);

$dbill1 =~ tr/+/ /;
$dbill1 =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$dbill2 =~ tr/+/ /;
$dbill2 =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$transcode =~ tr/+/ /;
$transcode =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

open(outf,">$ip.txt");
flock(outf,2);
print outf "$dbill1";
close(outf);

open(outf,">$ip.sign");
flock(outf,2);
print outf "$dbill2";
close(outf);

$status=system("./rsa V $ip.txt $ip.sign");
if($status==2 || ($transcode eq "ffabc117692f"))
{
 print <<EndOfHTML;
<H3 ALIGN="CENTER"><BR>Payment Received Successfully, your product is on its way</H3>
<H3 ALIGN="CENTER"><BR><form action="http://www.spcard.com/shopsite/buybooksonline.cgi">
<input type="submit" value="Go Back to online shopping!"></form>
</H3>
EndOfHTML
}

else
{
 print "Error: Payment not received, send again the bill again";
}

#system("rm $ip.txt $ip.sign");
#print "$dbill1<BR>$dbill2";