cust_main exports! cust_main-exports config option and part_export/cust_http.pm...
[freeside.git] / FS / FS / part_export / http.pm
1 package FS::part_export::http;
2
3 use base qw( FS::part_export );
4 use vars qw( %options %info );
5 use Tie::IxHash;
6
7 tie %options, 'Tie::IxHash',
8   'method' => { label   =>'Method',
9                 type    =>'select',
10                 #options =>[qw(POST GET)],
11                 options =>[qw(POST)],
12                 default =>'POST' },
13   'url'    => { label   => 'URL', default => 'http://', },
14   'insert_data' => {
15     label   => 'Insert data',
16     type    => 'textarea',
17     default => join("\n",
18       'DomainName $svc_x->domain',
19       'Email ( grep { $_ !~ /^(POST|FAX)$/ } $svc_x->cust_svc->cust_pkg->cust_main->invoicing_list)[0]',
20       'test 1',
21       'reseller $svc_x->cust_svc->cust_pkg->part_pkg->pkg =~ /reseller/i',
22     ),
23   },
24   'delete_data' => {
25     label   => 'Delete data',
26     type    => 'textarea',
27     default => join("\n",
28     ),
29   },
30   'replace_data' => {
31     label   => 'Replace data',
32     type    => 'textarea',
33     default => join("\n",
34     ),
35   },
36 ;
37
38 %info = (
39   'svc'     => 'svc_domain',
40   'desc'    => 'Send an HTTP or HTTPS GET or POST request',
41   'options' => \%options,
42   'notes'   => <<'END'
43 Send an HTTP or HTTPS GET or POST to the specified URL.  For HTTPS support,
44 <a href="http://search.cpan.org/dist/Crypt-SSLeay">Crypt::SSLeay</a>
45 or <a href="http://search.cpan.org/dist/IO-Socket-SSL">IO::Socket::SSL</a>
46 is required.
47 END
48 );
49
50 sub rebless { shift; }
51
52 sub _export_insert {
53   my $self = shift;
54   $self->_export_command('insert', @_);
55 }
56
57 sub _export_delete {
58   my $self = shift;
59   $self->_export_command('delete', @_);
60 }
61
62 sub _export_command {
63   my( $self, $action, $svc_x ) = ( shift, shift, shift );
64
65   return unless $self->option("${action}_data");
66
67   my $cust_main = $svc_x->table eq 'cust_main'
68                     ? $svc_x
69                     : $svc_x->cust_svc->cust_pkg->cust_main;
70
71   $self->http_queue( $svc_x->svcnum,
72     $self->option('method'),
73     $self->option('url'),
74     map {
75       /^\s*(\S+)\s+(.*)$/ or /()()/;
76       my( $field, $value_expression ) = ( $1, $2 );
77       my $value = eval $value_expression;
78       die $@ if $@;
79       ( $field, $value );
80     } split(/\n/, $self->option("${action}_data") )
81   );
82
83 }
84
85 sub _export_replace {
86   my( $self, $new, $old ) = ( shift, shift, shift );
87
88   return unless $self->option('replace_data');
89
90   my $new_cust_main = $new->table eq 'cust_main'
91                         ? $new
92                         : $new->cust_svc->cust_pkg->cust_main;
93
94   $self->http_queue( $svc_x->svcnum,
95     $self->option('method'),
96     $self->option('url'),
97     map {
98       /^\s*(\S+)\s+(.*)$/ or /()()/;
99       my( $field, $value_expression ) = ( $1, $2 );
100       die $@ if $@;
101       ( $field, $value );
102     } split(/\n/, $self->option('replace_data') )
103   );
104
105 }
106
107 sub http_queue {
108   my($self, $svcnum) = (shift, shift);
109   my $queue = new FS::queue { 'job' => "FS::part_export::http::http" };
110   $queue->svcnum($svcnum) if $svcnum;
111   $queue->insert( @_ );
112 }
113
114 sub http {
115   my($method, $url, @data) = @_;
116
117   $method = lc($method);
118
119   eval "use LWP::UserAgent;";
120   die "using LWP::UserAgent: $@" if $@;
121   eval "use HTTP::Request::Common;";
122   die "using HTTP::Request::Common: $@" if $@;
123
124   my $ua = LWP::UserAgent->new;
125
126   #my $response = $ua->$method(
127   #  $url, \%data,
128   #  'Content-Type'=>'application/x-www-form-urlencoded'
129   #);
130   my $req = HTTP::Request::Common::POST( $url, \@data );
131   my $response = $ua->request($req);
132
133   die $response->error_as_HTML if $response->is_error;
134
135 }
136
137 1;
138