#!/usr/bin/perl -w
use strict;
use CGI;
use LWP::Simple;
my $q = new CGI;

=head1 LICENCE

commsecure_server.pl - CommSecure Cleared transactions Test Server script
Copyright (C) 2002 James Bromberger <james@rcpt.to>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


=cut


sub prompt_to_verify {
	print $q->p('Verify this transaction');
	print $q->start_form;
	print $q->table( $q->Tr(
		[ $q->td(['MerchantNum: ', $q->textfield(-name=>'MerchantNum', -default => $q->param('MerchantNum')||'')]) . "\n",
		  $q->td(['RefNum: ' , $q->textfield(-name=>'RefNum', -default => $q->param('RefNum')||'')]) ."\n",
		  $q->td(['Amount: ' , $q->textfield(-name => 'Amount', -default => $q->param('Amount')||'', -size => 5)]) . "\n",
		  $q->td(['Description: ' , $q->textfield(-name => 'Description', -default => $q->param('Description')||'')]) . "\n",
		  $q->td(['CustNum: ' , $q->textfield(-name => 'CustNum', -default => $q->param('CustNum')||'')]) . "\n",
		  $q->td([$q->hidden(-name => 'sub', -value => 1), $q->submit(-name => 'Go')]) . "\n",
		]));
	print $q->end_form;
}

sub do_verify {
	my $content = get ('http://127.0.0.1/register/commsecure/validate.cgi?&RefNum=' . $q->param('RefNum') . '&Amount=' . $q->param('Amount'));
	return $content;
}

sub prompt_to_confirm {
	print $q->p('Give a result for a transaction');
	print $q->start_form;
	print $q->table (
		$q->Tr (
			[ $q->td( ['RefNum: ', $q->textfield(-name => 'RefNum', -default => $q->param('RefNum')||'')]),
			  $q->td( ['Amount: ', $q->textfield(-name => 'Amount', -default => $q->param('Amount')||'', -size => 5)]),
			  $q->td( ['Response (eg, A,D): ', $q->textfield(-name => 'Response', -size => 5)]),
			  $q->td( ['AuthNo: ', $q->textfield(-name => 'AuthNo', -size => 10)]),
			  $q->td( ['Audit: ', $q->textfield(-name => 'Audit', -size => 10)]),
			  $q->td( ['ErrorMsg: ', $q->textfield(-name => 'ErrorMsg')]),
			  $q->td( ['Sig: ', $q->textfield(-name=> 'Sig')]),
			  $q->td( [$q->hidden(-name => 'sub', -value => 1), $q->submit(-name => 'Go')]),
			  ]));
	print $q->end_form;
}

sub do_confirm {
	print $q->p("Doing CONFIRM");
	my $content = get ('http://127.0.0.1/register/commsecure/cleared.cgi?RefNum=' . $q->param('RefNum') . '&Amount=' . $q->param('Amount') . '&Response=' . $q->param('Response') . '&AuthNo=' . ($q->param('AuthNo')||'') . '&Audit=' . ($q->param('AuthNo')||'') . '&ErrorMsg=' . ($q->param('ErrorMsg')||'') . '&Sig=' . $q->param('Sig'));

	return $content;
}



print $q->header;
print $q->start_html(-title => "James's CommSecure Test Server",
			-author => 'james@rcpt.to',
			-style => {-code => "body { margin: 10%; margin-top: 2%; font-family: sans-serif;} "},
			);
print $q->h1('CommSecure would be here!');
print $q->p('Please note that this is <strong>NOT COMMSECURE</strong>. Do not enter your real credit card details here.');
if (defined $q->param('sub') && defined $q->param('MerchantNum') and defined $q->param('RefNum') and defined $q->param('Amount')) {
	print $q->p('The last verify resulted in: <strong>' . (do_verify()|| "err") . '</srong>');
} elsif (defined $q->param('sub') && defined $q->param('RefNum') and defined $q->param('Amount') and defined $q->param('Response') and defined $q->param('Sig')) {
	# Result
	print $q->p('The last confirm resulted in: ' . do_confirm());
	print $q->p('You are done now');
} else {
	# Just arrived.
}

print "<table><tr><td valign='top'>";
prompt_to_verify();
print "</td><td>- OR -</td><td valign='top'>";
prompt_to_confirm();
print "</td></tr></table>\n";

print $q->end_html;
