diff options
author | Ivan Kohler <ivan@freeside.biz> | 2015-09-21 02:02:21 -0700 |
---|---|---|
committer | Ivan Kohler <ivan@freeside.biz> | 2015-09-21 02:02:21 -0700 |
commit | 16e69398896d4d1c0b9c1e5786bdb31b11a18519 (patch) | |
tree | e004e42edeebb684930c1c9b4e00ca4914932077 /FS/FS/part_event/Action | |
parent | ce5f4c2396fd43608b60171464831cf2b53cc367 (diff) |
billing event to call web services, RT#35167
Diffstat (limited to 'FS/FS/part_event/Action')
-rw-r--r-- | FS/FS/part_event/Action/http.pm | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/FS/FS/part_event/Action/http.pm b/FS/FS/part_event/Action/http.pm new file mode 100644 index 0000000..b8715a7 --- /dev/null +++ b/FS/FS/part_event/Action/http.pm @@ -0,0 +1,85 @@ +package FS::part_event::Action::http; + +use strict; +use base qw( FS::part_event::Action ); +use LWP::UserAgent; +use HTTP::Request::Common; +use JSON::XS; +use FS::Misc::DateTime qw( iso8601 ); + +#sub description { 'Send an HTTP or HTTPS GET or POST request'; } +sub description { 'Send an HTTP or HTTPS POST request'; } + +sub eventtable_hashref { + { 'cust_bill' => 1, + 'cust_pay' => 1, + }, +} + +sub option_fields { + ( + 'method' => { label => 'Method', + type => 'select', + options => [qw( POST )], #GET )], + }, + 'url' => { label => 'URL', + type => 'text', + size => 120, + }, + 'ssl_no_verify' => { label => 'Skip SSL certificate validation', + type => 'checkbox', + }, + 'encoding' => { label => 'Encoding', + type => 'select', + options => [qw( JSON )], #XML, Form, etc. + }, + 'content' => { label => 'Content', #nneed better inline docs on format + type => 'textarea', + }, + #'response_error_param' => 'Response error parameter', + ); +} + +sub default_weight { 57; } + +our %content_type = ( + 'JSON' => 'application/json', +); + +sub do_action { + my( $self, $object ) = @_; + + my $cust_main = $self->cust_main($object); + + my %content = + map { + /^\s*(\S+)\s+(.*)$/ or /()()/; + my( $field, $value_expression ) = ( $1, $2 ); + my $value = eval $value_expression; + die $@ if $@; + ( $field, $value ); + } split(/\n/, $self->option('content') ); + + my $content = encode_json( \%content ); + + my @lwp_opts = (); + push @lwp_opts, 'ssl_opts'=>{ 'verify_hostname'=>0 } + if $self->option('ssl_no_verify'); + my $ua = LWP::UserAgent->new(@lwp_opts); + + my $req = HTTP::Request::Common::POST( + $self->option('url'), + Content_Type => $content_type{ $self->option('encoding') }, + Content => $content, + ); + + my $response = $ua->request($req); + + die $response->status_line if $response->is_error; + + my $response_json = decode_json( $response->content ); + die $response_json->{error} if $response_json->{error}; #XXX response_error_param + +} + +1; |