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 my $res = $ua->request( GET($url) );
162 $res->content, $res->code,
163 map { $_ => $res->header($_) } $res->header_field_names
167 die "unknown SSL module $ssl_module";
171 =item https_post [ \%options ] SCALAR | HASHREF | FIELD => VALUE, ...
173 Accepts form fields and values as either a hashref or a list. In the
174 latter case, ordering is preserved (see L<Tie::IxHash> to do so when
177 Also accepts instead a simple scalar containing the raw content.
179 Returns a list consisting of the page content as a string, the HTTP
180 response code, and a list of key/value pairs representing the HTTP
183 The options hashref supports setting headers and Content-Type:
186 headers => { 'X-Header1' => 'value', ... },
187 Content-Type => 'text/namevalue',
195 # handle optional options hashref
197 if ( scalar(@_) > 1 and ref( $_[0] ) eq "HASH" ) {
201 # accept a hashref or a list (keep it ordered)
203 if ( ref( $_[0] ) eq 'HASH' ) {
206 elsif ( scalar(@_) > 1 ) {
207 tie my %hash, 'Tie::IxHash', @_;
210 elsif ( scalar(@_) == 1 ) {
214 die "https_post called with no params\n";
217 $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
221 if ( ref( $opts->{headers} ) eq "HASH" ) {
222 %headers = %{ $opts->{headers} };
224 $headers{'Host'} ||= $self->server;
226 if ( $DEBUG && ref($post_data) ) {
229 map { " $_ => " . $post_data->{$_} . "\n" } keys %$post_data );
232 if ( $ssl_module eq 'Net::SSLeay' ) {
234 import Net::SSLeay qw(post_https make_headers make_form);
235 my $headers = make_headers(%headers);
238 no warnings 'uninitialized';
239 warn $self->server . ':' . $self->port . $self->path . "\n";
240 $Net::SSLeay::trace = $DEBUG;
243 my $raw_data = ref($post_data) ? make_form(%$post_data) : $post_data;
244 post_https( $self->server, $self->port, $self->path, $headers,
245 $raw_data, $opts->{"Content-Type"} );
247 elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
249 import HTTP::Request::Common qw(POST);
251 my $url = 'https://' . $self->server;
252 $url .= ':' . $self->port
253 unless $self->port == 443;
260 my $ua = new LWP::UserAgent;
263 if ( ref($post_data) ) {
264 $res = $ua->request( POST( $url, [%$post_data] ) );
267 my $req = new HTTP::Request( 'POST' => $url );
268 $req->content_type( $opts->{"Content-Type"} );
269 $req->content($post_data);
270 $res = $ua->request($req);
274 $res->content, $res->code,
275 map { $_ => $res->header($_) } $res->header_field_names
279 die "unknown SSL module $ssl_module";
287 L<Business::OnlinePayment>