4f9efff987dcec732f989a2b0d1acf6b5b09254d
[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.52 2004-10-01 01:38:02 ivan Exp $
5
6 use strict;
7 use vars qw( @payby $cgi $locales $packages
8              $pops %pop %popnum2pop
9              $init_data $error
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
14              $agentnum $refnum
15              $ieak_file $ieak_template
16              $signup_html $signup_template
17              $success_html $success_template
18              $decline_html $decline_template
19              $ac $exch $loc
20              $email_name $pkg
21              $self_url
22            );
23 use subs qw( print_form print_okay print_decline
24              success_default decline_default );
25 use CGI;
26 #use CGI::Carp qw(fatalsToBrowser);
27 use Text::Template;
28 use Business::CreditCard;
29 use HTTP::BrowserDetect;
30 use FS::SignupClient 0.03 qw( signup_info new_customer
31                               regionselector expselect popselector);
32
33 #acceptable payment methods
34 #
35 #@payby = qw( CARD BILL COMP );
36 #@payby = qw( CARD BILL );
37 #@payby = qw( CARD );
38 @payby = qw( CARD PREPAY );
39
40 $ieak_file = '/usr/local/freeside/ieak.template';
41 $signup_html = -e 'signup.html'
42                  ? 'signup.html'
43                  : '/usr/local/freeside/signup.html';
44 $success_html = -e 'success.html'
45                   ? 'success.html'
46                   : '/usr/local/freeside/success.html';
47 $decline_html = -e 'decline.html'
48                   ? 'decline.html'
49                   : '/usr/local/freeside/decline.html';
50
51
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
56   $ieak_txt = $1;
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;
61 } else {
62   $ieak_template = '';
63 }
64
65 $agentnum = '';
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
70   $signup_txt = $1;
71   $signup_template = new Text::Template ( TYPE => 'STRING',
72                                           SOURCE => $signup_txt,
73                                           DELIMITERS => [ '<%=', '%>' ]
74                                         )
75     or die $Text::Template::ERROR;
76   if ( $signup_txt =~
77          /<\s*INPUT TYPE="?hidden"?\s+NAME="?agentnum"?\s+VALUE="?(\d+)"?\s*>/si
78   ) {
79     $agentnum = $1;
80   }
81 } else {
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 => [ '<%=', '%>' ]
87   #                                      )
88   #  or die $Text::Template::ERROR;
89 }
90
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
95   $success_txt = $1;
96   $success_template = new Text::Template ( TYPE => 'STRING',
97                                            SOURCE => $success_txt,
98                                            DELIMITERS => [ '<%=', '%>' ],
99                                          )
100     or die $Text::Template::ERROR;
101 } else {
102   $success_template = new Text::Template ( TYPE => 'STRING',
103                                            SOURCE => &success_default,
104                                            DELIMITERS => [ '<%=', '%>' ],
105                                          )
106     or die $Text::Template::ERROR;
107 }
108
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
113   $decline_txt = $1;
114   $decline_template = new Text::Template ( TYPE => 'STRING',
115                                            SOURCE => $decline_txt,
116                                            DELIMITERS => [ '<%=', '%>' ],
117                                          )
118     or die $Text::Template::ERROR;
119 } else {
120   $decline_template = new Text::Template ( TYPE => 'STRING',
121                                            SOURCE => &decline_default,
122                                            DELIMITERS => [ '<%=', '%>' ],
123                                          )
124     or die $Text::Template::ERROR;
125 }
126
127
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;
131 %pop = ();
132 %popnum2pop = ();
133 foreach (@$pops) {
134   push @{ $pop{ $_->{state} }->{ $_->{ac} } }, $_;
135   $popnum2pop{$_->{popnum}} = $_;
136 }
137
138 $cgi = new CGI;
139
140 if ( defined $cgi->param('magic') ) {
141   if ( $cgi->param('magic') eq 'process' ) {
142
143     if ( $cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/ ) {
144       $state = $1;
145       $county = $3 || '';
146       $country = $4;
147     } elsif ( $cgi->param('state') =~ /^(\w*)$/ ) {
148       $state = $1;
149       $cgi->param('county') =~ /^([\w ]*)$/
150         or die "illegal county: ". $cgi->param('county');
151       $county = $1;
152       $cgi->param('country') =~ /^(\w+)$/
153         or die "illegal country: ". $cgi->param('country');
154       $country = $1;
155     } else {
156       die "illegal state: ". $cgi->param('state');
157     }
158
159     $payby = $cgi->param('payby');
160     if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
161       #$payinfo = join('@', map { $cgi->param( $payby. "_payinfo$_" ) } (1,2) );
162       $payinfo = $cgi->param($payby. '_payinfo1'). '@'. 
163                  $cgi->param($payby. '_payinfo2');
164     } else {
165       $payinfo = $cgi->param( $payby. '_payinfo' );
166     }
167     $paydate =
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' )
172                 : '';
173
174     if ( $invoicing_list = $cgi->param('invoicing_list') ) {
175       $invoicing_list .= ', POST' if $cgi->param('invoicing_list_POST');
176     } else {
177       $invoicing_list = 'POST';
178     }
179
180     $error = '';
181
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');
189     #$county,
190     #$state,
191     $zip              = $cgi->param('zip');
192     #$country,
193     $daytime          = $cgi->param('daytime');
194     $night            = $cgi->param('night');
195     $fax              = $cgi->param('fax');
196     #$payby,
197     #$payinfo,
198     #$paydate,
199     #$payname,
200     #$invoicing_list,
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');
211
212     if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
213       $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
214       $password  = '';
215       $password2 = '';
216     } else {
217       $password2 = $cgi->param('_password2');
218
219       if ( $payby =~ /^(CARD|DCRD)$/ && $cgi->param('CARD_type') ) {
220         $payinfo =~ s/\D//g;
221
222         $payinfo =~ /^(\d{13,16})$/
223           or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
224         $payinfo = $1;
225         validate($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');
229       }
230
231       $error ||= new_customer ( {
232         'last'             => $last,
233         'first'            => $first,
234         'ss'               => $ss,
235         'company'          => $company,
236         'address1'         => $address1,
237         'address2'         => $address2,
238         'city'             => $city,
239         'county'           => $county,
240         'state'            => $state,
241         'zip'              => $zip,
242         'country'          => $country,
243         'daytime'          => $daytime,
244         'night'            => $night,
245         'fax'              => $fax,
246         'payby'            => $payby,
247         'payinfo'          => $payinfo,
248         'paycvv'           => $paycvv,
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,
257         'popnum'           => $popnum,
258         'agentnum'         => $agentnum,
259         'refnum'           => $refnum,
260         map { $_ => $cgi->param($_) } grep { /^snarf_/ } $cgi->param
261       } );
262
263     }
264     
265     if ( $error eq '_decline' ) {
266       print_decline();
267     } elsif ( $error ) {
268       #fudge the snarf info
269       no strict 'refs';
270       ${$_} = $cgi->param($_) foreach grep { /^snarf_/ } $cgi->param;
271       print_form();
272     } else {
273       print_okay();
274     }
275
276   } else {
277     die "unrecognized magic: ". $cgi->param('magic');
278   }
279 } else {
280   $error = '';
281   $last = '';
282   $first = '';
283   $ss = '';
284   $company = '';
285   $address1 = '';
286   $address2 = '';
287   $city = '';
288   $state = $init_data->{statedefault};
289   $county = '';
290   $country = $init_data->{countrydefault};
291   $zip = '';
292   $daytime = '';
293   $night = '';
294   $fax = '';
295   $invoicing_list = '';
296   $payby = '';
297   $payinfo = '';
298   $paydate = '';
299   $payname = '';
300   $pkgpart = '';
301   $username = '';
302   $password = '';
303   $password2 = '';
304   $sec_phrase = '';
305   $popnum = '';
306   $referral_custnum = $cgi->param('ref') || '';
307   $init_popstate = $cgi->param('init_popstate') || '';
308   $refnum = $init_data->{'refnum'};
309   print_form;
310 }
311
312 sub print_form {
313
314   $cgi->delete('ref');
315   $cgi->delete('init_popstate');
316   $self_url = $cgi->self_url;
317
318   $error = "Error: $error" if $error;
319
320   print $cgi->header( '-expires' => 'now' ),
321         $signup_template->fill_in();
322
323 }
324
325 sub print_decline {
326   print $cgi->header( '-expires' => 'now' ),
327         $decline_template->fill_in();
328 }
329
330 sub print_okay {
331   my $user_agent = new HTTP::BrowserDetect $ENV{HTTP_USER_AGENT};
332
333   $cgi->param('username') =~ /^(.+)$/
334     or die "fatal: invalid username got past FS::SignupClient::new_customer";
335   my $username = $1;
336   $cgi->param('_password') =~ /^(.+)$/
337     or die "fatal: invalid password got past FS::SignupClient::new_customer";
338   my $password = $1;
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
342
343   my $pop = $popnum2pop{$cgi->param('popnum')};
344     #or die "fatal: invalid popnum got past FS::SignupClient::new_customer";
345   if ( $pop ) {
346     ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
347   } else {
348     ( $ac, $exch, $loc ) = ( '', '', ''); #presumably you're not using them.
349   }
350
351   #global for template
352   $pkg = ( grep { $_->{'pkgpart'} eq $pkgpart } @$packages )[0]->{'pkg'};
353
354   if ( $ieak_template && $user_agent->windows && $user_agent->ie ) {
355     #send an IEAK config
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();
361   }
362 }
363
364 sub success_default { #html to use if you don't specify a success file
365   <<'END';
366 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
367 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
368 Thanks for signing up!
369 <BR><BR>
370 Signup information for <%= $email_name %>:
371 <BR><BR>
372 Username: <%= $username %><BR>
373 Password: <%= $password %><BR>
374 Access number: (<%= $ac %>) / <%= $exch %> - <%= $local %><BR>
375 Package: <%= $pkg %><BR>
376 </BODY></HTML>
377 END
378 }
379
380 sub decline_default { #html to use if there is a decline
381   <<'END';
382 <HTML><HEAD><TITLE>Processing error</TITLE></HEAD>
383 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Processing error</FONT><BR><BR>
384 There has been an error processing your account.  Please contact customer
385 support.
386 </BODY></HTML>
387 END
388 }
389