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 if ( $ssl_module eq 'Net::SSLeay' ) {
144 import Net::SSLeay qw(get_https make_headers);
145 my $headers = make_headers(%headers);
146 get_https( $self->server, $self->port, $path, $headers, "",
147 $opts->{"Content-Type"} );
149 elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
151 import HTTP::Request::Common qw(GET);
153 my $url = 'https://' . $self->server;
154 $url .= ':' . $self->port
155 unless $self->port == 443;
158 my $ua = new LWP::UserAgent;
159 foreach my $hdr ( keys %headers ) {
160 $ua->default_header( $hdr => $headers{$hdr} );
162 my $res = $ua->request( GET($url) );
165 $res->content, $res->code,
166 map { $_ => $res->header($_) } $res->header_field_names
170 die "unknown SSL module $ssl_module";
174 =item https_post [ \%options ] SCALAR | HASHREF | FIELD => VALUE, ...
176 Accepts form fields and values as either a hashref or a list. In the
177 latter case, ordering is preserved (see L<Tie::IxHash> to do so when
180 Also accepts instead a simple scalar containing the raw content.
182 Returns a list consisting of the page content as a string, the HTTP
183 response code, and a list of key/value pairs representing the HTTP
186 The options hashref supports setting headers and Content-Type:
189 headers => { 'X-Header1' => 'value', ... },
190 Content-Type => 'text/namevalue',
198 # handle optional options hashref
200 if ( scalar(@_) > 1 and ref( $_[0] ) eq "HASH" ) {
204 # accept a hashref or a list (keep it ordered)
206 if ( ref( $_[0] ) eq 'HASH' ) {
209 elsif ( scalar(@_) > 1 ) {
210 tie my %hash, 'Tie::IxHash', @_;
213 elsif ( scalar(@_) == 1 ) {
217 die "https_post called with no params\n";
220 $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
224 if ( ref( $opts->{headers} ) eq "HASH" ) {
225 %headers = %{ $opts->{headers} };
227 $headers{'Host'} ||= $self->server;
229 if ( $DEBUG && ref($post_data) ) {
232 map { " $_ => " . $post_data->{$_} . "\n" } keys %$post_data );
235 if ( $ssl_module eq 'Net::SSLeay' ) {
237 import Net::SSLeay qw(post_https make_headers make_form);
238 my $headers = make_headers(%headers);
241 no warnings 'uninitialized';
242 warn $self->server . ':' . $self->port . $self->path . "\n";
243 $Net::SSLeay::trace = $DEBUG;
246 my $raw_data = ref($post_data) ? make_form(%$post_data) : $post_data;
247 post_https( $self->server, $self->port, $self->path, $headers,
248 $raw_data, $opts->{"Content-Type"} );
250 elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
252 import HTTP::Request::Common qw(POST);
254 my $url = 'https://' . $self->server;
255 $url .= ':' . $self->port
256 unless $self->port == 443;
263 my $ua = new LWP::UserAgent;
264 foreach my $hdr ( keys %headers ) {
265 $ua->default_header( $hdr => $headers{$hdr} );
269 if ( ref($post_data) ) {
270 $res = $ua->request( POST( $url, [%$post_data] ) );
273 my $req = new HTTP::Request( 'POST' => $url );
274 $req->content_type( $opts->{"Content-Type"} );
275 $req->content($post_data);
276 $res = $ua->request($req);
280 $res->content, $res->code,
281 map { $_ => $res->header($_) } $res->header_field_names
285 die "unknown SSL module $ssl_module";
293 L<Business::OnlinePayment>