3.05, add nacha_sec_code
[Business-OnlinePayment.git] / OnlinePayment / HTTPS.pm
1 package Business::OnlinePayment::HTTPS;
2
3 use strict;
4 use base qw(Business::OnlinePayment);
5 use vars qw($VERSION $DEBUG);
6 use Tie::IxHash;
7 use Net::HTTPS::Any 0.10;
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<Tie::IxHash> 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     my $self = shift;
72
73     # handle optional options hashref
74     my $opts = {};
75     if ( scalar(@_) > 1 and ref( $_[0] ) eq "HASH" ) {
76       $opts = shift;
77     }
78
79     # accept a hashref or a list (keep it ordered)
80     my $post_data;
81     if ( ref( $_[0] ) eq 'HASH' ) {
82       $post_data = shift;
83     } elsif ( scalar(@_) > 1 ) {
84       tie my %hash, 'Tie::IxHash', @_;
85       $post_data = \%hash;
86     } elsif ( scalar(@_) == 1 ) {
87       $post_data = shift;
88     } else {
89       die "https_get called with no params\n";
90     }
91
92     $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       'args'    => $post_data,
99       'debug'   => $DEBUG,
100     );
101
102     $self->response_page( $res_page );
103     $self->response_code( $res_code );
104     $self->response_headers( { @res_headers } );
105
106     ( $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<Tie::IxHash> 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     my $self = shift;
133
134     # handle optional options hashref
135     my $opts = {};
136     if ( scalar(@_) > 1 and ref( $_[0] ) eq "HASH" ) {
137         $opts = shift;
138     }
139
140     my %post = (
141       'host'         => $self->server,
142       'path'         => $self->path,
143       'headers'      => $opts->{headers},
144       'Content-Type' => $opts->{'Content-Type'},
145       'debug'        => $DEBUG,
146     );
147
148     # accept a hashref or a list (keep it ordered)
149     my $post_data = '';
150     my $content = undef;
151     if ( ref( $_[0] ) eq 'HASH' ) {
152       $post{'args'} = shift;
153     } elsif ( scalar(@_) > 1 ) {
154       tie my %hash, 'Tie::IxHash', @_;
155       $post{'args'} = \%hash;
156     } elsif ( scalar(@_) == 1 ) {
157       $post{'content'} = shift;
158     } else {
159       die "https_post called with no params\n";
160     }
161
162     $self->build_subs(qw( response_page response_code response_headers ));
163
164     my( $res_page, $res_code, @res_headers)= Net::HTTPS::Any::https_post(%post);
165
166     $self->response_page( $res_page );
167     $self->response_code( $res_code );
168     $self->response_headers( { @res_headers } );
169
170     ( $res_page, $res_code, @res_headers );
171
172 }
173
174 =back
175
176 =head1 SEE ALSO
177
178 L<Business::OnlinePayment>, L<Net::HTTPS::Any>
179
180 =cut
181
182 1;