svc_phone signup
[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 ( $svc_x eq 'svc_acct' ) {
169     if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
170       $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
171       $cgi->param('_password', '');
172       $cgi->param('_password2', '');
173     }
174
175     if ( $payby =~ /^(CARD|DCRD)$/ && $cgi->param('CARD_type') ) {
176       my $payinfo = $cgi->param('payinfo');
177       $payinfo =~ s/\D//g;
178
179       $payinfo =~ /^(\d{13,16})$/
180         or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
181       $payinfo = $1;
182       validate($payinfo)
183         or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
184       cardtype($payinfo) eq $cgi->param('CARD_type')
185         or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
186     }
187
188     if ($init_data->{emailinvoiceonly} && (length $cgi->param('invoicing_list') < 1)) {
189         $error ||= $init_data->{msgcat}{illegal_or_empty_text};
190     }
191
192     my $rv = '';
193     unless ( $error ) {
194       $rv = new_customer( {
195         ( map { $_ => scalar($cgi->param($_)) }
196             qw( last first ss company
197                 address1 address2 city county state zip country
198                 daytime night fax stateid stateid_state
199
200                 ship_last ship_first ship_company
201                 ship_address1 ship_address2 ship_city ship_county ship_state
202                   ship_zip ship_country
203                 ship_daytime ship_night ship_fax
204
205                 payby payinfo paycvv paydate payname paystate paytype
206                 invoicing_list referral_custnum promo_code reg_code
207                 pkgpart refnum agentnum
208                 username sec_phrase _password popnum
209                 countrycode phonenum sip_password pin
210               ),
211             grep { /^snarf_/ } $cgi->param
212         ),
213         'payip' => $cgi->remote_host(),
214       } );
215       $error = $rv->{'error'};
216     }
217     #eslaf
218     
219     if ( $error eq '_decline' ) {
220       print_decline();
221     } elsif ( $error ) {
222       #fudge the snarf info
223       no strict 'refs';
224       ${$_} = $cgi->param($_) foreach grep { /^snarf_/ } $cgi->param;
225       print_form();
226     } else {
227       print_okay(
228         'pkgpart' => scalar($cgi->param('pkgpart')),
229         %$rv,
230       );
231     }
232
233 } else {
234   $error = '';
235   print_form;
236 }
237
238 sub print_form {
239
240   $error = "Error: $error" if $error;
241
242   my $r = {
243     $cgi->Vars,
244     %{$init_data},
245     'error' => $error,
246   };
247
248   $r->{pkgpart} ||= $r->{default_pkgpart};
249
250   $r->{referral_custnum} = $r->{'ref'};
251   #$cgi->delete('ref');
252   #$cgi->delete('init_popstate');
253   $r->{self_url} = $cgi->self_url;
254
255   print $cgi->header( '-expires' => 'now' ),
256         $signup_template->fill_in( PACKAGE => 'FS::SelfService::_signupcgi',
257                                    HASH    => $r
258                                  );
259 }
260
261 sub print_decline {
262   print $cgi->header( '-expires' => 'now' ),
263         $decline_template->fill_in();
264 }
265
266 sub print_okay {
267   my %param = @_;
268   my $user_agent = new HTTP::BrowserDetect $ENV{HTTP_USER_AGENT};
269
270   my( $username, $password ) = ( '', '' );
271   my( $countrycode, $phonenum, $sip_password, $pin ) = ( '', '', '', '' );
272
273   my $svc_x = $param{signup_service} || 'svc_acct'; #just in case
274   if ( $svc_x eq 'svc_acct' ) {
275
276     $cgi->param('username') =~ /^(.+)$/
277       or die "fatal: invalid username got past FS::SelfService::new_customer";
278     $username = $1;
279     $cgi->param('_password') =~ /^(.+)$/
280       or die "fatal: invalid password got past FS::SelfService::new_customer";
281     $password = $1;
282
283   } elsif ( $svc_x eq 'svc_phone' ) {
284
285     $countrycode  = $param{countrycode};
286     $phonenum     = $param{phonenum};
287     $sip_password = $param{sip_password};
288     $pin          = $param{pin};
289
290   } else {
291     die "unknown signup service $svc_x";
292   }
293
294   ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
295     or die "fatal: invalid email_name got past FS::SelfService::new_customer";
296   my $email_name = $1; #global for template
297
298   #my %pop = ();
299   my %popnum2pop = ();
300   foreach ( @{ $init_data->{'svc_acct_pop'} } ) {
301     #push @{ $pop{ $_->{state} }->{ $_->{ac} } }, $_;
302     $popnum2pop{$_->{popnum}} = $_;
303   }
304
305   my( $ac, $exch, $loc);
306   my $pop = $popnum2pop{$cgi->param('popnum')};
307     #or die "fatal: invalid popnum got past FS::SelfService::new_customer";
308   if ( $pop ) {
309     ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
310   } else {
311     ( $ac, $exch, $loc ) = ( '', '', ''); #presumably you're not using them.
312   }
313
314   #global for template
315   my $part_pkg = ( grep { $_->{'pkgpart'} eq $param{'pkgpart'} }
316                         @{ $init_data->{'part_pkg'} }
317                  )[0];
318   my $pkg =  $part_pkg->{'pkg'};
319
320   if ( $ieak_template && $user_agent->windows && $user_agent->ie ) {
321
322     #send an IEAK config
323     print $cgi->header('application/x-Internet-signup'),
324           $ieak_template->fill_in();
325
326   } else { #send a simple confirmation
327
328     print $cgi->header( '-expires' => 'now' ),
329           $success_template->fill_in( HASH => {
330
331             email_name     => $email_name,
332             pkg            => $pkg,
333             part_pkg       => \$part_pkg,
334
335             signup_service => $svc_x,
336
337             #for svc_acct
338             username       => $username,
339             password       => $password,
340             _password      => $password,
341             ac             => $ac,   #for dialup POP
342             exch           => $exch, #
343             loc            => $loc,  #
344
345             #for svc_phone
346             countrycode    => $countrycode,
347             phonenum       => $phonenum,
348             sip_password   => $sip_password,
349             pin            => $pin,
350
351           });
352   }
353
354 }
355
356 sub success_default { #html to use if you don't specify a success file
357   <<'END';
358 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
359 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
360 Thanks for signing up!
361 <BR><BR>
362 Signup information for <%= $email_name %>:
363 <BR><BR>
364 Username: <%= $username %><BR>
365 Password: <%= $password %><BR>
366 Access number: (<%= $ac %>) / <%= $exch %> - <%= $local %><BR>
367 Package: <%= $pkg %><BR>
368 </BODY></HTML>
369 END
370 }
371
372 sub decline_default { #html to use if there is a decline
373   <<'END';
374 <HTML><HEAD><TITLE>Processing error</TITLE></HEAD>
375 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Processing error</FONT><BR><BR>
376 There has been an error processing your account.  Please contact customer
377 support.
378 </BODY></HTML>
379 END
380 }
381
382 # subs for the templates...
383
384 package FS::SelfService::_signupcgi;
385 use HTML::Entities;
386 use FS::SelfService qw(regionselector expselect popselector didselector);
387