check password match on agent add'l package order
[freeside.git] / fs_selfservice / FS-SelfService / cgi / agent.cgi
1 #!/usr/bin/perl -Tw
2
3 #some false laziness w/selfservice.cgi
4
5 use strict;
6 use vars qw($cgi $session_id $form_max $template_dir);
7 use subs qw(do_template);
8 use CGI;
9 use CGI::Carp qw(fatalsToBrowser);
10 use Business::CreditCard;
11 use Text::Template;
12 use FS::SelfService qw( agent_login agent_info
13                         agent_list_customers
14                         signup_info new_customer
15                         customer_info order_pkg
16                       );
17
18 $template_dir = '.';
19
20 $form_max = 255;
21
22 $cgi = new CGI;
23
24 unless ( defined $cgi->param('session') ) {
25   do_template('agent_login',{});
26   exit;
27 }
28
29 if ( $cgi->param('session') eq 'login' ) {
30
31   $cgi->param('username') =~ /^\s*([a-z0-9_\-\.\&]{0,$form_max})\s*$/i
32     or die "illegal username";
33   my $username = $1;
34
35   $cgi->param('password') =~ /^(.{0,$form_max})$/
36     or die "illegal password";
37   my $password = $1;
38
39   my $rv = agent_login(
40     'username' => $username,
41     'password' => $password,
42   );
43   if ( $rv->{error} ) {
44     do_template('agent_login', {
45       'error'    => $rv->{error},
46       'username' => $username,
47     } );
48     exit;
49   } else {
50     $cgi->param('session' => $rv->{session_id} );
51     $cgi->param('action'  => 'agent_main' );
52   }
53 }
54
55 $session_id = $cgi->param('session');
56
57 $cgi->param('action') =~
58    /^(agent_main|signup|process_signup|list_customers|view_customer|process_order_pkg)$/
59   or die "unknown action ". $cgi->param('action');
60 my $action = $1;
61
62 warn "running $action\n";
63 my $result = eval "&$action();";
64 die $@ if $@;
65
66 if ( $result->{error} eq "Can't resume session" ) { #ick
67   do_template('agent_login',{});
68   exit;
69 }
70
71 warn "processing template $action\n";
72 do_template($action, {
73   'session_id' => $session_id,
74   %{$result}
75 });
76
77 #-- 
78
79 sub agent_main { agent_info( 'session_id' => $session_id ); }
80
81 sub signup { signup_info( 'session_id' => $session_id ); }
82
83 sub process_signup {
84
85   my $init_data = signup_info( 'session_id' => $session_id );
86   if ( $init_data->{'error'} ) {
87     if ( $init_data->{'error'} eq "Can't resume session" ) { #ick
88       do_template('agent_login',{});
89       exit;
90     } else { #?
91       die $init_data->{'error'};
92     }
93   }
94
95   my $error = '';
96
97   #some false laziness w/signup.cgi
98   my $payby = $cgi->param('payby');
99   if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
100     #$payinfo = join('@', map { $cgi->param( $payby. "_payinfo$_" ) } (1,2) );
101     $cgi->param('payinfo' => $cgi->param($payby. '_payinfo1'). '@'. 
102                              $cgi->param($payby. '_payinfo2')
103                );
104   } else {
105     $cgi->param('payinfo' => $cgi->param( $payby. '_payinfo' ) );
106   }
107   $cgi->param('paydate' => $cgi->param( $payby. '_month' ). '-'.
108                            $cgi->param( $payby. '_year' )
109              );
110   $cgi->param('payname' => $cgi->param( $payby. '_payname' ) );
111   $cgi->param('paycvv' => defined $cgi->param( $payby. '_paycvv' )
112                             ? $cgi->param( $payby. '_paycvv' )
113                             : ''
114              );
115
116   if ( $cgi->param('invoicing_list') ) {
117     $cgi->param('invoicing_list' => $cgi->param('invoicing_list'). ', POST')
118       if $cgi->param('invoicing_list_POST');
119   } else {
120     $cgi->param('invoicing_list' => 'POST' );
121   }
122
123   if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
124     $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
125     $cgi->param('_password', '');
126     $cgi->param('_password2', '');
127   }
128
129   if ( $payby =~ /^(CARD|DCRD)$/ && $cgi->param('CARD_type') ) {
130     my $payinfo = $cgi->param('payinfo');
131     $payinfo =~ s/\D//g;
132
133     $payinfo =~ /^(\d{13,16})$/
134       or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
135     $payinfo = $1;
136     validate($payinfo)
137       or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
138     cardtype($payinfo) eq $cgi->param('CARD_type')
139       or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
140   }
141
142   unless ( $error ) {
143     my $rv = new_customer ( {
144       'session_id'       => $session_id,
145       map { $_ => $cgi->param($_) }
146         qw( last first ss company
147             address1 address2 city county state zip country
148             daytime night fax
149             payby payinfo paycvv paydate payname invoicing_list
150             pkgpart username sec_phrase _password popnum refnum
151           ),
152         grep { /^snarf_/ } $cgi->param
153     } );
154     $error = $rv->{'error'};
155   }
156
157   if ( $error ) { 
158     $action = 'signup';
159     my $r = { 
160       $cgi->Vars,
161       %{$init_data},
162       'error' => $error,
163     };
164     #warn join('\n', map "$_ => $r->{$_}", keys %$r )."\n";
165     $r;
166   } else {
167     $action = 'agent_main';
168     my $agent_info = agent_info( 'session_id' => $session_id );
169     $agent_info->{'message'} = 'Signup sucessful';
170     $agent_info;
171   }
172
173 }
174
175 sub list_customers {
176   agent_list_customers( 'session_id' => $session_id,
177                         map { $_ => $cgi->param($_) }
178                           grep defined($cgi->param($_)),
179                                qw(prospect active susp cancel)
180                       );
181 }
182
183 sub view_customer {
184
185   my $init_data = signup_info( 'session_id' => $session_id );
186   if ( $init_data->{'error'} ) {
187     if ( $init_data->{'error'} eq "Can't resume session" ) { #ick
188       do_template('agent_login',{});
189       exit;
190     } else { #?
191       die $init_data->{'error'};
192     }
193   }
194
195   my $customer_info = customer_info (
196     'agent_session_id' => $session_id,
197     'custnum'          => $cgi->param('custnum')
198   );
199
200
201   return {
202     ( map { $_ => $init_data->{$_} }
203           qw( part_pkg security_phrase svc_acct_pop ),
204     ),
205     %$customer_info,
206   };
207 }
208
209 sub process_order_pkg {
210
211   if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
212     my $init_data = signup_info( 'session_id' => $session_id );
213     $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
214     $cgi->param('_password', '');
215     $cgi->param('_password2', '');
216   }
217
218   my $results = order_pkg (
219     'agent_session_id' => $session_id,
220     map { $_ => $cgi->param($_) }
221         qw( custnum pkgpart username _password _password2 sec_phrase popnum )
222   );
223
224   $action = 'view_customer';
225   $cgi->delete( grep { $_ ne 'custnum' } $cgi->param )
226     unless $results->{'error'};
227
228   return {
229     $cgi->Vars,
230     %{view_customer()},
231     'message' => $results->{'error'}
232                    ? '<FONT COLOR="#FF0000">'. $results->{'error'}. '</FONT>'
233                    : 'Package order sucessful.'
234   };
235
236 }
237
238 #--
239
240 sub do_template {
241   my $name = shift;
242   my $fill_in = shift;
243   #warn join(' / ', map { "$_=>".$fill_in->{$_} } keys %$fill_in). "\n";
244
245   $cgi->delete_all();
246   $fill_in->{'selfurl'} = $cgi->self_url;
247   $fill_in->{'cgi'} = \$cgi;
248
249   my $template = new Text::Template( TYPE    => 'FILE',
250                                      SOURCE  => "$template_dir/$name.html",
251                                      DELIMITERS => [ '<%=', '%>' ],
252                                      UNTAINT => 1,                    )
253     or die $Text::Template::ERROR;
254
255   print $cgi->header( '-expires' => 'now' ),
256         $template->fill_in( PACKAGE => 'FS::SelfService::_agentcgi',
257                             HASH    => $fill_in
258                           );
259 }
260
261 package FS::SelfService::_agentcgi;
262 use FS::SelfService qw(regionselector expselect popselector);
263