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