#!/usr/bin/perl -w
use strict;
use DBI;
use CGI;
use lib qw(../lib/);
use CommSecure;


=head1 LICENCE

cleared.pl - ComMSecure Cleared transactions 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 

# Start to get the CGI environment, and begin the response.
my $query = new CGI;
print $query->header(-type => 'text/plain');

# Connect to the database
my $dbh = DBI->connect("dbi:mysql:database=LCA2003-Reg");
# If that failed, then we decline and exit. Sorry, no ones home.
if (not defined $dbh) {
	print "Declined\n";
	$dbh->disconnect;
	my $msg = "connection was from " . ($query->remote_host|| 'unknown');
	die "Cannot connect to DB: ". $msg;
	}

# Is there a RefNum in the message? If not, then this is a waste of time.
if (not defined $query->param('RefNum')) {
	print "Declined\n";
	$dbh->disconnect;
	die "No RefNum in parameters";
	}

# There is a RefNum -- lets load up the transaction, 
my $t = CommSecure->new(-dbh => $dbh, -refnum => $query->param('RefNum'));

# Is this going to be OK?
my $is_ok = 1;

# Did we fail to load the transaction?
$is_ok =0 unless $t;
# Is it an invalid transaction?
$is_ok =0 unless $t->is_valid;
# Has it been validated?
$is_ok =0 unless $t->validate;

# Save it regardless (inless $t is undefined, in which case we throw it away)
defined $t && $t->cleared(-amount => $query->param('Amount')||'',
	-refnum => $query->param('RefNum')||'',
	-response => $query->param('Response')||'',
	-authno => $query->param('AuthNo')||'',
	-audit => $query->param('Audit')||'',
	-errormsg => $query->param('ErrorMsg')||'',
	-sig => $query->param('Sig')||'',
	-host => $query->remote_host,
	-date => time(),
	);

if (not $is_ok || not $t->confirm->transaction_cleared) {
	warn "Confirmation from " . $query->remote_host . " was declined";
	print "Declined\n";
	$dbh->disconnect;
	exit;
}

warn "Confirmation accepted from " . $query->remote_host;
print "Accepted\n";

# Now generate a receipt.


use Receipt;
use Person;
use Email;
#warn "About to generate receipt for custnum " . $t->request->custnum;
my ($p,undef) = Person->find(-dbh => $dbh, -custnum => $t->request->custnum);
die "No Person!" unless defined $p;
#warn "Found person " . $p->name;
my $r = Receipt->new(-dbh => $dbh, -person => $p, -commsecure => $t, -generate => 1);
die "No receipt" unless defined $r;
my $signed = $r->text;
die "No text" unless $signed;
my $e = Email->new(-to => $p->email_address, -bcc => 'receipts@linux.conf.au', -subject => "[LCA2003] Payment Receipt: " . $p->name, -content => $signed);
