4 use vars qw($DEBUG $cgi $session_id $form_max $template_dir);
5 use subs qw(do_template);
7 use CGI::Carp qw(fatalsToBrowser);
11 use Number::Format 1.50;
12 use FS::SelfService qw(
13 access_info login_info login customer_info edit_info invoice
14 payment_info process_payment realtime_collect process_prepay
15 list_pkgs order_pkg signup_info order_recharge
16 part_svc_info provision_acct provision_external
17 unprovision_svc change_pkg domainselector
18 list_svcs list_svc_usage list_cdr_usage list_support_usage
30 unless ( defined $cgi->param('session') ) {
31 my $login_info = login_info();
33 do_template('login', $login_info );
37 if ( $cgi->param('session') eq 'login' ) {
39 $cgi->param('username') =~ /^\s*([a-z0-9_\-\.\&]{0,$form_max})\s*$/i
40 or die "illegal username";
43 $cgi->param('domain') =~ /^\s*([\w\-\.]{0,$form_max})\s*$/
44 or die "illegal domain";
47 $cgi->param('password') =~ /^(.{0,$form_max})$/
48 or die "illegal password";
52 'username' => $username,
54 'password' => $password,
57 my $login_info = login_info();
58 do_template('login', {
59 'error' => $rv->{error},
60 'username' => $username,
66 $cgi->param('session' => $rv->{session_id} );
67 $cgi->param('action' => 'myaccount' );
71 $session_id = $cgi->param('session');
73 #order|pw_list XXX ???
74 $cgi->param('action') =~
75 /^(myaccount|view_invoice|make_payment|make_ach_payment|make_thirdparty_payment|payment_results|ach_payment_results|recharge_prepay|recharge_results|logout|change_bill|change_ship|change_pay|process_change_bill|process_change_ship|process_change_pay|customer_order_pkg|process_order_pkg|customer_change_pkg|process_change_pkg|process_order_recharge|provision|provision_svc|process_svc_acct|process_svc_external|delete_svc|view_usage|view_usage_details|view_cdr_details|view_support_details|change_password|process_change_password)$/
76 or die "unknown action ". $cgi->param('action');
79 warn "calling $action sub\n"
81 $FS::SelfService::DEBUG = $DEBUG;
82 my $result = eval "&$action();";
85 if ( $result->{error} eq "Can't resume session"
86 || $result->{error} eq "Expired session" ) { #ick
88 my $login_info = login_info();
89 do_template('login', $login_info);
93 #warn $result->{'open_invoices'};
94 #warn scalar(@{$result->{'open_invoices'}});
96 warn "processing template $action\n"
98 do_template($action, {
99 'session_id' => $session_id,
100 'action' => $action, #so the menu knows what tab we're on...
101 #%{ payment_info( 'session_id' => $session_id ) }, # cust_paybys for the menu
107 sub myaccount { customer_info( 'session_id' => $session_id ); }
109 sub change_bill { my $payment_info =
110 payment_info( 'session_id' => $session_id );
111 return $payment_info if ( $payment_info->{'error'} );
113 customer_info( 'session_id' => $session_id );
119 sub change_ship { change_bill(@_); }
120 sub change_pay { change_bill(@_); }
122 sub _process_change_info {
123 my ($erroraction, @fields) = @_;
127 $results ||= edit_info (
128 'session_id' => $session_id,
129 map { ($_ => $cgi->param($_)) } grep { defined($cgi->param($_)) } @fields,
133 if ( $results->{'error'} ) {
135 $action = $erroraction;
139 'error' => '<FONT COLOR="#FF0000">'. $results->{'error'}. '</FONT>',
146 sub process_change_bill {
147 _process_change_info( 'change_bill',
148 qw( first last company address1 address2 city state
149 county zip country daytime night fax )
153 sub process_change_ship {
154 my @list = map { "ship_$_" }
155 qw( first last company address1 address2 city state
156 county zip country daytime night fax
158 if ($cgi->param('same') eq 'Y') {
159 foreach (@list) { $cgi->param($_, '') }
162 _process_change_info( 'change_ship', @list );
165 sub process_change_pay {
166 my $postal = $cgi->param( 'postal_invoicing' );
168 qw( payby payinfo payinfo1 payinfo2 month year payname
169 address1 address2 city county state zip country auto paytype
170 paystate ss stateid stateid_state invoicing_list
172 push @list, 'postal_invoicing' if $postal;
173 unless ( $postal || $cgi->param( 'invoicing_list' ) ) {
174 $action = 'change_pay';
178 'error' => '<FONT COLOR="#FF0000">Postal or email required.</FONT>',
181 _process_change_info( 'change_pay', @list );
186 $cgi->param('invnum') =~ /^(\d+)$/ or die "illegal invnum";
189 invoice( 'session_id' => $session_id,
195 sub customer_order_pkg {
196 my $init_data = signup_info( 'customer_session_id' => $session_id );
197 return $init_data if ( $init_data->{'error'} );
199 my $customer_info = customer_info( 'session_id' => $session_id );
200 return $customer_info if ( $customer_info->{'error'} );
203 ( map { $_ => $init_data->{$_} }
204 qw( part_pkg security_phrase svc_acct_pop ),
210 sub customer_change_pkg {
211 my $init_data = signup_info( 'customer_session_id' => $session_id );
212 return $init_data if ( $init_data->{'error'} );
214 my $customer_info = customer_info( 'session_id' => $session_id );
215 return $customer_info if ( $customer_info->{'error'} );
218 ( map { $_ => $init_data->{$_} }
219 qw( part_pkg security_phrase svc_acct_pop ),
221 ( map { $_ => $cgi->param($_) }
228 sub process_order_pkg {
232 unless ( length($cgi->param('_password')) ) {
233 my $init_data = signup_info( 'customer_session_id' => $session_id );
234 $results = { 'error' => $init_data->{msgcat}{empty_password} };
235 $results = { 'error' => $init_data->{error} } if($init_data->{error});
237 if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
238 my $init_data = signup_info( 'customer_session_id' => $session_id );
239 $results = { 'error' => $init_data->{msgcat}{passwords_dont_match} };
240 $results = { 'error' => $init_data->{error} } if($init_data->{error});
241 $cgi->param('_password', '');
242 $cgi->param('_password2', '');
245 $results ||= order_pkg (
246 'session_id' => $session_id,
247 map { $_ => $cgi->param($_) }
248 qw( custnum pkgpart username _password _password2 sec_phrase popnum )
252 if ( $results->{'error'} ) {
253 $action = 'customer_order_pkg';
256 %{customer_order_pkg()},
257 'error' => '<FONT COLOR="#FF0000">'. $results->{'error'}. '</FONT>',
265 sub process_change_pkg {
269 $results ||= change_pkg (
270 'session_id' => $session_id,
271 map { $_ => $cgi->param($_) }
276 if ( $results->{'error'} ) {
277 $action = 'customer_change_pkg';
280 %{customer_change_pkg()},
281 'error' => '<FONT COLOR="#FF0000">'. $results->{'error'}. '</FONT>',
289 sub process_order_recharge {
293 $results ||= order_recharge (
294 'session_id' => $session_id,
295 map { $_ => $cgi->param($_) }
300 if ( $results->{'error'} ) {
301 $action = 'view_usage';
302 if ($results->{'error'} eq '_decline') {
303 $results->{'error'} = "There has been an error processing your account. Please contact customer support."
308 'error' => '<FONT COLOR="#FF0000">'. $results->{'error'}. '</FONT>',
317 payment_info( 'session_id' => $session_id );
320 sub payment_results {
322 use Business::CreditCard;
324 #we should only do basic checking here for DoS attacks and things
325 #that couldn't be constructed by the web form... let process_payment() do
326 #the rest, it gives better error messages
328 $cgi->param('amount') =~ /^\s*(\d+(\.\d{2})?)\s*$/
329 or die "Illegal amount: ". $cgi->param('amount'); #!!!
332 my $payinfo = $cgi->param('payinfo');
334 $payinfo =~ /^(\d{13,16})$/
335 #or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
336 or die "illegal card"; #!!!
339 #or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
340 or die "invalid card"; #!!!
342 if ( $cgi->param('card_type') ) {
343 cardtype($payinfo) eq $cgi->param('card_type')
344 #or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
345 or die "not a ". $cgi->param('card_type');
348 $cgi->param('paycvv') =~ /^\s*(.{0,4})\s*$/ or die "illegal CVV2";
351 $cgi->param('month') =~ /^(\d{2})$/ or die "illegal month";
353 $cgi->param('year') =~ /^(\d{4})$/ or die "illegal year";
356 $cgi->param('payname') =~ /^(.{0,80})$/ or die "illegal payname";
359 $cgi->param('address1') =~ /^(.{0,80})$/ or die "illegal address1";
362 $cgi->param('address2') =~ /^(.{0,80})$/ or die "illegal address2";
365 $cgi->param('city') =~ /^(.{0,80})$/ or die "illegal city";
368 $cgi->param('state') =~ /^(.{0,80})$/ or die "illegal state";
371 $cgi->param('zip') =~ /^(.{0,10})$/ or die "illegal zip";
374 $cgi->param('country') =~ /^(.{0,2})$/ or die "illegal country";
378 $save = 1 if $cgi->param('save');
381 $auto = 1 if $cgi->param('auto');
383 $cgi->param('paybatch') =~ /^([\w\-\.]+)$/ or die "illegal paybatch";
387 'session_id' => $session_id,
390 'payinfo' => $payinfo,
394 'payname' => $payname,
395 'address1' => $address1,
396 'address2' => $address2,
400 'country' => $country,
403 'paybatch' => $paybatch,
408 sub make_ach_payment {
409 payment_info( 'session_id' => $session_id );
412 sub ach_payment_results {
414 #we should only do basic checking here for DoS attacks and things
415 #that couldn't be constructed by the web form... let process_payment() do
416 #the rest, it gives better error messages
418 $cgi->param('amount') =~ /^\s*(\d+(\.\d{2})?)\s*$/
419 or die "illegal amount"; #!!!
422 my $payinfo1 = $cgi->param('payinfo1');
423 $payinfo1=~ /^(\d+)$/
424 or die "illegal account"; #!!!
427 my $payinfo2 = $cgi->param('payinfo2');
428 $payinfo2=~ /^(\d+)$/
429 or die "illegal ABA/routing code"; #!!!
432 $cgi->param('payname') =~ /^(.{0,80})$/ or die "illegal payname";
435 $cgi->param('paystate') =~ /^(.{0,2})$/ or die "illegal paystate";
438 $cgi->param('paytype') =~ /^(.{0,80})$/ or die "illegal paytype";
441 $cgi->param('ss') =~ /^(.{0,80})$/ or die "illegal ss";
444 $cgi->param('stateid') =~ /^(.{0,80})$/ or die "illegal stateid";
447 $cgi->param('stateid_state') =~ /^(.{0,2})$/ or die "illegal stateid_state";
448 my $stateid_state = $1;
451 $save = 1 if $cgi->param('save');
454 $auto = 1 if $cgi->param('auto');
456 $cgi->param('paybatch') =~ /^([\w\-\.]+)$/ or die "illegal paybatch";
460 'session_id' => $session_id,
463 'payinfo1' => $payinfo1,
464 'payinfo2' => $payinfo2,
467 'payname' => $payname,
468 'paytype' => $paytype,
469 'paystate' => $paystate,
471 'stateid' => $stateid,
472 'stateid_state' => $stateid_state,
475 'paybatch' => $paybatch,
480 sub make_thirdparty_payment {
481 $cgi->param('payby_method') =~ /^(CC|ECHECK)$/
482 or die "illegal payby method";
483 realtime_collect( 'session_id' => $session_id, 'method' => $1 );
486 sub recharge_prepay {
487 customer_info( 'session_id' => $session_id );
490 sub recharge_results {
492 my $prepaid_cardnum = $cgi->param('prepaid_cardnum');
493 $prepaid_cardnum =~ s/\W//g;
494 $prepaid_cardnum =~ /^(\w*)$/ or die "illegal prepaid card number";
495 $prepaid_cardnum = $1;
497 process_prepay ( 'session_id' => $session_id,
498 'prepaid_cardnum' => $prepaid_cardnum,
503 FS::SelfService::logout( 'session_id' => $session_id );
507 my $result = list_pkgs( 'session_id' => $session_id );
508 die $result->{'error'} if exists $result->{'error'} && $result->{'error'};
514 my $result = part_svc_info(
515 'session_id' => $session_id,
516 map { $_ => $cgi->param($_) } qw( pkgnum svcpart ),
518 die $result->{'error'} if exists $result->{'error'} && $result->{'error'};
520 $result->{'svcdb'} =~ /^svc_(.*)$/
521 #or return { 'error' => 'Unknown svcdb '. $result->{'svcdb'} };
522 or die 'Unknown svcdb '. $result->{'svcdb'};
528 sub process_svc_acct {
530 my $result = provision_acct (
531 'session_id' => $session_id,
532 map { $_ => $cgi->param($_) } qw(
533 pkgnum svcpart username domsvc _password _password2 sec_phrase popnum )
536 if ( exists $result->{'error'} && $result->{'error'} ) {
537 #warn "$result $result->{'error'}";
538 $action = 'provision_svc_acct';
541 %{ part_svc_info( 'session_id' => $session_id,
542 map { $_ => $cgi->param($_) } qw( pkgnum svcpart )
545 'error' => $result->{'error'},
548 #warn "$result $result->{'error'}";
554 sub process_svc_external {
556 'session_id' => $session_id,
557 map { $_ => $cgi->param($_) } qw( pkgnum svcpart )
563 'session_id' => $session_id,
564 'svcnum' => $cgi->param('svcnum'),
570 'session_id' => $session_id,
571 'svcdb' => [ 'svc_acct', 'svc_phone' ],
576 sub view_usage_details {
578 'session_id' => $session_id,
579 'svcnum' => $cgi->param('svcnum'),
580 'beginning' => $cgi->param('beginning') || '',
581 'ending' => $cgi->param('ending') || '',
585 sub view_cdr_details {
587 'session_id' => $session_id,
588 'svcnum' => $cgi->param('svcnum'),
589 'beginning' => $cgi->param('beginning') || '',
590 'ending' => $cgi->param('ending') || '',
594 sub view_support_details {
596 'session_id' => $session_id,
597 'svcnum' => $cgi->param('svcnum'),
598 'beginning' => $cgi->param('beginning') || '',
599 'ending' => $cgi->param('ending') || '',
603 sub change_password {
605 'session_id' => $session_id,
606 'svcdb' => 'svc_acct',
610 sub process_change_password {
612 my $result = myaccount_passwd(
613 'session_id' => $session_id,
614 map { $_ => $cgi->param($_) } qw( svcnum new_password new_password2 )
617 if ( exists $result->{'error'} && $result->{'error'} ) {
619 $action = 'change_password';
622 %{ list_svcs( 'session_id' => $session_id,
623 'svcdb' => 'svc_acct',
626 #'svcnum' => $cgi->param('svcnum'),
627 'error' => $result->{'error'}
645 $fill_in->{'selfurl'} = $cgi->self_url;
646 $fill_in->{'cgi'} = \$cgi;
648 my $access_info = $session_id
649 ? access_info( 'session_id' => $session_id )
651 $fill_in->{$_} = $access_info->{$_} foreach keys %$access_info;
653 my $source = "$template_dir/$name.html";
654 #warn "creating template for $source\n";
655 my $template = new Text::Template( TYPE => 'FILE',
657 DELIMITERS => [ '<%=', '%>' ],
660 or die $Text::Template::ERROR;
662 #warn "filling in $template with $fill_in\n";
663 print $cgi->header( '-expires' => 'now' ),
664 $template->fill_in( PACKAGE => 'FS::SelfService::_selfservicecgi',
669 #*FS::SelfService::_selfservicecgi::include = \&Text::Template::fill_in_file;
671 package FS::SelfService::_selfservicecgi;
673 #use FS::SelfService qw(regionselector expselect popselector);
675 use FS::SelfService qw(regionselector popselector domainselector location_form);
677 #false laziness w/agent.cgi
680 my $template = new Text::Template( TYPE => 'FILE',
681 SOURCE => "$main::template_dir/$name.html",
682 DELIMITERS => [ '<%=', '%>' ],
685 or die $Text::Template::ERROR;
687 $template->fill_in( PACKAGE => 'FS::SelfService::_selfservicecgi',