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