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