selfservice payment fees, RT#18345
[freeside.git] / FS / FS / ClientAPI / MasonComponent.pm
1 package FS::ClientAPI::MasonComponent;
2
3 use strict;
4 use vars qw( $cache $DEBUG $me );
5 use subs qw( _cache );
6 use FS::Mason qw( mason_interps );
7 use FS::Conf;
8 use FS::ClientAPI_SessionCache;
9 use FS::Record qw( qsearch qsearchs );
10 use FS::cust_main;
11 use FS::part_pkg;
12
13 $DEBUG = 0;
14 $me = '[FS::ClientAPI::MasonComponent]';
15
16 my %allowed_comps = map { $_=>1 } qw(
17   /elements/select-did.html
18   /misc/areacodes.cgi
19   /misc/exchanges.cgi
20   /misc/phonenums.cgi
21   /misc/states.cgi
22   /misc/counties.cgi
23   /misc/svc_acct-domains.cgi
24   /misc/part_svc-columns.cgi
25 );
26
27 my %session_comps = map { $_=>1 } qw(
28   /elements/location.html
29   /elements/tr-amount_fee.html
30   /edit/cust_main/first_pkg/select-part_pkg.html
31 );
32
33 my %session_callbacks = (
34
35   '/elements/location.html' => sub {
36     my( $custnum, $argsref ) = @_;
37     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
38       or return "unknown custnum $custnum";
39     my %args = @$argsref;
40     $args{object} = $cust_main->bill_location;
41     @$argsref = ( %args );
42     return ''; #no error
43   },
44
45   '/elements/tr-amount_fee.html' => sub {
46     my( $custnum, $argsref ) = @_;
47
48     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
49       or return "unknown custnum $custnum";
50
51     my $conf = new FS::Conf;
52
53     my %args = @$argsref;
54     %args = (
55       %args,
56       'process-pkgpart'    => scalar($conf->config('selfservice_process-pkgpart')),
57       'process-display'    => scalar($conf->config('selfservice_process-display')),
58       'process-skip-first' => $conf->exists('selfservice_process-skip_first'),
59       'num_payments'       => scalar($cust_main->cust_pay), 
60       'surcharge_percentage' => scalar($conf->config('credit-card-surcharge-percentage')),
61     );
62     @$argsref = ( %args );
63
64     return ''; #no error
65   },
66
67   '/edit/cust_main/first_pkg/select-part_pkg.html' => sub {
68     my( $custnum, $argsref ) = @_;
69     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
70       or return "unknown custnum $custnum";
71
72     my $pkgpart = $cust_main->agent->pkgpart_hashref;
73
74     #false laziness w/ edit/cust_main/first_pkg.html
75     my @first_svc = ( 'svc_acct', 'svc_phone' );
76
77     my @part_pkg =
78       grep { $_->svcpart(\@first_svc)
79              && ( $pkgpart->{ $_->pkgpart } 
80                   || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
81                 )
82            }
83       qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
84
85     my $conf = new FS::Conf;
86     if ( $conf->exists('pkg-addon_classnum') ) {
87
88       my %classnum = map  { ( $_->addon_classnum => 1 ) }
89                      grep { $_->freq !~ /^0/ }
90                      map  { $_->part_pkg }
91                           $cust_main->ncancelled_pkgs;
92
93       unless ( $classnum{''} || ! keys %classnum ) {
94         @part_pkg = grep $classnum{ $_->classnum }, @part_pkg;
95       }
96     }
97
98     my %args = @$argsref;
99     $args{part_pkg} = \@part_pkg;
100     $args{first_svc} = \@first_svc;
101     @$argsref = ( %args );
102     return ''; #no error
103
104   },
105
106 );
107
108 my $outbuf;
109 my( $fs_interp, $rt_interp ) = mason_interps('standalone', 'outbuf'=>\$outbuf);
110
111 sub mason_comp {
112   my $packet = shift;
113
114   warn "$me mason_comp called on $packet\n" if $DEBUG;
115
116   my $comp = $packet->{'comp'};
117   unless ( $allowed_comps{$comp} || $session_comps{$comp} ) {
118     return { 'error' => 'Illegal component' };
119   }
120
121   my @args = $packet->{'args'} ? @{ $packet->{'args'} } : ();
122
123   if ( $session_comps{$comp} ) {
124
125     my $session = _cache->get($packet->{'session_id'})
126       or return ( 'error' => "Can't resume session" ); #better error message
127     my $custnum = $session->{'custnum'};
128
129     my $error = &{ $session_callbacks{$comp} }( $custnum, \@args );
130     return { 'error' => $error } if $error;
131
132   }
133
134   my $conf = new FS::Conf;
135   $FS::Mason::Request::FSURL = $conf->config('selfservice_server-base_url');
136   $FS::Mason::Request::FSURL .= '/' unless $FS::Mason::Request::FSURL =~ /\/$/;
137   $FS::Mason::Request::QUERY_STRING = $packet->{'query_string'} || '';
138
139   $outbuf = '';
140   $fs_interp->exec($comp, @args); #only FS for now alas...
141
142   #errors? (turn off in-line error reporting?)
143
144   return { 'output' => $outbuf };
145
146 }
147
148 #hmm
149 sub _cache {
150   $cache ||= new FS::ClientAPI_SessionCache( {
151                'namespace' => 'FS::ClientAPI::MyAccount',
152              } );
153 }
154
155 1;