File Coverage

blib/lib/Business/OnlinePayment/HTTPS.pm
Criterion Covered Total %
statement 15 51 29.4
branch 0 16 0.0
condition 0 6 0.0
subroutine 5 7 71.4
pod 2 2 100.0
total 22 82 26.8


line stmt bran cond sub pod time code
1             package Business::OnlinePayment::HTTPS;
2              
3 2     2   511 use strict;
  2         4  
  2         54  
4 2     2   35 use base qw(Business::OnlinePayment);
  2         3  
  2         563  
5 2     2   11 use vars qw($VERSION $DEBUG);
  2         4  
  2         82  
6 2     2   863 use Tie::IxHash;
  2         7202  
  2         57  
7 2     2   787 use Net::HTTPS::Any 0.10;
  2         28475  
  2         868  
8              
9             $VERSION = '0.10';
10             $DEBUG = 0;
11              
12             =head1 NAME
13              
14             Business::OnlinePayment::HTTPS - Base class for HTTPS payment APIs
15              
16             =head1 SYNOPSIS
17              
18             package Business::OnlinePayment::MyProcessor;
19             use base qw(Business::OnlinePayment::HTTPS);
20            
21             sub submit {
22             my $self = shift;
23            
24             #...
25            
26             # pass a list (order is preserved, if your gateway needs that)
27             ( $page, $response, %reply_headers )
28             = $self->https_get( field => 'value', ... );
29            
30             # or a hashref
31             my %hash = ( field => 'value', ... );
32             ( $page, $response_code, %reply_headers )
33             = $self->https_get( \%hash );
34            
35             #...
36             }
37              
38             =head1 DESCRIPTION
39              
40             This is a base class for HTTPS based gateways, providing useful code
41             for implementors of HTTPS payment APIs.
42              
43             It depends on Net::HTTPS::Any, which in turn depends on
44             Net::SSLeay _or_ ( Crypt::SSLeay and LWP::UserAgent ).
45              
46             =head1 METHODS
47              
48             =over 4
49              
50             =item https_get [ \%options ] HASHREF | FIELD => VALUE, ...
51              
52             Accepts parameters as either a hashref or a list of fields and values.
53             In the latter case, ordering is preserved (see L to do so
54             when passing a hashref).
55              
56             Returns a list consisting of the page content as a string, the HTTP
57             response code and message (i.e. "200 OK" or "404 Not Found"), and a list of
58             key/value pairs representing the HTTP response headers.
59              
60             The options hashref supports setting headers:
61              
62             {
63             headers => { 'X-Header1' => 'value', ... },
64             }
65              
66             =cut
67              
68             # Content-Type => 'text/namevalue',
69              
70             sub https_get {
71 0     0 1   my $self = shift;
72              
73             # handle optional options hashref
74 0           my $opts = {};
75 0 0 0       if ( scalar(@_) > 1 and ref( $_[0] ) eq "HASH" ) {
76 0           $opts = shift;
77             }
78              
79             # accept a hashref or a list (keep it ordered)
80 0           my $post_data;
81 0 0         if ( ref( $_[0] ) eq 'HASH' ) {
    0          
    0          
82 0           $post_data = shift;
83             } elsif ( scalar(@_) > 1 ) {
84 0           tie my %hash, 'Tie::IxHash', @_;
85 0           $post_data = \%hash;
86             } elsif ( scalar(@_) == 1 ) {
87 0           $post_data = shift;
88             } else {
89 0           die "https_get called with no params\n";
90             }
91              
92 0           $self->build_subs(qw( response_page response_code response_headers ));
93              
94             my( $res_page, $res_code, @res_headers) = Net::HTTPS::Any::https_get(
95             'host' => $self->server,
96             'path' => $self->path,
97             'headers' => $opts->{headers},
98 0           'args' => $post_data,
99             'debug' => $DEBUG,
100             );
101              
102 0           $self->response_page( $res_page );
103 0           $self->response_code( $res_code );
104 0           $self->response_headers( { @res_headers } );
105              
106 0           ( $res_page, $res_code, @res_headers );
107              
108             }
109              
110             =item https_post [ \%options ] SCALAR | HASHREF | FIELD => VALUE, ...
111              
112             Accepts form fields and values as either a hashref or a list. In the
113             latter case, ordering is preserved (see L to do so when
114             passing a hashref).
115              
116             Also accepts instead a simple scalar containing the raw content.
117              
118             Returns a list consisting of the page content as a string, the HTTP
119             response code and message (i.e. "200 OK" or "404 Not Found"), and a list of
120             key/value pairs representing the HTTP response headers.
121              
122             The options hashref supports setting headers and Content-Type:
123              
124             {
125             headers => { 'X-Header1' => 'value', ... },
126             Content-Type => 'text/namevalue',
127             }
128              
129             =cut
130              
131             sub https_post {
132 0     0 1   my $self = shift;
133              
134             # handle optional options hashref
135 0           my $opts = {};
136 0 0 0       if ( scalar(@_) > 1 and ref( $_[0] ) eq "HASH" ) {
137 0           $opts = shift;
138             }
139              
140             my %post = (
141             'host' => $self->server,
142             'path' => $self->path,
143             'headers' => $opts->{headers},
144 0           'Content-Type' => $opts->{'Content-Type'},
145             'debug' => $DEBUG,
146             );
147              
148             # accept a hashref or a list (keep it ordered)
149 0           my $post_data = '';
150 0           my $content = undef;
151 0 0         if ( ref( $_[0] ) eq 'HASH' ) {
    0          
    0          
152 0           $post{'args'} = shift;
153             } elsif ( scalar(@_) > 1 ) {
154 0           tie my %hash, 'Tie::IxHash', @_;
155 0           $post{'args'} = \%hash;
156             } elsif ( scalar(@_) == 1 ) {
157 0           $post{'content'} = shift;
158             } else {
159 0           die "https_post called with no params\n";
160             }
161              
162 0           $self->build_subs(qw( response_page response_code response_headers ));
163              
164 0           my( $res_page, $res_code, @res_headers)= Net::HTTPS::Any::https_post(%post);
165              
166 0           $self->response_page( $res_page );
167 0           $self->response_code( $res_code );
168 0           $self->response_headers( { @res_headers } );
169              
170 0           ( $res_page, $res_code, @res_headers );
171              
172             }
173              
174             =back
175              
176             =head1 SEE ALSO
177              
178             L, L
179              
180             =cut
181              
182             1;