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