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