RT# 76308 - Added action Sent HTTP or HTTPS post to event types Package, Customer...
[freeside.git] / FS / FS / part_event / Action / http.pm
1 package FS::part_event::Action::http;
2 use base qw( FS::part_event::Action );
3
4 use strict;
5 use vars qw( $me );
6 use IO::Socket::SSL;
7 use LWP::UserAgent;
8 use HTTP::Request::Common;
9 use JSON::XS;
10 use FS::Misc::DateTime qw( iso8601 );
11
12 $me = '[FS::part_event::Action::http]';
13
14 #sub description { 'Send an HTTP or HTTPS GET or POST request'; }
15 sub description { 'Send an HTTP or HTTPS POST request'; }
16
17 sub eventtable_hashref {
18   { 'cust_bill' => 1,
19     'cust_pay'  => 1,
20     'cust_pkg'  => 1,
21     'cust_main' => 1,
22     'cust_pay_batch' => 1,
23     'cust_statement' => 1,
24     'svc_acct' => 1,
25   },
26 }
27
28 sub option_fields {
29   (
30     'method'        => { label => 'Method',
31                          type  => 'select',
32                          options => [qw( POST )], #GET )],
33                        },
34     'url'           => { label => 'URL',
35                          type  => 'text',
36                          size  => 120,
37                        },
38     'ssl_no_verify' => { label => 'Skip SSL certificate validation',
39                          type  => 'checkbox',
40                        },
41     'encoding'      => { label => 'Encoding',
42                          type  => 'select',
43                          options => [qw( JSON )], #XML, Form, etc.
44                        },
45     'content'       => { label => 'Content', #nneed better inline docs on format
46                          type  => 'textarea',
47                        },
48     #'response_error_param' => 'Response error parameter',
49     'debug'         => { label => 'Enable debugging',
50                          type  => 'checkbox',
51                          value => 1,
52                        },
53   );
54 }
55
56 sub default_weight { 57; }
57
58 our %content_type = (
59   'JSON' => 'application/json',
60 );
61
62 sub do_action {
63   my( $self, $object ) = @_;
64
65   my $cust_main = $self->cust_main($object);
66
67   my %content =
68     map {
69       /^\s*(\S+)\s+(.*)$/ or /()()/;
70       my( $field, $value_expression ) = ( $1, $2 );
71       my $value = eval $value_expression;
72       die $@ if $@;
73       ( $field, $value );
74     } split(/\n/, $self->option('content') );
75
76   if ( $self->option('debug') ) {
77     warn "[$me] $_: ". $content{$_}. "\n" foreach keys %content;
78   }
79
80   my $content = encode_json( \%content );
81
82   my @lwp_opts = ();
83   push @lwp_opts, 'ssl_opts' => {
84                     verify_hostname => 0,
85                     SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
86                   }
87     if $self->option('ssl_no_verify');
88   my $ua = LWP::UserAgent->new(@lwp_opts);
89
90   my $req = HTTP::Request::Common::POST(
91     $self->option('url'),
92     Content_Type => $content_type{ $self->option('encoding') },
93     Content      => $content,
94   );
95
96   my $response = $ua->request($req);
97
98   die $response->status_line if $response->is_error;
99
100   my $response_json = decode_json( $response->content );
101   die $response_json->{error} if $response_json->{error}; #XXX response_error_param
102
103 }
104
105 1;