1 package FS::msg_template::http;
2 use base qw( FS::msg_template );
5 use vars qw( $DEBUG $conf );
7 # needed to talk to the external service
9 use HTTP::Request::Common;
12 # needed to manage prepared messages
16 our $me = '[FS::msg_template::http]';
18 sub extension_table { 'msg_template_http' }
22 FS::msg_template::http - Send messages via a web service.
26 FS::msg_template::http is a message processor in which the message is exported
27 to a web service, at both the prepare and send stages.
36 $self->ut_textn('prepare_url')
37 || $self->ut_textn('send_url')
38 || $self->ut_textn('username')
39 || $self->ut_textn('password')
40 || $self->ut_anything('content')
41 || $self->SUPER::check;
46 my( $self, %opt ) = @_;
48 my $json = Cpanel::JSON::XS->new->canonical(1);
50 my $cust_main = $opt{'cust_main'}; # or die 'cust_main required';
51 my $object = $opt{'object'} or die 'object required';
53 my $hashref = $self->prepare_substitutions(%opt);
55 my $document = $json->decode( $self->content || '{}' );
57 'msgname' => $self->msgname,
58 'msgtype' => $opt{'msgtype'},
62 # put override content _somewhere_ so it can be used
63 if ( $opt{'override_content'} ) {
64 $document->{'content'} = $opt{'override_content'};
67 my $request_content = $json->encode($document);
68 warn "$me ".$self->prepare_url."\n" if $DEBUG;
69 warn "$request_content\n\n" if $DEBUG > 1;
70 my $ua = LWP::UserAgent->new;
73 'Content-Type' => 'application/json',
74 'Content' => $request_content,
76 if ( $self->username ) {
77 $request->authorization_basic( $self->username, $self->password );
79 my $response = $ua->request($request);
80 warn "$me received:\n" . $response->as_string . "\n\n" if $DEBUG;
82 my $cust_msg = FS::cust_msg->new({
83 'custnum' => $cust_main->custnum,
84 'msgnum' => $self->msgnum,
86 'msgtype' => ($opt{'msgtype'} || ''),
89 if ( $response->is_success ) {
90 $cust_msg->set(body => $response->decoded_content);
91 $cust_msg->set(status => 'prepared');
93 $cust_msg->set(status => 'failed');
94 $cust_msg->set(error => $response->decoded_content);
100 =item send_prepared CUST_MSG
102 Takes the CUST_MSG object and sends it to its recipient.
108 my $cust_msg = shift or die "cust_msg required";
109 # don't just fail if called as a class method
111 $self = $cust_msg->msg_template;
114 # use cust_msg->header for anything? we _could_...
115 my $request_content = $cust_msg->body;
117 warn "$me ".$self->send_url."\n" if $DEBUG;
118 warn "$request_content\n\n" if $DEBUG > 1;
119 my $ua = LWP::UserAgent->new;
122 'Content-Type' => 'application/json',
123 'Content' => $request_content,
125 if ( $self->username ) {
126 $request->authorization_basic( $self->username, $self->password );
128 my $response = $ua->request($request);
129 warn "$me received:\n" . $response->as_string . "\n\n" if $DEBUG;
132 if ( $response->is_success ) {
133 $cust_msg->set(status => 'sent');
135 $error = $response->decoded_content;
136 $cust_msg->set(error => $error);
137 $cust_msg->set(status => 'failed');
140 if ( $cust_msg->custmsgnum ) {
155 L<FS::Record>, schema.html from the base documentation.