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