4 # $Id: signup.cgi,v 1.29.2.22 2004-10-01 01:38:03 ivan Exp $
7 use vars qw( @payby $cgi $locales $packages
10 $last $first $ss $company $address1 $address2 $city $state $county
11 $country $zip $daytime $night $fax $invoicing_list $payby $payinfo
12 $paycvv $paydate $payname $referral_custnum $init_popstate
13 $pkgpart $username $password $password2 $sec_phrase $popnum
15 $ieak_file $ieak_template
16 $signup_html $signup_template
17 $success_html $success_template
18 $decline_html $decline_template
23 use subs qw( print_form print_okay print_decline
24 success_default decline_default
27 #use CGI::Carp qw(fatalsToBrowser);
29 use Business::CreditCard;
30 use HTTP::BrowserDetect;
31 use FS::SignupClient 0.03 qw( signup_info new_customer );
33 #acceptable payment methods
35 #@payby = qw( CARD BILL COMP );
36 #@payby = qw( CARD BILL );
38 @payby = qw( CARD PREPAY );
40 $ieak_file = '/usr/local/freeside/ieak.template';
41 $signup_html = -e 'signup.html'
43 : '/usr/local/freeside/signup.html';
44 $success_html = -e 'success.html'
46 : '/usr/local/freeside/success.html';
47 $decline_html = -e 'decline.html'
49 : '/usr/local/freeside/decline.html';
52 if ( -e $ieak_file ) {
53 my $ieak_txt = Text::Template::_load_text($ieak_file)
54 or die $Text::Template::ERROR;
55 $ieak_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
57 $ieak_txt =~ s/\r//g; # don't double \r on old templates
58 $ieak_txt =~ s/\n/\r\n/g;
59 $ieak_template = new Text::Template ( TYPE => 'STRING', SOURCE => $ieak_txt )
60 or die $Text::Template::ERROR;
66 if ( -e $signup_html ) {
67 my $signup_txt = Text::Template::_load_text($signup_html)
68 or die $Text::Template::ERROR;
69 $signup_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
71 $signup_template = new Text::Template ( TYPE => 'STRING',
72 SOURCE => $signup_txt,
73 DELIMITERS => [ '<%=', '%>' ]
75 or die $Text::Template::ERROR;
77 /<\s*INPUT TYPE="?hidden"?\s+NAME="?agentnum"?\s+VALUE="?(\d+)"?\s*>/si
82 #too much maintenance hassle to keep in this file
83 die "can't find ./signup.html or /usr/local/freeside/signup.html";
84 #$signup_template = new Text::Template ( TYPE => 'STRING',
85 # SOURCE => &signup_default,
86 # DELIMITERS => [ '<%=', '%>' ]
88 # or die $Text::Template::ERROR;
91 if ( -e $success_html ) {
92 my $success_txt = Text::Template::_load_text($success_html)
93 or die $Text::Template::ERROR;
94 $success_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
96 $success_template = new Text::Template ( TYPE => 'STRING',
97 SOURCE => $success_txt,
98 DELIMITERS => [ '<%=', '%>' ],
100 or die $Text::Template::ERROR;
102 $success_template = new Text::Template ( TYPE => 'STRING',
103 SOURCE => &success_default,
104 DELIMITERS => [ '<%=', '%>' ],
106 or die $Text::Template::ERROR;
109 if ( -e $decline_html ) {
110 my $decline_txt = Text::Template::_load_text($decline_html)
111 or die $Text::Template::ERROR;
112 $decline_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
114 $decline_template = new Text::Template ( TYPE => 'STRING',
115 SOURCE => $decline_txt,
116 DELIMITERS => [ '<%=', '%>' ],
118 or die $Text::Template::ERROR;
120 $decline_template = new Text::Template ( TYPE => 'STRING',
121 SOURCE => &decline_default,
122 DELIMITERS => [ '<%=', '%>' ],
124 or die $Text::Template::ERROR;
128 ( $locales, $packages, $pops, $init_data ) = signup_info();
129 @payby = @{$init_data->{'payby'}} if @{$init_data->{'payby'}};
130 $packages = $init_data->{agentnum2part_pkg}{$agentnum} if $agentnum;
134 #push @{ $pop{ $_->{state} }->{ $_->{ac} } }, $_;
135 push @{ $pop{ $_->{state} } }, $_;
136 $popnum2pop{$_->{popnum}} = $_;
141 if ( defined $cgi->param('magic') ) {
142 if ( $cgi->param('magic') eq 'process' ) {
144 if ( $cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/ ) {
148 } elsif ( $cgi->param('state') =~ /^(\w*)$/ ) {
150 $cgi->param('county') =~ /^([\w ]*)$/
151 or die "illegal county: ". $cgi->param('county');
153 $cgi->param('country') =~ /^(\w+)$/
154 or die "illegal country: ". $cgi->param('country');
157 die "illegal state: ". $cgi->param('state');
160 $payby = $cgi->param('payby');
161 if ( $payby eq 'CHEK' ) {
162 #$payinfo = join('@', map { $cgi->param( $payby. "_payinfo$_" ) } (1,2) );
163 $payinfo = $cgi->param('CHEK_payinfo1').'@'.$cgi->param('CHEK_payinfo2');
165 $payinfo = $cgi->param( $payby. '_payinfo' );
168 $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' );
169 $payname = $cgi->param( $payby. '_payname' );
170 $paycvv = defined $cgi->param( $payby. '_paycvv' )
171 ? $cgi->param( $payby. '_paycvv' )
174 if ( $invoicing_list = $cgi->param('invoicing_list') ) {
175 $invoicing_list .= ', POST' if $cgi->param('invoicing_list_POST');
177 $invoicing_list = 'POST';
182 $last = $cgi->param('last');
183 $first = $cgi->param('first');
184 $ss = $cgi->param('ss');
185 $company = $cgi->param('company');
186 $address1 = $cgi->param('address1');
187 $address2 = $cgi->param('address2');
188 $city = $cgi->param('city');
191 $zip = $cgi->param('zip');
193 $daytime = $cgi->param('daytime');
194 $night = $cgi->param('night');
195 $fax = $cgi->param('fax');
201 $referral_custnum = $cgi->param('ref');
202 $pkgpart = $cgi->param('pkgpart');
203 $username = $cgi->param('username');
204 $sec_phrase = $cgi->param('sec_phrase');
205 $password = $cgi->param('_password');
206 $popnum = $cgi->param('popnum');
207 #$agentnum, # = $cgi->param('agentnum'),
208 $agentnum ||= $cgi->param('agentnum');
209 $init_popstate = $cgi->param('init_popstate');
210 $refnum = $cgi->param('refnum');
212 if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
213 $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
217 $password2 = $cgi->param('_password2');
219 if ( $payby eq 'CARD' && $cgi->param('CARD_type') ) {
222 $payinfo =~ /^(\d{13,16})$/
223 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
226 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
227 cardtype($payinfo) eq $cgi->param('CARD_type')
228 or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
231 $error ||= new_customer ( {
235 'company' => $company,
236 'address1' => $address1,
237 'address2' => $address2,
242 'country' => $country,
243 'daytime' => $daytime,
247 'payinfo' => $payinfo,
249 'paydate' => $paydate,
250 'payname' => $payname,
251 'invoicing_list' => $invoicing_list,
252 'referral_custnum' => $referral_custnum,
253 'pkgpart' => $pkgpart,
254 'username' => $username,
255 'sec_phrase' => $sec_phrase,
256 '_password' => $password,
258 'agentnum' => $agentnum,
260 map { $_ => $cgi->param($_) } grep { /^snarf_/ } $cgi->param
265 if ( $error eq '_decline' ) {
268 #fudge the snarf info
270 ${$_} = $cgi->param($_) foreach grep { /^snarf_/ } $cgi->param;
277 die "unrecognized magic: ". $cgi->param('magic');
288 $state = $cgi->param('init_popstate') || $init_data->{statedefault};
290 $country = $init_data->{countrydefault};
295 $invoicing_list = '';
306 $referral_custnum = $cgi->param('ref') || '';
307 $init_popstate = $cgi->param('init_popstate') || '';
308 $refnum = $init_data->{'refnum'};
315 $cgi->delete('init_popstate');
316 $self_url = $cgi->self_url;
318 $error = "Error: $error" if $error;
320 print $cgi->header( '-expires' => 'now' ),
321 $signup_template->fill_in();
326 print $cgi->header( '-expires' => 'now' ),
327 $decline_template->fill_in();
331 my $user_agent = new HTTP::BrowserDetect $ENV{HTTP_USER_AGENT};
333 $cgi->param('username') =~ /^(.+)$/
334 or die "fatal: invalid username got past FS::SignupClient::new_customer";
336 $cgi->param('_password') =~ /^(.+)$/
337 or die "fatal: invalid password got past FS::SignupClient::new_customer";
339 ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
340 or die "fatal: invalid email_name got past FS::SignupClient::new_customer";
341 $email_name = $1; #global for template
343 my $pop = $popnum2pop{$cgi->param('popnum')};
344 #or die "fatal: invalid popnum got past FS::SignupClient::new_customer";
346 ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
348 ( $ac, $exch, $loc ) = ( '', '', ''); #presumably you're not using them.
352 $pkg = ( grep { $_->{'pkgpart'} eq $pkgpart } @$packages )[0]->{'pkg'};
354 if ( $ieak_template && $user_agent->windows && $user_agent->ie ) {
356 print $cgi->header('application/x-Internet-signup'),
357 $ieak_template->fill_in();
358 } else { #send a simple confirmation
359 print $cgi->header( '-expires' => 'now' ),
360 $success_template->fill_in();
364 #horrible false laziness with FS/FS/svc_acct_pop.pm::popselector
366 my( $popnum, $state ) = @_;
368 return '<INPUT TYPE="hidden" NAME="popnum" VALUE="">' unless @$pops;
369 return $pops->[0]{city}. ', '. $pops->[0]{state}.
370 ' ('. $pops->[0]{ac}. ')/'. $pops->[0]{exch}. '-'. $pops->[0]{loc}.
371 '<INPUT TYPE="hidden" NAME="popnum" VALUE="'. $pops->[0]{popnum}. '">'
372 if scalar(@$pops) == 1;
375 #push @{ $pop{$_->{state}} }, $_ foreach @$pops;
379 if ( $init_popstate ) {
380 $text .= '<INPUT TYPE="hidden" NAME="init_popstate" VALUE="'.
381 $init_popstate. '">';
385 function opt(what,href,text) {
386 var optionName = new Option(text, href, false, false)
387 var length = what.length;
388 what.options[length] = optionName;
390 function popstate_changed(what) {
391 state = what.options[what.selectedIndex].text;
392 what.form.popnum.options.length = 0;
393 what.form.popnum.options[0] = new Option("", "", false, true);
396 foreach my $popstate ( sort { $a cmp $b } keys %pop ) {
397 $text .= "\nif ( state == \"$popstate\" ) {\n";
399 foreach my $pop ( @{$pop{$popstate}}) {
400 my $o_popnum = $pop->{popnum};
401 my $poptext = $pop->{city}. ', '. $pop->{state}.
402 ' ('. $pop->{ac}. ')/'. $pop->{exch}. '-'. $pop->{loc};
404 $text .= "opt(what.form.popnum, \"$o_popnum\", \"$poptext\");\n"
409 $text .= "}\n</SCRIPT>\n";
412 qq!<SELECT NAME="popstate" SIZE=1 onChange="popstate_changed(this)">!.
414 $text .= "<OPTION>$_" foreach sort { $a cmp $b } keys %pop;
415 $text .= '</SELECT>'; #callback? return 3 html pieces? #'</TD><TD>';
418 $text .= qq!<SELECT NAME="popnum" SIZE=1><OPTION> !;
420 #comment this block to disable initial list population
421 my @initial_select = ();
422 if ( $init_popstate ) {
423 @initial_select = grep { $_->{state} eq $init_popstate } @$pops;
425 if ( scalar( @$pops ) > 100 ) {
426 push @initial_select, $popnum2pop{$popnum} if $popnum2pop{$popnum};
428 @initial_select = @$pops;
431 foreach my $pop ( sort { $a->{state} cmp $b->{state} } @initial_select ) {
432 $text .= qq!<OPTION VALUE="!. $pop->{popnum}. '"'.
433 ( ( $popnum && $pop->{popnum} == $popnum ) ? ' SELECTED' : '' ). ">".
434 $pop->{city}. ', '. $pop->{state}.
435 ' ('. $pop->{ac}. ')/'. $pop->{exch}. '-'. $pop->{loc};
437 $text .= '</SELECT>';
444 my $date = shift || '';
445 my( $m, $y ) = ( 0, 0 );
446 if ( $date =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
447 ( $m, $y ) = ( $2, $1 );
448 } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
449 ( $m, $y ) = ( $1, $3 );
451 my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
453 $return .= "<OPTION";
454 $return .= " SELECTED" if $_ == $m;
457 $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
458 for ( 2001 .. 2037 ) {
459 $return .= "<OPTION";
460 $return .= " SELECTED" if $_ == $y;
463 $return .= "</SELECT>";
468 #false laziness w/FS::cust_main_county
470 my ( $selected_county, $selected_state, $selected_country,
471 $prefix, $onchange ) = @_;
473 $prefix = '' unless defined $prefix;
477 my %cust_main_county;
479 # unless ( @cust_main_county ) { #cache
480 #@cust_main_county = qsearch('cust_main_county', {} );
481 #foreach my $c ( @cust_main_county ) {
482 foreach my $c ( @$locales ) {
483 #$countyflag=1 if $c->county;
484 $countyflag=1 if $c->{county};
485 #push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
486 #$cust_main_county{$c->country}{$c->state}{$c->county} = 1;
487 $cust_main_county{$c->{country}}{$c->{state}}{$c->{county}} = 1;
490 $countyflag=1 if $selected_county;
492 my $script_html = <<END;
494 function opt(what,value,text) {
495 var optionName = new Option(text, value, false, false);
496 var length = what.length;
497 what.options[length] = optionName;
499 function ${prefix}country_changed(what) {
500 country = what.options[what.selectedIndex].text;
501 for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
502 what.form.${prefix}state.options[i] = null;
504 #what.form.${prefix}state.options[0] = new Option('', '', false, true);
506 foreach my $country ( sort keys %cust_main_county ) {
507 $script_html .= "\nif ( country == \"$country\" ) {\n";
508 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
509 my $text = $state || '(n/a)';
510 $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
512 $script_html .= "}\n";
515 $script_html .= <<END;
517 function ${prefix}state_changed(what) {
521 $script_html .= <<END;
522 state = what.options[what.selectedIndex].text;
523 country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
524 for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
525 what.form.${prefix}county.options[i] = null;
528 foreach my $country ( sort keys %cust_main_county ) {
529 $script_html .= "\nif ( country == \"$country\" ) {\n";
530 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
531 $script_html .= "\nif ( state == \"$state\" ) {\n";
532 #foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
533 foreach my $county ( sort keys %{$cust_main_county{$country}{$state}} ) {
534 my $text = $county || '(n/a)';
536 qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
538 $script_html .= "}\n";
540 $script_html .= "}\n";
544 $script_html .= <<END;
549 my $county_html = $script_html;
551 $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange">!;
552 $county_html .= '</SELECT>';
555 qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$selected_county">!;
558 my $state_html = qq!<SELECT NAME="${prefix}state" !.
559 qq!onChange="${prefix}state_changed(this); $onchange">!;
560 foreach my $state ( sort keys %{ $cust_main_county{$selected_country} } ) {
561 my $text = $state || '(n/a)';
562 my $selected = $state eq $selected_state ? 'SELECTED' : '';
563 $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
565 $state_html .= '</SELECT>';
567 $state_html .= '</SELECT>';
569 my $country_html = qq!<SELECT NAME="${prefix}country" !.
570 qq!onChange="${prefix}country_changed(this); $onchange">!;
571 my $countrydefault = $init_data->{countrydefault} || 'US';
572 foreach my $country (
573 sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
574 keys %cust_main_county
576 my $selected = $country eq $selected_country ? ' SELECTED' : '';
577 $country_html .= "\n<OPTION$selected>$country</OPTION>"
579 $country_html .= '</SELECT>';
581 ($county_html, $state_html, $country_html);
585 sub success_default { #html to use if you don't specify a success file
587 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
588 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
589 Thanks for signing up!
591 Signup information for <%= $email_name %>:
593 Username: <%= $username %><BR>
594 Password: <%= $password %><BR>
595 Access number: (<%= $ac %>) / <%= $exch %> - <%= $local %><BR>
596 Package: <%= $pkg %><BR>
601 sub decline_default { #html to use if there is a decline
603 <HTML><HEAD><TITLE>Processing error</TITLE></HEAD>
604 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Processing error</FONT><BR><BR>
605 There has been an error processing your account. Please contact customer