summaryrefslogtreecommitdiff
path: root/BatchPayment/Transport/HTTPS.pm
blob: 0eb783b549d59d92db1fefe553a52c5533208a1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package Business::BatchPayment::Transport::HTTPS;

=head2 HTTPS transport

Sends a request by HTTPS POST, and downloads the response the same way.
Options are 'server', 'port', 'get_path', 'put_path', optionally 
'content_type'.

=cut

use strict;
use Moose;
use Net::HTTPS::Any 0.10;
with 'Business::BatchPayment::Transport';

has [ qw( host port get_path put_path ) ] => ( 
  is => 'rw',
  isa => 'Str'
);

has 'content_type' => (
  is => 'rw',
  isa => 'Str',
  default => '', # application/x-www-form-urlencoded
);

sub https_post {
  my $self = shift;
  my $path = shift;
  my $content = shift;
  my %post = (
    host => $self->host,
    port => $self->port,
    path => $path,
    debug => ($self->debug > 3 ? 1 : 0),
    'Content-Type' => $self->content_type
  );
  if (ref $content and ref $content eq 'HASH') {
    $post{'args'} = $content;
  } else {
    $post{'content'} = $content;
  }

  warn "starting https_post...\n" if $self->debug;
  my ( $page, $response, %reply_headers ) = Net::HTTPS::Any::https_post(%post);

  warn "PAGE:\n$page\n\nRESPONSE:\n$response\n\n" if $self->debug >= 2;
  return ($page, $response, %reply_headers);
}

sub upload {
  my $self = shift;
  my $content = shift;
  $self->https_post($self->put_path, $content);
}

sub download {
  # will probably need to be overridden in most cases
  my $self = shift;
  my ($page, $response, %reply_headers) = $self->https_post($self->get_path);
  $page;
}

__PACKAGE__->meta->make_immutable;

1;