changes for cardfortress
[Business-BatchPayment.git] / BatchPayment / Transport / HTTPS.pm
1 package Business::BatchPayment::Transport::HTTPS;
2
3 =head2 HTTPS transport
4
5 Sends a request by HTTPS POST, and downloads the response the same way.
6 Options are 'server', 'port', 'get_path', 'put_path', optionally 
7 'content_type'.
8
9 =cut
10
11 use strict;
12 use Moose;
13 use Net::HTTPS::Any 0.10;
14 with 'Business::BatchPayment::Transport';
15
16 has [ qw( host port get_path put_path ) ] => ( 
17   is => 'rw',
18   isa => 'Str'
19 );
20
21 has 'content_type' => (
22   is => 'rw',
23   isa => 'Str',
24   default => '', # application/x-www-form-urlencoded
25 );
26
27 sub https_post {
28   my $self = shift;
29   my $path = shift;
30   my $content = shift;
31   my %post = (
32     host => $self->host,
33     port => $self->port,
34     path => $path,
35     debug => ($self->debug > 3 ? 1 : 0),
36     'Content-Type' => $self->content_type
37   );
38   if (ref $content and ref $content eq 'HASH') {
39     $post{'args'} = $content;
40   } else {
41     $post{'content'} = $content;
42   }
43
44   warn "starting https_post...\n" if $self->debug;
45   my ( $page, $response, %reply_headers ) = Net::HTTPS::Any::https_post(%post);
46
47   warn "PAGE:\n$page\n\nRESPONSE:\n$response\n\n" if $self->debug >= 2;
48   return ($page, $response, %reply_headers);
49 }
50
51 sub upload {
52   my $self = shift;
53   my $content = shift;
54   $self->https_post($self->put_path, $content);
55 }
56
57 sub download {
58   # will probably need to be overridden in most cases
59   my $self = shift;
60   my ($page, $response, %reply_headers) = $self->https_post($self->get_path);
61   $page;
62 }
63
64 __PACKAGE__->meta->make_immutable;
65
66 1;