1 package Business::OnlinePayment::HTTPS;
4 use vars qw($VERSION $DEBUG $ssl_module $skip_NetSSLeay);
7 use base qw(Business::OnlinePayment);
17 die if defined($skip_NetSSLeay) && $skip_NetSSLeay;
19 Net::SSLeay->VERSION(1.30);
22 # qw(get_https post_https make_form make_headers);
23 $ssl_module = 'Net::SSLeay';
28 require LWP::UserAgent;
29 require HTTP::Request::Common;
30 require Crypt::SSLeay;
32 #import HTTP::Request::Common qw(GET POST);
33 $ssl_module = 'Crypt::SSLeay';
37 unless ($ssl_module) {
38 die "One of Net::SSLeay (v1.30 or later)"
39 . " or Crypt::SSLeay (+LWP) is required";
46 Business::OnlinePayment::HTTPS - Base class for HTTPS payment APIs
50 package Business::OnlinePayment::MyProcessor;
51 use base qw(Business::OnlinePayment::HTTPS);
58 # pass a list (order is preserved, if your gateway needs that)
59 ( $page, $response, %reply_headers )
60 = $self->https_get( field => 'value', ... );
63 my %hash = ( field => 'value', ... );
64 ( $page, $response_code, %reply_headers )
65 = $self->https_get( \%hash );
72 This is a base class for HTTPS based gateways, providing useful code
73 for implementors of HTTPS payment APIs.
75 It depends on Net::SSLeay _or_ ( Crypt::SSLeay and LWP::UserAgent ).
81 =item https_get [ \%options ] HASHREF | FIELD => VALUE, ...
83 Accepts parameters as either a hashref or a list of fields and values.
84 In the latter case, ordering is preserved (see L<Tie::IxHash> to do so
85 when passing a hashref).
87 Returns a list consisting of the page content as a string, the HTTP
88 response code, and a list of key/value pairs representing the HTTP
91 The options hashref supports setting headers and Content-Type:
94 headers => { 'X-Header1' => 'value', ... },
95 Content-Type => 'text/namevalue',
103 # handle optional options hashref
105 if ( scalar(@_) > 1 and ref( $_[0] ) eq "HASH" ) {
109 # accept a hashref or a list (keep it ordered)
111 if ( ref( $_[0] ) eq 'HASH' ) {
114 elsif ( scalar(@_) > 1 ) {
115 tie my %hash, 'Tie::IxHash', @_;
118 elsif ( scalar(@_) == 1 ) {
122 die "https_get called with no params\n";
125 $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
129 if ( ref( $opts->{headers} ) eq "HASH" ) {
130 %headers = %{ $opts->{headers} };
132 $headers{'Host'} ||= $self->server;
134 my $path = $self->path;
135 if ( keys %$post_data ) {
138 map { uri_escape($_) . '=' . uri_escape( $post_data->{$_} ) }
142 $self->build_subs(qw( response_page response_code response_headers ));
144 if ( $ssl_module eq 'Net::SSLeay' ) {
146 import Net::SSLeay qw(get_https make_headers);
147 my $headers = make_headers(%headers);
149 my( $res_page, $res_code, @res_headers ) =
150 get_https( $self->server,
155 $opts->{"Content-Type"},
158 $self->response_page( $res_page );
159 $self->response_code( $res_code );
160 $self->response_headers( { @res_headers } );
162 ( $res_page, $res_code, @res_headers );
164 } elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
166 import HTTP::Request::Common qw(GET);
168 my $url = 'https://' . $self->server;
169 $url .= ':' . $self->port
170 unless $self->port == 443;
173 my $ua = new LWP::UserAgent;
174 foreach my $hdr ( keys %headers ) {
175 $ua->default_header( $hdr => $headers{$hdr} );
177 my $res = $ua->request( GET($url) );
179 my @res_headers = map { $_ => $res->header($_) }
180 $res->header_field_names;
182 $self->response_page( $res->content );
183 $self->response_code( $res->code );
184 $self->response_headers( { @res_headers } );
186 ( $res->content, $res->code, @res_headers );
189 die "unknown SSL module $ssl_module";
194 =item https_post [ \%options ] SCALAR | HASHREF | FIELD => VALUE, ...
196 Accepts form fields and values as either a hashref or a list. In the
197 latter case, ordering is preserved (see L<Tie::IxHash> to do so when
200 Also accepts instead a simple scalar containing the raw content.
202 Returns a list consisting of the page content as a string, the HTTP
203 response code, and a list of key/value pairs representing the HTTP
206 The options hashref supports setting headers and Content-Type:
209 headers => { 'X-Header1' => 'value', ... },
210 Content-Type => 'text/namevalue',
218 # handle optional options hashref
220 if ( scalar(@_) > 1 and ref( $_[0] ) eq "HASH" ) {
224 # accept a hashref or a list (keep it ordered)
226 if ( ref( $_[0] ) eq 'HASH' ) {
229 elsif ( scalar(@_) > 1 ) {
230 tie my %hash, 'Tie::IxHash', @_;
233 elsif ( scalar(@_) == 1 ) {
237 die "https_post called with no params\n";
240 $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
244 if ( ref( $opts->{headers} ) eq "HASH" ) {
245 %headers = %{ $opts->{headers} };
247 $headers{'Host'} ||= $self->server;
249 if ( $DEBUG && ref($post_data) ) {
252 map { " $_ => " . $post_data->{$_} . "\n" } keys %$post_data );
255 $self->build_subs(qw( response_page response_code response_headers ));
257 if ( $ssl_module eq 'Net::SSLeay' ) {
259 import Net::SSLeay qw(post_https make_headers make_form);
260 my $headers = make_headers(%headers);
263 no warnings 'uninitialized';
264 warn $self->server . ':' . $self->port . $self->path . "\n";
265 $Net::SSLeay::trace = $DEBUG;
268 my $raw_data = ref($post_data) ? make_form(%$post_data) : $post_data;
270 my( $res_page, $res_code, @res_headers ) =
271 post_https( $self->server,
276 $opts->{"Content-Type"},
279 $self->response_page( $res_page );
280 $self->response_code( $res_code );
281 $self->response_headers( { @res_headers } );
283 ( $res_page, $res_code, @res_headers );
285 } elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
287 import HTTP::Request::Common qw(POST);
289 my $url = 'https://' . $self->server;
290 $url .= ':' . $self->port
291 unless $self->port == 443;
298 my $ua = new LWP::UserAgent;
299 foreach my $hdr ( keys %headers ) {
300 $ua->default_header( $hdr => $headers{$hdr} );
304 if ( ref($post_data) ) {
305 $res = $ua->request( POST( $url, [%$post_data] ) );
308 my $req = new HTTP::Request( 'POST' => $url );
309 $req->content_type( $opts->{"Content-Type"} );
310 $req->content($post_data);
311 $res = $ua->request($req);
314 my @res_headers = map { $_ => $res->header($_) }
315 $res->header_field_names;
317 $self->response_page( $res->content );
318 $self->response_code( $res->code );
319 $self->response_headers( { @res_headers } );
321 ( $res->content, $res->code, @res_headers );
324 die "unknown SSL module $ssl_module";
333 L<Business::OnlinePayment>