#!/usr/bin/perl use strict; my $app = Self->new( 'PARAMS' => { 'mode_param' => 'action', 'run_modes' => { 'AUTOLOAD' => 'input_display', 'display' => 'input_display', 'finalise' => 'input_finalise', 'update' => 'input_update' }, 'start_mode' => 'display' } ); $app->run; exit 0; package Self; BEGIN { unshift @INC, '.'; } use Apache::Session::DB_File; use Business::CardAccess; use CGI::Carp; use Data::Dumper; use Template; use base 'CGI::Application'; use strict; sub setup { my $self = shift; $self->run_modes( $self->param('run_modes') || { 'start' => 'dump_html' } ); $self->mode_param( $self->param('mode_param') || 'rm' ); $self->start_mode( $self->param('start_mode') || 'start' ); $self->param( 'template' => Template->new({ 'INCLUDE_PATH' => $self->param('tmpl_path') || '../templates' }) ); } sub input_display { my $self = shift; my $cgi = $self->query; my %session; eval { my $session_id = $cgi->cookie( -name => 'ecom', -path => '/' ); tie %session, 'Apache::Session::DB_File', $session_id, { 'FileName' => '.sessions' }; }; tie %session, 'Apache::Session::DB_File', undef, { 'FileName' => '.sessions' } if $@; $session{'payments'} ||= [ { 'index' => 0, 'description' => 'One-Time Payment', 'amount' => 0 } ]; $self->header_props( -cookie => $cgi->cookie( -name => 'ecom', -path => '/', -value => $session{'_session_id'} ) ); my $html = ''; $self->param('template')->process('display.tt2', \%session, \$html); untie %session; return $html; } sub input_update { my $self = shift; my $cgi = $self->query; my %session; eval { my $session_id = $cgi->cookie( -name => 'ecom', -path => '/' ); tie %session, 'Apache::Session::DB_File', $session_id, { 'FileName' => '.sessions' }; }; croak( 'Cannot retrieve user session ID from client-side cookie' ) if $@; $session{'payments'} ||= []; my @payments = @{$session{'payments'}}; foreach (0..$#payments) { $cgi->param('description_'.$_, $1) if $cgi->param('description_'.$_) =~ /^([\w\-\ ]+)$/; $cgi->param('amount_'.$_, $1) if $cgi->param('amount_'.$_) =~ /^\$?(\d+(?:|\.\d{1,2}))$/; splice( @payments, $_, 1, { 'index' => $_, 'description' => $cgi->param('description_'.$_), 'amount' => $cgi->param('amount_'.$_) } ); splice(@payments, $_, 1) if defined $cgi->param('delete_'.$_); } $session{'payments'} = \@payments; my $html = ''; $self->param('template')->process('display.tt2', \%session, \$html); untie %session; return $html; } 1; __END__