self-service: make payment UI done
[freeside.git] / fs_selfservice / FS-SelfService / cgi / selfservice.cgi
1 #!/usr/bin/perl -Tw
2
3 use strict;
4 use vars qw($cgi $session_id $form_max $template_dir);
5 use subs qw(do_template);
6 use CGI;
7 use CGI::Carp qw(fatalsToBrowser);
8 use Text::Template;
9 use FS::SelfService qw(login customer_info invoice payment_info);
10
11 $template_dir = '.';
12
13 $form_max = 255;
14
15 $cgi = new CGI;
16
17 unless ( defined $cgi->param('session') ) {
18   do_template('login',{});
19   exit;
20 }
21
22 if ( $cgi->param('session') eq 'login' ) {
23
24   $cgi->param('username') =~ /^\s*([a-z0-9_\-\.\&]{0,$form_max})\s*$/i
25     or die "illegal username";
26   my $username = $1;
27
28   $cgi->param('domain') =~ /^\s*([\w\-\.]{0,$form_max})\s*$/
29     or die "illegal domain";
30   my $domain = $1;
31
32   $cgi->param('password') =~ /^(.{0,$form_max})$/
33     or die "illegal password";
34   my $password = $1;
35
36   my $rv = login(
37     'username' => $username,
38     'domain'   => $domain,
39     'password' => $password,
40   );
41   if ( $rv->{error} ) {
42     do_template('login', {
43       'error'    => $rv->{error},
44       'username' => $username,
45       'domain'   => $domain,
46     } );
47     exit;
48   } else {
49     $cgi->param('session' => $rv->{session_id} );
50     $cgi->param('action'  => 'myaccount' );
51   }
52 }
53
54 $session_id = $cgi->param('session');
55
56 $cgi->param('action') =~ /^(myaccount|view_invoice|make_payment)$/
57   or die "unknown action ". $cgi->param('action');
58 my $action = $1;
59
60 my $result = eval "&$action();";
61 die $@ if $@;
62
63 if ( $result->{error} eq "Can't resume session" ) { #ick
64   do_template('login',{});
65   exit;
66 }
67
68 #warn $result->{'open_invoices'};
69 #warn scalar(@{$result->{'open_invoices'}});
70
71 warn "processing template $action\n";
72 do_template($action, {
73   'session_id' => $session_id,
74   %{$result}
75 });
76
77 #--
78
79 sub myaccount { customer_info( 'session_id' => $session_id ); }
80
81 sub view_invoice {
82
83   $cgi->param('invnum') =~ /^(\d+)$/ or die "illegal invnum";
84   my $invnum = $1;
85
86   invoice( 'session_id' => $session_id,
87            'invnum'     => $invnum,
88          );
89
90 }
91
92 sub make_payment {
93   payment_info( 'session_id' => $session_id );
94 }
95
96 #--
97
98 sub do_template {
99   my $name = shift;
100   my $fill_in = shift;
101
102   $cgi->delete_all();
103   $fill_in->{'selfurl'} = $cgi->self_url;
104
105   my $template = new Text::Template( TYPE    => 'FILE',
106                                      SOURCE  => "$template_dir/$name.html",
107                                      DELIMITERS => [ '<%=', '%>' ],
108                                      UNTAINT => 1,                    )
109     or die $Text::Template::ERROR;
110
111   print $cgi->header( '-expires' => 'now' ),
112         $template->fill_in( HASH => $fill_in );
113 }
114