first crack at payment processing with self-service (step two of the process)
[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   use Business::CreditCard;
100
101   $cgi->param('amount') =~ /^\s*(\d+(\.\d{2})?)\s*$/
102     or die "illegal amount"; #!!!
103   my $amount = $1;
104
105   my $payinfo = $cgi->param('payinfo');
106   $payinfo =~ s/\D//g;
107   $payinfo =~ /^(\d{13,16})$/
108     #or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
109     or die "illegal card"; #!!!
110   $payinfo = $1;
111   validate($payinfo)
112     #or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
113     or die "invalid card"; #!!!
114   cardtype($payinfo) eq $cgi->param('card_type')
115     #or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
116     or die "not a ". $cgi->param('card_type');
117
118   $cgi->param('month') =~ /^(\d{2})$/ or die "illegal month";
119   my $month = $1;
120   $cgi->param('year') =~ /^(\d{4})$/ or die "illegal year";
121   my $year = $1;
122
123   $cgi->param('payname') =~ /^(.{0,80})$/ or die "illegal payname";
124   my $payname = $1;
125
126   $cgi->param('address1') =~ /^(.{0,80})$/ or die "illegal address1";
127   my $address1 = $1;
128
129   $cgi->param('address2') =~ /^(.{0,80})$/ or die "illegal address2";
130   my $address2 = $1;
131
132   $cgi->param('city') =~ /^(.{0,80})$/ or die "illegal city";
133   my $city = $1;
134
135   $cgi->param('state') =~ /^(.{2})$/ or die "illegal state";
136   my $state = $1;
137
138   $cgi->param('zip') =~ /^(.{0,10})$/ or die "illegal zip";
139   my $zip = $1;
140
141   my $save = 0;
142   $save = 1 if $cgi->param('save');
143
144   my $auto = 0;
145   $auto = 1 if $cgi->param('auto');
146
147   $cgi->param('paybatch') =~ /^([\w\-\.]+)$/ or die "illegal paybatch";
148   my $paybatch = $1;
149
150   process_payment(
151     'session_id' => $session_id,
152     'amount'     => $amount,
153     'payinfo'    => $payinfo,
154     'month'      => $month,
155     'year'       => $year,
156     'payname'    => $payname,
157     'address1'   => $address1,
158     'address2'   => $address2,
159     'city'       => $city,
160     'state'      => $state,
161     'zip'        => $zip,
162     'save'       => $save,
163     'auto'       => $auto,
164     'paybatch'   => $paybatch,
165   );
166
167 }
168
169 #--
170
171 sub do_template {
172   my $name = shift;
173   my $fill_in = shift;
174
175   $cgi->delete_all();
176   $fill_in->{'selfurl'} = $cgi->self_url;
177
178   my $template = new Text::Template( TYPE    => 'FILE',
179                                      SOURCE  => "$template_dir/$name.html",
180                                      DELIMITERS => [ '<%=', '%>' ],
181                                      UNTAINT => 1,                    )
182     or die $Text::Template::ERROR;
183
184   print $cgi->header( '-expires' => 'now' ),
185         $template->fill_in( HASH => $fill_in );
186 }
187