nomadix, RT#5876
[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              $collect_html $collect_template
12              $decline_html $decline_template
13            );
14
15 use subs qw( print_form print_okay print_decline
16              success_default collect_default decline_default
17            );
18 use CGI;
19 #use CGI::Carp qw(fatalsToBrowser);
20 use Text::Template;
21 use Business::CreditCard;
22 use HTTP::BrowserDetect;
23 use FS::SelfService qw( signup_info new_customer );
24
25 #acceptable payment methods
26 #
27 #@payby = qw( CARD BILL COMP );
28 #@payby = qw( CARD BILL );
29 #@payby = qw( CARD );
30 @payby = qw( CARD PREPAY );
31
32 $ieak_file = '/usr/local/freeside/ieak.template';
33 $signup_html = -e 'signup.html'
34                  ? 'signup.html'
35                  : '/usr/local/freeside/signup.html';
36 $success_html = -e 'success.html'
37                   ? 'success.html'
38                   : '/usr/local/freeside/success.html';
39 $collect_html = -e 'collect.html'
40                   ? 'collect.html'
41                   : '/usr/local/freeside/collect.html';
42 $decline_html = -e 'decline.html'
43                   ? 'decline.html'
44                   : '/usr/local/freeside/decline.html';
45
46
47 if ( -e $ieak_file ) {
48   my $ieak_txt = Text::Template::_load_text($ieak_file)
49     or die $Text::Template::ERROR;
50   $ieak_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
51   $ieak_txt = $1;
52   $ieak_txt =~ s/\r//g; # don't double \r on old templates
53   $ieak_txt =~ s/\n/\r\n/g;
54   $ieak_template = new Text::Template ( TYPE => 'STRING', SOURCE => $ieak_txt )
55     or die $Text::Template::ERROR;
56 } else {
57   $ieak_template = '';
58 }
59
60 $agentnum = '';
61 if ( -e $signup_html ) {
62   my $signup_txt = Text::Template::_load_text($signup_html)
63     or die $Text::Template::ERROR;
64   $signup_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
65   $signup_txt = $1;
66   $signup_template = new Text::Template ( TYPE => 'STRING',
67                                           SOURCE => $signup_txt,
68                                           DELIMITERS => [ '<%=', '%>' ]
69                                         )
70     or die $Text::Template::ERROR;
71   if ( $signup_txt =~
72          /<\s*INPUT TYPE="?hidden"?\s+NAME="?agentnum"?\s+VALUE="?(\d+)"?\s*\/?\s*>/si
73   ) {
74     $agentnum = $1;
75   }
76 } else {
77   #too much maintenance hassle to keep in this file
78   die "can't find ./signup.html or /usr/local/freeside/signup.html";
79   #$signup_template = new Text::Template ( TYPE => 'STRING',
80   #                                        SOURCE => &signup_default,
81   #                                        DELIMITERS => [ '<%=', '%>' ]
82   #                                      )
83   #  or die $Text::Template::ERROR;
84 }
85
86 if ( -e $success_html ) {
87   my $success_txt = Text::Template::_load_text($success_html)
88     or die $Text::Template::ERROR;
89   $success_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
90   $success_txt = $1;
91   $success_template = new Text::Template ( TYPE => 'STRING',
92                                            SOURCE => $success_txt,
93                                            DELIMITERS => [ '<%=', '%>' ],
94                                          )
95     or die $Text::Template::ERROR;
96 } else {
97   $success_template = new Text::Template ( TYPE => 'STRING',
98                                            SOURCE => &success_default,
99                                            DELIMITERS => [ '<%=', '%>' ],
100                                          )
101     or die $Text::Template::ERROR;
102 }
103
104 if ( -e $collect_html ) {
105   my $collect_txt = Text::Template::_load_text($collect_html)
106     or die $Text::Template::ERROR;
107   $collect_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
108   $collect_txt = $1;
109   $collect_template = new Text::Template ( TYPE => 'STRING',
110                                            SOURCE => $collect_txt,
111                                            DELIMITERS => [ '<%=', '%>' ],
112                                          )
113     or die $Text::Template::ERROR;
114 } else {
115   $collect_template = new Text::Template ( TYPE => 'STRING',
116                                            SOURCE => &collect_default,
117                                            DELIMITERS => [ '<%=', '%>' ],
118                                          )
119     or die $Text::Template::ERROR;
120 }
121
122 if ( -e $decline_html ) {
123   my $decline_txt = Text::Template::_load_text($decline_html)
124     or die $Text::Template::ERROR;
125   $decline_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
126   $decline_txt = $1;
127   $decline_template = new Text::Template ( TYPE => 'STRING',
128                                            SOURCE => $decline_txt,
129                                            DELIMITERS => [ '<%=', '%>' ],
130                                          )
131     or die $Text::Template::ERROR;
132 } else {
133   $decline_template = new Text::Template ( TYPE => 'STRING',
134                                            SOURCE => &decline_default,
135                                            DELIMITERS => [ '<%=', '%>' ],
136                                          )
137     or die $Text::Template::ERROR;
138 }
139
140 $cgi = new CGI;
141
142 $init_data = signup_info( 'agentnum'   => $agentnum,
143                           'promo_code' => scalar($cgi->param('promo_code')),
144                           'reg_code'   => uc(scalar($cgi->param('reg_code'))),
145                         );
146
147 my $magic  = $cgi->param('magic') || '';
148 my $action = $cgi->param('action') || '';
149
150 if ( $magic eq 'process' || $action eq 'process_signup' ) {
151
152     $error = '';
153
154     $cgi->param('agentnum', $agentnum) if $agentnum;
155     $cgi->param('reg_code', uc(scalar($cgi->param('reg_code'))) );
156
157     #false laziness w/agent.cgi, identical except for agentnum
158     my $payby = $cgi->param('payby');
159     if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
160       #$payinfo = join('@', map { $cgi->param( $payby. "_payinfo$_" ) } (1,2) );
161       $cgi->param('payinfo' => $cgi->param($payby. '_payinfo1'). '@'. 
162                                $cgi->param($payby. '_payinfo2')
163                  );
164     } else {
165       $cgi->param('payinfo' => $cgi->param( $payby. '_payinfo' ) );
166     }
167     $cgi->param('paydate' => $cgi->param( $payby. '_month' ). '-'.
168                              $cgi->param( $payby. '_year' )
169                );
170     $cgi->param('payname' => $cgi->param( $payby. '_payname' ) );
171     $cgi->param('paycvv' => defined $cgi->param( $payby. '_paycvv' )
172                               ? $cgi->param( $payby. '_paycvv' )
173                               : ''
174                );
175     $cgi->param('paytype' => defined $cgi->param( $payby. '_paytype' )
176                               ? $cgi->param( $payby. '_paytype' )
177                               : ''
178                );
179     $cgi->param('paystate' => defined $cgi->param( $payby. '_paystate' )
180                               ? $cgi->param( $payby. '_paystate' )
181                               : ''
182                );
183
184     if ( $cgi->param('invoicing_list') ) {
185       $cgi->param('invoicing_list' => $cgi->param('invoicing_list'). ', POST')
186         if $cgi->param('invoicing_list_POST');
187     } else {
188       $cgi->param('invoicing_list' => 'POST' );
189     }
190
191     #if ( $svc_x eq 'svc_acct' ) {
192     if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
193       $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
194       $cgi->param('_password', '');
195       $cgi->param('_password2', '');
196     }
197
198     if ( $payby =~ /^(CARD|DCRD)$/ && $cgi->param('CARD_type') ) {
199       my $payinfo = $cgi->param('payinfo');
200       $payinfo =~ s/\D//g;
201
202       $payinfo =~ /^(\d{13,16})$/
203         or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
204       $payinfo = $1;
205       validate($payinfo)
206         or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
207       cardtype($payinfo) eq $cgi->param('CARD_type')
208         or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
209     }
210
211     if ($init_data->{emailinvoiceonly} && (length $cgi->param('invoicing_list') < 1)) {
212         $error ||= $init_data->{msgcat}{illegal_or_empty_text};
213     }
214
215     my $rv = '';
216     unless ( $error ) {
217       $rv = new_customer( {
218         ( map { $_ => scalar($cgi->param($_)) }
219             qw( last first ss company
220                 address1 address2 city county state zip country
221                 daytime night fax stateid stateid_state
222
223                 ship_last ship_first ship_company
224                 ship_address1 ship_address2 ship_city ship_county ship_state
225                   ship_zip ship_country
226                 ship_daytime ship_night ship_fax
227
228                 payby payinfo paycvv paydate payname paystate paytype
229                 invoicing_list referral_custnum promo_code reg_code
230                 pkgpart refnum agentnum
231                 username sec_phrase _password popnum
232                 mac_addr
233                 countrycode phonenum sip_password pin
234               ),
235             grep { /^snarf_/ } $cgi->param
236         ),
237         'payip' => $cgi->remote_host(),
238       } );
239       $error = $rv->{'error'};
240     }
241     #eslaf
242     
243     if ( $error eq '_decline' ) {
244       print_decline();
245     } elsif ( $error eq '_collect' ) {
246       map { $cgi->param($_, $rv->{$_}) }
247         qw( popup_url reference collectitems amount );
248       print_collect();
249     } elsif ( $error ) {
250       #fudge the snarf info
251       no strict 'refs';
252       ${$_} = $cgi->param($_) foreach grep { /^snarf_/ } $cgi->param;
253       print_form();
254     } else {
255       print_okay(
256         'pkgpart' => scalar($cgi->param('pkgpart')),
257         %$rv,
258       );
259     }
260
261 } elsif ( $magic eq 'success' || $action eq 'success' ) {
262
263   $cgi->param('username', 'username');  #hmmm temp kludge
264   $cgi->param('_password', 'password');
265   print_okay( map { /^([\w ]+)$/ ? ( $_ => $1 ) : () } $cgi->param ); #hmmm
266
267 } elsif ( $magic eq 'decline' || $action eq 'decline' ) {
268
269   print_decline();
270
271 } else {
272   $error = '';
273   print_form;
274 }
275
276 sub print_form {
277
278   $error = "Error: $error" if $error;
279
280   my $r = {
281     $cgi->Vars,
282     %{$init_data},
283     'error' => $error,
284   };
285
286   $r->{pkgpart} ||= $r->{default_pkgpart};
287
288   $r->{referral_custnum} = $r->{'ref'};
289   #$cgi->delete('ref');
290   #$cgi->delete('init_popstate');
291   $r->{self_url} = $cgi->self_url;
292
293   print $cgi->header( '-expires' => 'now' ),
294         $signup_template->fill_in( PACKAGE => 'FS::SelfService::_signupcgi',
295                                    HASH    => $r
296                                  );
297 }
298
299 sub print_collect {
300
301   $error = "Error: $error" if $error;
302
303   my $r = {
304     $cgi->Vars,
305     %{$init_data},
306     'error' => $error,
307   };
308
309   $r->{pkgpart} ||= $r->{default_pkgpart};
310
311   $r->{referral_custnum} = $r->{'ref'};
312   $r->{self_url} = $cgi->self_url;
313
314   print $cgi->header( '-expires' => 'now' ),
315         $collect_template->fill_in( PACKAGE => 'FS::SelfService::_signupcgi',
316                                     HASH    => $r
317                                   );
318 }
319
320 sub print_decline {
321   print $cgi->header( '-expires' => 'now' ),
322         $decline_template->fill_in();
323 }
324
325 sub print_okay {
326   my %param = @_;
327   my $user_agent = new HTTP::BrowserDetect $ENV{HTTP_USER_AGENT};
328
329   my( $username, $password ) = ( '', '' );
330   my( $countrycode, $phonenum, $sip_password, $pin ) = ( '', '', '', '' );
331
332   my $svc_x = $param{signup_service} || 'svc_acct'; #just in case
333   if ( $svc_x eq 'svc_acct' ) {
334
335     $cgi->param('username') =~ /^(.+)$/
336       or die "fatal: invalid username got past FS::SelfService::new_customer";
337     $username = $1;
338     $cgi->param('_password') =~ /^(.+)$/
339       or die "fatal: invalid password got past FS::SelfService::new_customer";
340     $password = $1;
341
342   } elsif ( $svc_x eq 'svc_phone' ) {
343
344     $countrycode  = $param{countrycode};
345     $phonenum     = $param{phonenum};
346     $sip_password = $param{sip_password};
347     $pin          = $param{pin};
348
349   } else {
350     die "unknown signup service $svc_x";
351   }
352
353   ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
354     or die "fatal: invalid email_name got past FS::SelfService::new_customer";
355   my $email_name = $1; #global for template
356
357   #my %pop = ();
358   my %popnum2pop = ();
359   foreach ( @{ $init_data->{'svc_acct_pop'} } ) {
360     #push @{ $pop{ $_->{state} }->{ $_->{ac} } }, $_;
361     $popnum2pop{$_->{popnum}} = $_;
362   }
363
364   my( $ac, $exch, $loc);
365   my $pop = $popnum2pop{$cgi->param('popnum')};
366     #or die "fatal: invalid popnum got past FS::SelfService::new_customer";
367   if ( $pop ) {
368     ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
369   } else {
370     ( $ac, $exch, $loc ) = ( '', '', ''); #presumably you're not using them.
371   }
372
373   #global for template
374   my $part_pkg = ( grep { $_->{'pkgpart'} eq $param{'pkgpart'} }
375                         @{ $init_data->{'part_pkg'} }
376                  )[0];
377   my $pkg =  $part_pkg->{'pkg'};
378
379   if ( $ieak_template && $user_agent->windows && $user_agent->ie ) {
380
381     #send an IEAK config
382     print $cgi->header('application/x-Internet-signup'),
383           $ieak_template->fill_in();
384
385   } else { #send a simple confirmation
386
387     print $cgi->header( '-expires' => 'now' ),
388           $success_template->fill_in( HASH => {
389
390             email_name     => $email_name,
391             pkg            => $pkg,
392             part_pkg       => \$part_pkg,
393
394             signup_service => $svc_x,
395
396             #for svc_acct
397             username       => $username,
398             password       => $password,
399             _password      => $password,
400             ac             => $ac,   #for dialup POP
401             exch           => $exch, #
402             loc            => $loc,  #
403
404             #for svc_phone
405             countrycode    => $countrycode,
406             phonenum       => $phonenum,
407             sip_password   => $sip_password,
408             pin            => $pin,
409
410           });
411   }
412
413 }
414
415 sub success_default { #html to use if you don't specify a success file
416   <<'END';
417 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
418 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
419 Thanks for signing up!
420 <BR><BR>
421 Signup information for <%= $email_name %>:
422 <BR><BR>
423 Username: <%= $username %><BR>
424 Password: <%= $password %><BR>
425 Access number: (<%= $ac %>) / <%= $exch %> - <%= $local %><BR>
426 Package: <%= $pkg %><BR>
427 </BODY></HTML>
428 END
429 }
430
431 sub collect_default { #html to use if there is a collect phase
432   <<'END';
433 <HTML><HEAD><TITLE>Pay now</TITLE></HEAD>
434 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Pay now</FONT><BR><BR>
435 <SCRIPT TYPE="text/javascript">
436   function popcollect() {
437     overlib( OLiframeContent('<%= $popup_url %>', 336, 550, 'Secure Payment Area', 0, 'auto' ), CAPTION, 'Pay now', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK, BGCOLOR, '#333399', CGCOLOR, '#333399', CLOSETEXT, 'Close' );
438     return false;
439   }
440 </SCRIPT>
441 <SCRIPT TYPE="text/javascript" SRC="overlibmws.js"></SCRIPT>
442 <SCRIPT TYPE="text/javascript" SRC="overlibmws_iframe.js"></SCRIPT>
443 <SCRIPT TYPE="text/javascript" SRC="overlibmws_draggable.js"></SCRIPT>
444 <SCRIPT TYPE="text/javascript" SRC="overlibmws_crossframe.js"></SCRIPT>
445 <SCRIPT TYPE="text/javascript" SRC="iframecontentmws.js"></SCRIPT>
446 You are about to contact our payment processor to pay <%= $amount %> for
447 <%= $pkg %>.<BR><BR>
448 Your transaction reference number is <%= $reference %><BR><BR>
449 <FORM NAME="collect_popper" method="post" action="javascript:void(0)" onSubmit="popcollect()">
450 <%=
451   my %itemhash = @collectitems;
452   foreach my $input (keys %itemhash) {
453     $OUT .= qq!<INPUT NAME="$input" TYPE="hidden" VALUE="$itemhash{$input}">!;
454   }
455 %>
456 <INPUT NAME="submit" type="submit" value="Pay now">
457 </FORM>
458 </BODY></HTML>
459 END
460 }
461
462 sub decline_default { #html to use if there is a decline
463   <<'END';
464 <HTML><HEAD><TITLE>Processing error</TITLE></HEAD>
465 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Processing error</FONT><BR><BR>
466 There has been an error processing your account.  Please contact customer
467 support.
468 </BODY></HTML>
469 END
470 }
471
472 # subs for the templates...
473
474 package FS::SelfService::_signupcgi;
475 use HTML::Entities;
476 use FS::SelfService qw(regionselector expselect popselector didselector);
477