merge webpay support in with autoselection of old realtime_bop and realtime_refund_bop
[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                 countrycode phonenum sip_password pin
233               ),
234             grep { /^snarf_/ } $cgi->param
235         ),
236         'payip' => $cgi->remote_host(),
237       } );
238       $error = $rv->{'error'};
239     }
240     #eslaf
241     
242     if ( $error eq '_decline' ) {
243       print_decline();
244     } elsif ( $error eq '_collect' ) {
245       map { $cgi->param($_, $rv->{$_}) }
246         qw( popup_url reference collectitems amount );
247       print_collect();
248     } elsif ( $error ) {
249       #fudge the snarf info
250       no strict 'refs';
251       ${$_} = $cgi->param($_) foreach grep { /^snarf_/ } $cgi->param;
252       print_form();
253     } else {
254       print_okay(
255         'pkgpart' => scalar($cgi->param('pkgpart')),
256         %$rv,
257       );
258     }
259
260 } elsif ( $magic eq 'success' || $action eq 'success' ) {
261
262   $cgi->param('username', 'username');  #hmmm temp kludge
263   $cgi->param('_password', 'password');
264   print_okay( map { /^([\w ]+)$/ ? ( $_ => $1 ) : () } $cgi->param ); #hmmm
265
266 } elsif ( $magic eq 'decline' || $action eq 'decline' ) {
267
268   print_decline();
269
270 } else {
271   $error = '';
272   print_form;
273 }
274
275 sub print_form {
276
277   $error = "Error: $error" if $error;
278
279   my $r = {
280     $cgi->Vars,
281     %{$init_data},
282     'error' => $error,
283   };
284
285   $r->{pkgpart} ||= $r->{default_pkgpart};
286
287   $r->{referral_custnum} = $r->{'ref'};
288   #$cgi->delete('ref');
289   #$cgi->delete('init_popstate');
290   $r->{self_url} = $cgi->self_url;
291
292   print $cgi->header( '-expires' => 'now' ),
293         $signup_template->fill_in( PACKAGE => 'FS::SelfService::_signupcgi',
294                                    HASH    => $r
295                                  );
296 }
297
298 sub print_collect {
299
300   $error = "Error: $error" if $error;
301
302   my $r = {
303     $cgi->Vars,
304     %{$init_data},
305     'error' => $error,
306   };
307
308   $r->{pkgpart} ||= $r->{default_pkgpart};
309
310   $r->{referral_custnum} = $r->{'ref'};
311   $r->{self_url} = $cgi->self_url;
312
313   print $cgi->header( '-expires' => 'now' ),
314         $collect_template->fill_in( PACKAGE => 'FS::SelfService::_signupcgi',
315                                     HASH    => $r
316                                   );
317 }
318
319 sub print_decline {
320   print $cgi->header( '-expires' => 'now' ),
321         $decline_template->fill_in();
322 }
323
324 sub print_okay {
325   my %param = @_;
326   my $user_agent = new HTTP::BrowserDetect $ENV{HTTP_USER_AGENT};
327
328   my( $username, $password ) = ( '', '' );
329   my( $countrycode, $phonenum, $sip_password, $pin ) = ( '', '', '', '' );
330
331   my $svc_x = $param{signup_service} || 'svc_acct'; #just in case
332   if ( $svc_x eq 'svc_acct' ) {
333
334     $cgi->param('username') =~ /^(.+)$/
335       or die "fatal: invalid username got past FS::SelfService::new_customer";
336     $username = $1;
337     $cgi->param('_password') =~ /^(.+)$/
338       or die "fatal: invalid password got past FS::SelfService::new_customer";
339     $password = $1;
340
341   } elsif ( $svc_x eq 'svc_phone' ) {
342
343     $countrycode  = $param{countrycode};
344     $phonenum     = $param{phonenum};
345     $sip_password = $param{sip_password};
346     $pin          = $param{pin};
347
348   } else {
349     die "unknown signup service $svc_x";
350   }
351
352   ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
353     or die "fatal: invalid email_name got past FS::SelfService::new_customer";
354   my $email_name = $1; #global for template
355
356   #my %pop = ();
357   my %popnum2pop = ();
358   foreach ( @{ $init_data->{'svc_acct_pop'} } ) {
359     #push @{ $pop{ $_->{state} }->{ $_->{ac} } }, $_;
360     $popnum2pop{$_->{popnum}} = $_;
361   }
362
363   my( $ac, $exch, $loc);
364   my $pop = $popnum2pop{$cgi->param('popnum')};
365     #or die "fatal: invalid popnum got past FS::SelfService::new_customer";
366   if ( $pop ) {
367     ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
368   } else {
369     ( $ac, $exch, $loc ) = ( '', '', ''); #presumably you're not using them.
370   }
371
372   #global for template
373   my $part_pkg = ( grep { $_->{'pkgpart'} eq $param{'pkgpart'} }
374                         @{ $init_data->{'part_pkg'} }
375                  )[0];
376   my $pkg =  $part_pkg->{'pkg'};
377
378   if ( $ieak_template && $user_agent->windows && $user_agent->ie ) {
379
380     #send an IEAK config
381     print $cgi->header('application/x-Internet-signup'),
382           $ieak_template->fill_in();
383
384   } else { #send a simple confirmation
385
386     print $cgi->header( '-expires' => 'now' ),
387           $success_template->fill_in( HASH => {
388
389             email_name     => $email_name,
390             pkg            => $pkg,
391             part_pkg       => \$part_pkg,
392
393             signup_service => $svc_x,
394
395             #for svc_acct
396             username       => $username,
397             password       => $password,
398             _password      => $password,
399             ac             => $ac,   #for dialup POP
400             exch           => $exch, #
401             loc            => $loc,  #
402
403             #for svc_phone
404             countrycode    => $countrycode,
405             phonenum       => $phonenum,
406             sip_password   => $sip_password,
407             pin            => $pin,
408
409           });
410   }
411
412 }
413
414 sub success_default { #html to use if you don't specify a success file
415   <<'END';
416 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
417 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
418 Thanks for signing up!
419 <BR><BR>
420 Signup information for <%= $email_name %>:
421 <BR><BR>
422 Username: <%= $username %><BR>
423 Password: <%= $password %><BR>
424 Access number: (<%= $ac %>) / <%= $exch %> - <%= $local %><BR>
425 Package: <%= $pkg %><BR>
426 </BODY></HTML>
427 END
428 }
429
430 sub collect_default { #html to use if there is a collect phase
431   <<'END';
432 <HTML><HEAD><TITLE>Pay now</TITLE></HEAD>
433 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Pay now</FONT><BR><BR>
434 <SCRIPT TYPE="text/javascript">
435   function popcollect() {
436     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' );
437     return false;
438   }
439 </SCRIPT>
440 <SCRIPT TYPE="text/javascript" SRC="overlibmws.js"></SCRIPT>
441 <SCRIPT TYPE="text/javascript" SRC="overlibmws_iframe.js"></SCRIPT>
442 <SCRIPT TYPE="text/javascript" SRC="overlibmws_draggable.js"></SCRIPT>
443 <SCRIPT TYPE="text/javascript" SRC="overlibmws_crossframe.js"></SCRIPT>
444 <SCRIPT TYPE="text/javascript" SRC="iframecontentmws.js"></SCRIPT>
445 You are about to contact our payment processor to pay <%= $amount %> for
446 <%= $pkg %>.<BR><BR>
447 Your transaction reference number is <%= $reference %><BR><BR>
448 <FORM NAME="collect_popper" method="post" action="javascript:void(0)" onSubmit="popcollect()">
449 <%=
450   my %itemhash = @collectitems;
451   foreach my $input (keys %itemhash) {
452     $OUT .= qq!<INPUT NAME="$input" TYPE="hidden" VALUE="$itemhash{$input}">!;
453   }
454 %>
455 <INPUT NAME="submit" type="submit" value="Pay now">
456 </FORM>
457 </BODY></HTML>
458 END
459 }
460
461 sub decline_default { #html to use if there is a decline
462   <<'END';
463 <HTML><HEAD><TITLE>Processing error</TITLE></HEAD>
464 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Processing error</FONT><BR><BR>
465 There has been an error processing your account.  Please contact customer
466 support.
467 </BODY></HTML>
468 END
469 }
470
471 # subs for the templates...
472
473 package FS::SelfService::_signupcgi;
474 use HTML::Entities;
475 use FS::SelfService qw(regionselector expselect popselector didselector);
476