international self-service payments, RT#1592
[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(qsearchs);
10 use FS::cust_main;
11
12 $DEBUG = 0;
13 $me = '[FS::ClientAPI::MasonComponent]';
14
15 my %allowed_comps = map { $_=>1 } qw(
16   /elements/select-did.html
17   /misc/areacodes.cgi
18   /misc/exchanges.cgi
19   /misc/phonenums.cgi
20   /misc/states.cgi
21   /misc/counties.cgi
22 );
23
24 my %session_comps = map { $_=>1 } qw(
25   /elements/location.html
26 );
27
28 my %session_callbacks = (
29   '/elements/location.html' => sub {
30     my( $custnum, $argsref ) = @_;
31     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
32       or return "unknown custnum $custnum";
33     my %args = @$argsref;
34     $args{object} = $cust_main;
35     @$argsref = ( %args );
36     return ''; #no error
37   },
38 );
39
40 my $outbuf;
41 my( $fs_interp, $rt_interp ) = mason_interps('standalone', 'outbuf'=>\$outbuf);
42
43 sub mason_comp {
44   my $packet = shift;
45
46   warn "$me mason_comp called on $packet\n" if $DEBUG;
47
48   my $comp = $packet->{'comp'};
49   unless ( $allowed_comps{$comp} || $session_comps{$comp} ) {
50     return { 'error' => 'Illegal component' };
51   }
52
53   my @args = $packet->{'args'} ? @{ $packet->{'args'} } : ();
54
55   if ( $session_comps{$comp} ) {
56
57     my $session = _cache->get($packet->{'session_id'})
58       or return ( 'error' => "Can't resume session" ); #better error message
59     my $custnum = $session->{'custnum'};
60
61     my $error = &{ $session_callbacks{$comp} }( $custnum, \@args );
62     return { 'error' => $error } if $error;
63
64   }
65
66   my $conf = new FS::Conf;
67   $FS::Mason::Request::FSURL = $conf->config('selfservice_server-base_url');
68   $FS::Mason::Request::QUERY_STRING = $packet->{'query_string'} || '';
69
70   $outbuf = '';
71   $fs_interp->exec($comp, @args); #only FS for now alas...
72
73   #errors? (turn off in-line error reporting?)
74
75   return { 'output' => $outbuf };
76
77 }
78
79 #hmm
80 sub _cache {
81   $cache ||= new FS::ClientAPI_SessionCache( {
82                'namespace' => 'FS::ClientAPI::MyAccount',
83              } );
84 }
85
86 1;