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