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 = JSON->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'},
63 my $request_content = $json->encode($document);
64 warn "$me ".$self->prepare_url."\n" if $DEBUG;
65 warn "$request_content\n\n" if $DEBUG > 1;
66 my $ua = LWP::UserAgent->new;
69 'Content-Type' => 'application/json',
70 'Content' => $request_content,
72 if ( $self->username ) {
73 $request->authorization_basic( $self->username, $self->password );
75 my $response = $ua->request($request);
76 warn "$me received:\n" . $response->as_string . "\n\n" if $DEBUG;
78 my $cust_msg = FS::cust_msg->new({
79 'custnum' => $cust_main->custnum,
80 'msgnum' => $self->msgnum,
82 'msgtype' => ($opt{'msgtype'} || ''),
85 if ( $response->is_success ) {
86 $cust_msg->set(body => $response->decoded_content);
87 $cust_msg->set(status => 'prepared');
89 $cust_msg->set(status => 'failed');
90 $cust_msg->set(error => $response->decoded_content);
96 =item send_prepared CUST_MSG
98 Takes the CUST_MSG object and sends it to its recipient.
104 my $cust_msg = shift or die "cust_msg required";
105 # don't just fail if called as a class method
107 $self = $cust_msg->msg_template;
110 # use cust_msg->header for anything? we _could_...
111 my $request_content = $cust_msg->body;
113 warn "$me ".$self->send_url."\n" if $DEBUG;
114 warn "$request_content\n\n" if $DEBUG > 1;
115 my $ua = LWP::UserAgent->new;
118 'Content-Type' => 'application/json',
119 'Content' => $request_content,
121 if ( $self->username ) {
122 $request->authorization_basic( $self->username, $self->password );
124 my $response = $ua->request($request);
125 warn "$me received:\n" . $response->as_string . "\n\n" if $DEBUG;
128 if ( $response->is_success ) {
129 $cust_msg->set(status => 'sent');
131 $error = $response->decoded_content;
132 $cust_msg->set(error => $error);
133 $cust_msg->set(status => 'failed');
136 if ( $cust_msg->custmsgnum ) {
151 L<FS::Record>, schema.html from the base documentation.