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