2ce2c8b6c319658eef0ecd0ad2008c465ffbafbe
[freeside.git] / fs_selfservice / FS-SelfService / cgi / selfservice.cgi
1 #!/usr/bin/perl -Tw
2
3 use strict;
4 use vars qw($cgi $session_id $form_max $template_dir);
5 use subs qw(do_template);
6 use CGI;
7 use CGI::Carp qw(fatalsToBrowser);
8 use Text::Template;
9 use FS::SelfService qw(login customer_info invoice payment_info);
10
11 $template_dir = '.';
12
13 $form_max = 255;
14
15 $cgi = new CGI;
16
17 unless ( defined $cgi->param('session') ) {
18   do_template('login',{});
19   exit;
20 }
21
22 if ( $cgi->param('session') eq 'login' ) {
23
24   $cgi->param('username') =~ /^\s*([a-z0-9_\-\.\&]{0,$form_max})\s*$/i
25     or die "illegal username";
26   my $username = $1;
27
28   $cgi->param('domain') =~ /^\s*([\w\-\.]{0,$form_max})\s*$/
29     or die "illegal domain";
30   my $domain = $1;
31
32   $cgi->param('password') =~ /^(.{0,$form_max})$/
33     or die "illegal password";
34   my $password = $1;
35
36   my $rv = login(
37     'username' => $username,
38     'domain'   => $domain,
39     'password' => $password,
40   );
41   if ( $rv->{error} ) {
42     do_template('login', {
43       'error'    => $rv->{error},
44       'username' => $username,
45       'domain'   => $domain,
46     } );
47     exit;
48   } else {
49     $cgi->param('session' => $rv->{session_id} );
50     $cgi->param('action'  => 'myaccount' );
51   }
52 }
53
54 $session_id = $cgi->param('session');
55
56 $cgi->param('action') =~
57     /^(myaccount|view_invoice|make_payment|process_payment)$/
58   or die "unknown action ". $cgi->param('action');
59 my $action = $1;
60
61 my $result = eval "&$action();";
62 die $@ if $@;
63
64 if ( $result->{error} eq "Can't resume session" ) { #ick
65   do_template('login',{});
66   exit;
67 }
68
69 #warn $result->{'open_invoices'};
70 #warn scalar(@{$result->{'open_invoices'}});
71
72 warn "processing template $action\n";
73 do_template($action, {
74   'session_id' => $session_id,
75   %{$result}
76 });
77
78 #--
79
80 sub myaccount { customer_info( 'session_id' => $session_id ); }
81
82 sub view_invoice {
83
84   $cgi->param('invnum') =~ /^(\d+)$/ or die "illegal invnum";
85   my $invnum = $1;
86
87   invoice( 'session_id' => $session_id,
88            'invnum'     => $invnum,
89          );
90
91 }
92
93 sub make_payment {
94   payment_info( 'session_id' => $session_id );
95 }
96
97 sub process_payment {
98
99   $cgi->param('amount') =~ /^\s*(\d+(\.\d{2})?)\s*$/
100     or die "illegal amount"; #!!!
101   my $amount = $1;
102
103   my $payinfo = $cgi->param('payinfo');
104   $payinfo =~ s/\D//g;
105   $payinfo =~ /^(\d{13,16})$/
106     #or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
107     or die "illegal card"; #!!!
108   $payinfo = $1;
109   validate($payinfo)
110     #or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
111     or die "invalid card"; #!!!
112   cardtype($payinfo) eq $cgi->param('card_type')
113     #or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
114     or die "not a ". $cgi->param('card_type');
115
116   $cgi->param('month') =~ /^(\d{2})$/ or die "illegal month";
117   my $month = $1;
118   $cgi->param('year') =~ /^(\d{4})$/ or die "illegal year";
119   my $year = $1;
120
121   $cgi->param('payname') =~ /^(.{0,80})$/ or die "illegal payname";
122   my $payname = $1;
123
124   $cgi->param('address1') =~ /^(.{0,80})$/ or die "illegal address1";
125   my $address1 = $1;
126
127   $cgi->param('address2') =~ /^(.{0,80})$/ or die "illegal address2";
128   my $address2 = $1;
129
130   $cgi->param('city') =~ /^(.{0,80})$/ or die "illegal city";
131   my $city = $1;
132
133   $cgi->param('state') =~ /^(.{2})$/ or die "illegal state";
134   my $state = $1;
135
136   $cgi->param('zip') =~ /^(.{0,10})$/ or die "illegal zip";
137   my $zip = $1;
138
139   my $save = 0;
140   $save = 1 if $cgi->param('save');
141
142   my $auto = 0;
143   $auto = 1 if $cgi->param('auto');
144
145   $cgi->param('paybatch') =~ /^([\w\-\.]+)$/ or die "illegal paybatch";
146   my $patbatch = $1;
147
148   process_payment(
149     'session_id' => $session_id,
150     'amount'     => $amount,
151     'payinfo'    => $payinfo,
152     'month'      => $month,
153     'year'       => $year,
154     'payname'    => $payname,
155     'address1'   => $address1,
156     'address2'   => $address2,
157     'city'       => $city,
158     'state'      => $state,
159     'zip'        => $zip,
160     'save'       => $save,
161     'auto'       => $auto,
162     'paybatch'   => $paybatch,
163   );
164
165 }
166
167 #--
168
169 sub do_template {
170   my $name = shift;
171   my $fill_in = shift;
172
173   $cgi->delete_all();
174   $fill_in->{'selfurl'} = $cgi->self_url;
175
176   my $template = new Text::Template( TYPE    => 'FILE',
177                                      SOURCE  => "$template_dir/$name.html",
178                                      DELIMITERS => [ '<%=', '%>' ],
179                                      UNTAINT => 1,                    )
180     or die $Text::Template::ERROR;
181
182   print $cgi->header( '-expires' => 'now' ),
183         $template->fill_in( HASH => $fill_in );
184 }
185