a09189270b0ad173c9d4a0eb1ef6b54d3ef26682
[freeside.git] / fs_selfservice / FS-SelfService / cgi / signup.cgi
1 #!/usr/bin/perl -T
2 #!/usr/bin/perl -Tw
3 #
4 # $Id: signup.cgi,v 1.3 2006-08-29 17:50:06 lawrence Exp $
5
6 use strict;
7 use vars qw( @payby $cgi $init_data
8              $self_url $error $agentnum
9
10              $ieak_file $ieak_template
11              $signup_html $signup_template
12              $success_html $success_template
13              $decline_html $decline_template
14            );
15
16 use subs qw( print_form print_okay print_decline
17              success_default decline_default
18            );
19 use CGI;
20 #use CGI::Carp qw(fatalsToBrowser);
21 use Text::Template;
22 use Business::CreditCard;
23 use HTTP::BrowserDetect;
24 use FS::SelfService qw( signup_info new_customer );
25
26 #acceptable payment methods
27 #
28 #@payby = qw( CARD BILL COMP );
29 #@payby = qw( CARD BILL );
30 #@payby = qw( CARD );
31 @payby = qw( CARD PREPAY );
32
33 $ieak_file = '/usr/local/freeside/ieak.template';
34 $signup_html = -e 'signup.html'
35                  ? 'signup.html'
36                  : '/usr/local/freeside/signup.html';
37 $success_html = -e 'success.html'
38                   ? 'success.html'
39                   : '/usr/local/freeside/success.html';
40 $decline_html = -e 'decline.html'
41                   ? 'decline.html'
42                   : '/usr/local/freeside/decline.html';
43
44
45 if ( -e $ieak_file ) {
46   my $ieak_txt = Text::Template::_load_text($ieak_file)
47     or die $Text::Template::ERROR;
48   $ieak_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
49   $ieak_txt = $1;
50   $ieak_txt =~ s/\r//g; # don't double \r on old templates
51   $ieak_txt =~ s/\n/\r\n/g;
52   $ieak_template = new Text::Template ( TYPE => 'STRING', SOURCE => $ieak_txt )
53     or die $Text::Template::ERROR;
54 } else {
55   $ieak_template = '';
56 }
57
58 $agentnum = '';
59 if ( -e $signup_html ) {
60   my $signup_txt = Text::Template::_load_text($signup_html)
61     or die $Text::Template::ERROR;
62   $signup_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
63   $signup_txt = $1;
64   $signup_template = new Text::Template ( TYPE => 'STRING',
65                                           SOURCE => $signup_txt,
66                                           DELIMITERS => [ '<%=', '%>' ]
67                                         )
68     or die $Text::Template::ERROR;
69   if ( $signup_txt =~
70          /<\s*INPUT TYPE="?hidden"?\s+NAME="?agentnum"?\s+VALUE="?(\d+)"?\s*>/si
71   ) {
72     $agentnum = $1;
73   }
74 } else {
75   #too much maintenance hassle to keep in this file
76   die "can't find ./signup.html or /usr/local/freeside/signup.html";
77   #$signup_template = new Text::Template ( TYPE => 'STRING',
78   #                                        SOURCE => &signup_default,
79   #                                        DELIMITERS => [ '<%=', '%>' ]
80   #                                      )
81   #  or die $Text::Template::ERROR;
82 }
83
84 if ( -e $success_html ) {
85   my $success_txt = Text::Template::_load_text($success_html)
86     or die $Text::Template::ERROR;
87   $success_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
88   $success_txt = $1;
89   $success_template = new Text::Template ( TYPE => 'STRING',
90                                            SOURCE => $success_txt,
91                                            DELIMITERS => [ '<%=', '%>' ],
92                                          )
93     or die $Text::Template::ERROR;
94 } else {
95   $success_template = new Text::Template ( TYPE => 'STRING',
96                                            SOURCE => &success_default,
97                                            DELIMITERS => [ '<%=', '%>' ],
98                                          )
99     or die $Text::Template::ERROR;
100 }
101
102 if ( -e $decline_html ) {
103   my $decline_txt = Text::Template::_load_text($decline_html)
104     or die $Text::Template::ERROR;
105   $decline_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
106   $decline_txt = $1;
107   $decline_template = new Text::Template ( TYPE => 'STRING',
108                                            SOURCE => $decline_txt,
109                                            DELIMITERS => [ '<%=', '%>' ],
110                                          )
111     or die $Text::Template::ERROR;
112 } else {
113   $decline_template = new Text::Template ( TYPE => 'STRING',
114                                            SOURCE => &decline_default,
115                                            DELIMITERS => [ '<%=', '%>' ],
116                                          )
117     or die $Text::Template::ERROR;
118 }
119
120 $cgi = new CGI;
121
122 $init_data = signup_info( 'agentnum'   => $agentnum,
123                           'promo_code' => scalar($cgi->param('promo_code')),
124                           'reg_code'   => uc(scalar($cgi->param('reg_code'))),
125                         );
126
127 if (    ( defined($cgi->param('magic')) && $cgi->param('magic') eq 'process' )
128      || ( defined($cgi->param('action')) && $cgi->param('action') eq 'process_signup' )
129    ) {
130
131     $error = '';
132
133     $cgi->param('agentnum', $agentnum) if $agentnum;
134     $cgi->param('reg_code', uc(scalar($cgi->param('reg_code'))) );
135
136     #false laziness w/agent.cgi, identical except for agentnum
137     my $payby = $cgi->param('payby');
138     if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
139       #$payinfo = join('@', map { $cgi->param( $payby. "_payinfo$_" ) } (1,2) );
140       $cgi->param('payinfo' => $cgi->param($payby. '_payinfo1'). '@'. 
141                                $cgi->param($payby. '_payinfo2')
142                  );
143     } else {
144       $cgi->param('payinfo' => $cgi->param( $payby. '_payinfo' ) );
145     }
146     $cgi->param('paydate' => $cgi->param( $payby. '_month' ). '-'.
147                              $cgi->param( $payby. '_year' )
148                );
149     $cgi->param('payname' => $cgi->param( $payby. '_payname' ) );
150     $cgi->param('paycvv' => defined $cgi->param( $payby. '_paycvv' )
151                               ? $cgi->param( $payby. '_paycvv' )
152                               : ''
153                );
154
155     if ( $cgi->param('invoicing_list') ) {
156       $cgi->param('invoicing_list' => $cgi->param('invoicing_list'). ', POST')
157         if $cgi->param('invoicing_list_POST');
158     } else {
159       $cgi->param('invoicing_list' => 'POST' );
160     }
161
162     if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
163       $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
164       $cgi->param('_password', '');
165       $cgi->param('_password2', '');
166     }
167
168     if ( $payby =~ /^(CARD|DCRD)$/ && $cgi->param('CARD_type') ) {
169       my $payinfo = $cgi->param('payinfo');
170       $payinfo =~ s/\D//g;
171
172       $payinfo =~ /^(\d{13,16})$/
173         or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
174       $payinfo = $1;
175       validate($payinfo)
176         or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
177       cardtype($payinfo) eq $cgi->param('CARD_type')
178         or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
179     }
180
181     if ($init_data->{emailinvoiceonly} && (length $cgi->param('invoicing_list') < 1)) {
182         $error ||= $init_data->{msgcat}{illegal_or_empty_text};
183     }
184
185     unless ( $error ) {
186       my $rv = new_customer( {
187         map { $_ => scalar($cgi->param($_)) }
188           qw( last first ss company
189               address1 address2 city county state zip country
190               daytime night fax
191
192               ship_last ship_first ship_company
193               ship_address1 ship_address2 ship_city ship_county ship_state
194                 ship_zip ship_country
195               ship_daytime ship_night ship_fax
196
197               payby payinfo paycvv paydate payname invoicing_list
198               referral_custnum promo_code reg_code
199               pkgpart username sec_phrase _password popnum refnum
200               agentnum
201             ),
202           grep { /^snarf_/ } $cgi->param
203       } );
204       $error = $rv->{'error'};
205     }
206     #eslaf
207     
208     if ( $error eq '_decline' ) {
209       print_decline();
210     } elsif ( $error ) {
211       #fudge the snarf info
212       no strict 'refs';
213       ${$_} = $cgi->param($_) foreach grep { /^snarf_/ } $cgi->param;
214       print_form();
215     } else {
216       print_okay(
217         'pkgpart' => scalar($cgi->param('pkgpart')),
218       );
219     }
220
221 } else {
222   $error = '';
223   print_form;
224 }
225
226 sub print_form {
227
228   $error = "Error: $error" if $error;
229
230   my $r = {
231     $cgi->Vars,
232     %{$init_data},
233     'error' => $error,
234   };
235
236   $r->{referral_custnum} = $r->{'ref'};
237   #$cgi->delete('ref');
238   #$cgi->delete('init_popstate');
239   $r->{self_url} = $cgi->self_url;
240
241   print $cgi->header( '-expires' => 'now' ),
242         $signup_template->fill_in( PACKAGE => 'FS::SelfService::_signupcgi',
243                                    HASH    => $r
244                                  );
245 }
246
247 sub print_decline {
248   print $cgi->header( '-expires' => 'now' ),
249         $decline_template->fill_in();
250 }
251
252 sub print_okay {
253   my %param = @_;
254   my $user_agent = new HTTP::BrowserDetect $ENV{HTTP_USER_AGENT};
255
256   $cgi->param('username') =~ /^(.+)$/
257     or die "fatal: invalid username got past FS::SelfService::new_customer";
258   my $username = $1;
259   $cgi->param('_password') =~ /^(.+)$/
260     or die "fatal: invalid password got past FS::SelfService::new_customer";
261   my $password = $1;
262   ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
263     or die "fatal: invalid email_name got past FS::SelfService::new_customer";
264   my $email_name = $1; #global for template
265
266   #my %pop = ();
267   my %popnum2pop = ();
268   foreach ( @{ $init_data->{'svc_acct_pop'} } ) {
269     #push @{ $pop{ $_->{state} }->{ $_->{ac} } }, $_;
270     $popnum2pop{$_->{popnum}} = $_;
271   }
272
273   my( $ac, $exch, $loc);
274   my $pop = $popnum2pop{$cgi->param('popnum')};
275     #or die "fatal: invalid popnum got past FS::SelfService::new_customer";
276   if ( $pop ) {
277     ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
278   } else {
279     ( $ac, $exch, $loc ) = ( '', '', ''); #presumably you're not using them.
280   }
281
282   #global for template
283   my $pkg = ( grep { $_->{'pkgpart'} eq $param{'pkgpart'} }
284                    @{ $init_data->{'part_pkg'} }
285             )[0]->{'pkg'};
286
287   if ( $ieak_template && $user_agent->windows && $user_agent->ie ) {
288     #send an IEAK config
289     print $cgi->header('application/x-Internet-signup'),
290           $ieak_template->fill_in();
291   } else { #send a simple confirmation
292     print $cgi->header( '-expires' => 'now' ),
293           $success_template->fill_in( HASH => {
294             username   => $username,
295             password   => $password,
296             _password  => $password,
297             email_name => $email_name,
298             ac         => $ac,
299             exch       => $exch,
300             loc        => $loc,
301             pkg        => $pkg,
302           });
303   }
304 }
305
306 sub success_default { #html to use if you don't specify a success file
307   <<'END';
308 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
309 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
310 Thanks for signing up!
311 <BR><BR>
312 Signup information for <%= $email_name %>:
313 <BR><BR>
314 Username: <%= $username %><BR>
315 Password: <%= $password %><BR>
316 Access number: (<%= $ac %>) / <%= $exch %> - <%= $local %><BR>
317 Package: <%= $pkg %><BR>
318 </BODY></HTML>
319 END
320 }
321
322 sub decline_default { #html to use if there is a decline
323   <<'END';
324 <HTML><HEAD><TITLE>Processing error</TITLE></HEAD>
325 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Processing error</FONT><BR><BR>
326 There has been an error processing your account.  Please contact customer
327 support.
328 </BODY></HTML>
329 END
330 }
331
332 # subs for the templates...
333
334 package FS::SelfService::_signupcgi;
335 use HTML::Entities;
336 use FS::SelfService qw(regionselector expselect popselector);
337