3 # $Id: signup.cgi,v 1.50 2004-01-04 03:52:54 ivan Exp $
6 use vars qw( @payby $cgi $locales $packages
9 $last $first $ss $company $address1 $address2 $city $state $county
10 $country $zip $daytime $night $fax $invoicing_list $payby $payinfo
11 $paycvv $paydate $payname $referral_custnum $init_popstate
12 $pkgpart $username $password $password2 $sec_phrase $popnum
14 $ieak_file $ieak_template
15 $signup_html $signup_template
16 $success_html $success_template
17 $decline_html $decline_template
22 use subs qw( print_form print_okay print_decline
23 success_default decline_default
26 #use CGI::Carp qw(fatalsToBrowser);
28 use Business::CreditCard;
29 use HTTP::BrowserDetect;
30 use FS::SignupClient 0.03 qw( signup_info new_customer );
32 #acceptable payment methods
34 #@payby = qw( CARD BILL COMP );
35 #@payby = qw( CARD BILL );
37 @payby = qw( CARD PREPAY );
39 $ieak_file = '/usr/local/freeside/ieak.template';
40 $signup_html = -e 'signup.html'
42 : '/usr/local/freeside/signup.html';
43 $success_html = -e 'success.html'
45 : '/usr/local/freeside/success.html';
46 $decline_html = -e 'decline.html'
48 : '/usr/local/freeside/decline.html';
51 if ( -e $ieak_file ) {
52 my $ieak_txt = Text::Template::_load_text($ieak_file)
53 or die $Text::Template::ERROR;
54 $ieak_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
56 $ieak_txt =~ s/\r//g; # don't double \r on old templates
57 $ieak_txt =~ s/\n/\r\n/g;
58 $ieak_template = new Text::Template ( TYPE => 'STRING', SOURCE => $ieak_txt )
59 or die $Text::Template::ERROR;
65 if ( -e $signup_html ) {
66 my $signup_txt = Text::Template::_load_text($signup_html)
67 or die $Text::Template::ERROR;
68 $signup_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
70 $signup_template = new Text::Template ( TYPE => 'STRING',
71 SOURCE => $signup_txt,
72 DELIMITERS => [ '<%=', '%>' ]
74 or die $Text::Template::ERROR;
76 /<\s*INPUT TYPE="?hidden"?\s+NAME="?agentnum"?\s+VALUE="?(\d+)"?\s*>/si
81 #too much maintenance hassle to keep in this file
82 die "can't find ./signup.html or /usr/local/freeside/signup.html";
83 #$signup_template = new Text::Template ( TYPE => 'STRING',
84 # SOURCE => &signup_default,
85 # DELIMITERS => [ '<%=', '%>' ]
87 # or die $Text::Template::ERROR;
90 if ( -e $success_html ) {
91 my $success_txt = Text::Template::_load_text($success_html)
92 or die $Text::Template::ERROR;
93 $success_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
95 $success_template = new Text::Template ( TYPE => 'STRING',
96 SOURCE => $success_txt,
97 DELIMITERS => [ '<%=', '%>' ],
99 or die $Text::Template::ERROR;
101 $success_template = new Text::Template ( TYPE => 'STRING',
102 SOURCE => &success_default,
103 DELIMITERS => [ '<%=', '%>' ],
105 or die $Text::Template::ERROR;
108 if ( -e $decline_html ) {
109 my $decline_txt = Text::Template::_load_text($decline_html)
110 or die $Text::Template::ERROR;
111 $decline_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
113 $decline_template = new Text::Template ( TYPE => 'STRING',
114 SOURCE => $decline_txt,
115 DELIMITERS => [ '<%=', '%>' ],
117 or die $Text::Template::ERROR;
119 $decline_template = new Text::Template ( TYPE => 'STRING',
120 SOURCE => &decline_default,
121 DELIMITERS => [ '<%=', '%>' ],
123 or die $Text::Template::ERROR;
127 ( $locales, $packages, $pops, $init_data ) = signup_info();
128 @payby = @{$init_data->{'payby'}} if @{$init_data->{'payby'}};
129 $packages = $init_data->{agentnum2part_pkg}{$agentnum} if $agentnum;
133 push @{ $pop{ $_->{state} }->{ $_->{ac} } }, $_;
134 $popnum2pop{$_->{popnum}} = $_;
139 if ( defined $cgi->param('magic') ) {
140 if ( $cgi->param('magic') eq 'process' ) {
142 if ( $cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/ ) {
146 } elsif ( $cgi->param('state') =~ /^(\w*)$/ ) {
148 $cgi->param('county') =~ /^([\w ]*)$/
149 or die "illegal county: ". $cgi->param('county');
151 $cgi->param('country') =~ /^(\w+)$/
152 or die "illegal country: ". $cgi->param('country');
155 die "illegal state: ". $cgi->param('state');
158 $payby = $cgi->param('payby');
159 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
160 #$payinfo = join('@', map { $cgi->param( $payby. "_payinfo$_" ) } (1,2) );
161 $payinfo = $cgi->param($payby. '_payinfo1'). '@'.
162 $cgi->param($payby. '_payinfo2');
164 $payinfo = $cgi->param( $payby. '_payinfo' );
167 $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' );
168 $payname = $cgi->param( $payby. '_payname' );
169 $paycvv = defined $cgi->param( $payby. '_paycvv' )
170 ? $cgi->param( $payby. '_paycvv' )
173 if ( $invoicing_list = $cgi->param('invoicing_list') ) {
174 $invoicing_list .= ', POST' if $cgi->param('invoicing_list_POST');
176 $invoicing_list = 'POST';
181 $last = $cgi->param('last');
182 $first = $cgi->param('first');
183 $ss = $cgi->param('ss');
184 $company = $cgi->param('company');
185 $address1 = $cgi->param('address1');
186 $address2 = $cgi->param('address2');
187 $city = $cgi->param('city');
190 $zip = $cgi->param('zip');
192 $daytime = $cgi->param('daytime');
193 $night = $cgi->param('night');
194 $fax = $cgi->param('fax');
200 $referral_custnum = $cgi->param('ref');
201 $pkgpart = $cgi->param('pkgpart');
202 $username = $cgi->param('username');
203 $sec_phrase = $cgi->param('sec_phrase');
204 $password = $cgi->param('_password');
205 $popnum = $cgi->param('popnum');
206 #$agentnum, # = $cgi->param('agentnum'),
207 $agentnum ||= $cgi->param('agentnum');
208 $init_popstate = $cgi->param('init_popstate');
209 $refnum = $cgi->param('refnum');
211 if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
212 $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
216 $password2 = $cgi->param('_password2');
218 if ( $payby =~ /^(CARD|DCRD)$/ && $cgi->param('CARD_type') ) {
221 $payinfo =~ /^(\d{13,16})$/
222 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
225 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
226 cardtype($payinfo) eq $cgi->param('CARD_type')
227 or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
230 $error ||= new_customer ( {
234 'company' => $company,
235 'address1' => $address1,
236 'address2' => $address2,
241 'country' => $country,
242 'daytime' => $daytime,
246 'payinfo' => $payinfo,
248 'paydate' => $paydate,
249 'payname' => $payname,
250 'invoicing_list' => $invoicing_list,
251 'referral_custnum' => $referral_custnum,
252 'pkgpart' => $pkgpart,
253 'username' => $username,
254 'sec_phrase' => $sec_phrase,
255 '_password' => $password,
257 'agentnum' => $agentnum,
259 map { $_ => $cgi->param($_) } grep { /^snarf_/ } $cgi->param
264 if ( $error eq '_decline' ) {
267 #fudge the snarf info
269 ${$_} = $cgi->param($_) foreach grep { /^snarf_/ } $cgi->param;
276 die "unrecognized magic: ". $cgi->param('magic');
287 $state = $init_data->{statedefault};
289 $country = $init_data->{countrydefault};
294 $invoicing_list = '';
305 $referral_custnum = $cgi->param('ref') || '';
306 $init_popstate = $cgi->param('init_popstate') || '';
307 $refnum = $init_data->{'refnum'};
314 $cgi->delete('init_popstate');
315 $self_url = $cgi->self_url;
317 $error = "Error: $error" if $error;
319 print $cgi->header( '-expires' => 'now' ),
320 $signup_template->fill_in();
325 print $cgi->header( '-expires' => 'now' ),
326 $decline_template->fill_in();
330 my $user_agent = new HTTP::BrowserDetect $ENV{HTTP_USER_AGENT};
332 $cgi->param('username') =~ /^(.+)$/
333 or die "fatal: invalid username got past FS::SignupClient::new_customer";
335 $cgi->param('_password') =~ /^(.+)$/
336 or die "fatal: invalid password got past FS::SignupClient::new_customer";
338 ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
339 or die "fatal: invalid email_name got past FS::SignupClient::new_customer";
340 $email_name = $1; #global for template
342 my $pop = $popnum2pop{$cgi->param('popnum')};
343 #or die "fatal: invalid popnum got past FS::SignupClient::new_customer";
345 ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
347 ( $ac, $exch, $loc ) = ( '', '', ''); #presumably you're not using them.
351 $pkg = ( grep { $_->{'pkgpart'} eq $pkgpart } @$packages )[0]->{'pkg'};
353 if ( $ieak_template && $user_agent->windows && $user_agent->ie ) {
355 print $cgi->header('application/x-Internet-signup'),
356 $ieak_template->fill_in();
357 } else { #send a simple confirmation
358 print $cgi->header( '-expires' => 'now' ),
359 $success_template->fill_in();
363 #horrible false laziness with FS/FS/svc_acct_pop.pm::popselector
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 #my %popnum2pop = ();
377 # push @{ $pop{ $_->{state} }->{ $_->{ac} } }, $_;
378 # $popnum2pop{$_->{popnum}} = $_;
383 function opt(what,href,text) {
384 var optionName = new Option(text, href, false, false)
385 var length = what.length;
386 what.options[length] = optionName;
390 if ( $init_popstate ) {
391 $text .= '<INPUT TYPE="hidden" NAME="init_popstate" VALUE="'.
392 $init_popstate. '">';
395 function acstate_changed(what) {
396 state = what.options[what.selectedIndex].text;
397 what.form.popac.options.length = 0
398 what.form.popac.options[0] = new Option("Area code", "-1", false, true);
402 my @states = $init_popstate ? ( $init_popstate ) : keys %pop;
403 foreach my $state ( sort { $a cmp $b } @states ) {
404 $text .= "\nif ( state == \"$state\" ) {\n" unless $init_popstate;
406 foreach my $ac ( sort { $a cmp $b } keys %{ $pop{$state} }) {
407 $text .= "opt(what.form.popac, \"$ac\", \"$ac\");\n";
408 if ($ac eq $cgi->param('popac')) {
409 $text .= "what.form.popac.options[what.form.popac.length-1].selected = true;\n";
412 $text .= "}\n" unless $init_popstate;
414 $text .= "popac_changed(what.form.popac)}\n";
417 function popac_changed(what) {
418 ac = what.options[what.selectedIndex].text;
419 what.form.popnum.options.length = 0;
420 what.form.popnum.options[0] = new Option("City", "-1", false, true);
424 foreach my $state ( @states ) {
425 foreach my $popac ( keys %{ $pop{$state} } ) {
426 $text .= "\nif ( ac == \"$popac\" ) {\n";
428 foreach my $pop ( @{$pop{$state}->{$popac}}) {
429 my $o_popnum = $pop->{popnum};
430 my $poptext = $pop->{city}. ', '. $pop->{state}.
431 ' ('. $pop->{ac}. ')/'. $pop->{exch}. '-'. $pop->{loc};
433 $text .= "opt(what.form.popnum, \"$o_popnum\", \"$poptext\");\n";
434 if ($popnum == $o_popnum) {
435 $text .= "what.form.popnum.options[what.form.popnum.length-1].selected = true;\n";
443 $text .= "}\n</SCRIPT>\n";
446 qq!<TABLE CELLPADDING="0"><TR><TD><SELECT NAME="acstate"! .
447 qq!SIZE=1 onChange="acstate_changed(this)"><OPTION VALUE=-1>State!;
448 $text .= "<OPTION" . ($_ eq $cgi->param('acstate') ? " SELECTED" : "") .
449 ">$_" foreach sort { $a cmp $b } @states;
450 $text .= '</SELECT>'; #callback? return 3 html pieces? #'</TD>';
453 qq!<SELECT NAME="popac" SIZE=1 onChange="popac_changed(this)">!.
454 qq!<OPTION>Area code</SELECT></TR><TR VALIGN="top">!;
456 $text .= qq!<TR><TD><SELECT NAME="popnum" SIZE=1 STYLE="width: 20em"><OPTION>City!;
459 #comment this block to disable initial list polulation
460 my @initial_select = ();
461 if ( scalar( @$pops ) > 100 ) {
462 push @initial_select, $popnum2pop{$popnum} if $popnum2pop{$popnum};
464 @initial_select = @$pops;
466 foreach my $pop ( sort { $a->{state} cmp $b->{state} } @initial_select ) {
467 $text .= qq!<OPTION VALUE="!. $pop->{popnum}. '"'.
468 ( ( $popnum && $pop->{popnum} == $popnum ) ? ' SELECTED' : '' ). ">".
469 $pop->{city}. ', '. $pop->{state}.
470 ' ('. $pop->{ac}. ')/'. $pop->{exch}. '-'. $pop->{loc};
473 $text .= qq!</SELECT></TD></TR></TABLE>!;
481 my $date = shift || '';
482 my( $m, $y ) = ( 0, 0 );
483 if ( $date =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
484 ( $m, $y ) = ( $2, $1 );
485 } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
486 ( $m, $y ) = ( $1, $3 );
488 my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
490 $return .= "<OPTION";
491 $return .= " SELECTED" if $_ == $m;
494 $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
495 for ( 2001 .. 2037 ) {
496 $return .= "<OPTION";
497 $return .= " SELECTED" if $_ == $y;
500 $return .= "</SELECT>";
505 #false laziness w/FS::cust_main_county
507 my ( $selected_county, $selected_state, $selected_country,
508 $prefix, $onchange ) = @_;
510 $prefix = '' unless defined $prefix;
514 my %cust_main_county;
516 # unless ( @cust_main_county ) { #cache
517 #@cust_main_county = qsearch('cust_main_county', {} );
518 #foreach my $c ( @cust_main_county ) {
519 foreach my $c ( @$locales ) {
520 #$countyflag=1 if $c->county;
521 $countyflag=1 if $c->{county};
522 #push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
523 #$cust_main_county{$c->country}{$c->state}{$c->county} = 1;
524 $cust_main_county{$c->{country}}{$c->{state}}{$c->{county}} = 1;
527 $countyflag=1 if $selected_county;
529 my $script_html = <<END;
531 function opt(what,value,text) {
532 var optionName = new Option(text, value, false, false);
533 var length = what.length;
534 what.options[length] = optionName;
536 function ${prefix}country_changed(what) {
537 country = what.options[what.selectedIndex].text;
538 for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
539 what.form.${prefix}state.options[i] = null;
541 #what.form.${prefix}state.options[0] = new Option('', '', false, true);
543 foreach my $country ( sort keys %cust_main_county ) {
544 $script_html .= "\nif ( country == \"$country\" ) {\n";
545 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
546 my $text = $state || '(n/a)';
547 $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
549 $script_html .= "}\n";
552 $script_html .= <<END;
554 function ${prefix}state_changed(what) {
558 $script_html .= <<END;
559 state = what.options[what.selectedIndex].text;
560 country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
561 for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
562 what.form.${prefix}county.options[i] = null;
565 foreach my $country ( sort keys %cust_main_county ) {
566 $script_html .= "\nif ( country == \"$country\" ) {\n";
567 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
568 $script_html .= "\nif ( state == \"$state\" ) {\n";
569 #foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
570 foreach my $county ( sort keys %{$cust_main_county{$country}{$state}} ) {
571 my $text = $county || '(n/a)';
573 qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
575 $script_html .= "}\n";
577 $script_html .= "}\n";
581 $script_html .= <<END;
586 my $county_html = $script_html;
588 $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange">!;
589 $county_html .= '</SELECT>';
592 qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$selected_county">!;
595 my $state_html = qq!<SELECT NAME="${prefix}state" !.
596 qq!onChange="${prefix}state_changed(this); $onchange">!;
597 foreach my $state ( sort keys %{ $cust_main_county{$selected_country} } ) {
598 my $text = $state || '(n/a)';
599 my $selected = $state eq $selected_state ? 'SELECTED' : '';
600 $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
602 $state_html .= '</SELECT>';
604 $state_html .= '</SELECT>';
606 my $country_html = qq!<SELECT NAME="${prefix}country" !.
607 qq!onChange="${prefix}country_changed(this); $onchange">!;
608 my $countrydefault = $init_data->{countrydefault} || 'US';
609 foreach my $country (
610 sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
611 keys %cust_main_county
613 my $selected = $country eq $selected_country ? ' SELECTED' : '';
614 $country_html .= "\n<OPTION$selected>$country</OPTION>"
616 $country_html .= '</SELECT>';
618 ($county_html, $state_html, $country_html);
622 sub success_default { #html to use if you don't specify a success file
624 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
625 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
626 Thanks for signing up!
628 Signup information for <%= $email_name %>:
630 Username: <%= $username %><BR>
631 Password: <%= $password %><BR>
632 Access number: (<%= $ac %>) / <%= $exch %> - <%= $local %><BR>
633 Package: <%= $pkg %><BR>
638 sub decline_default { #html to use if there is a decline
640 <HTML><HEAD><TITLE>Processing error</TITLE></HEAD>
641 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Processing error</FONT><BR><BR>
642 There has been an error processing your account. Please contact customer