- add optional \%options (options hashref) to https_get and https_post
authorplobbes <plobbes>
Mon, 12 Mar 2007 06:22:32 +0000 (06:22 +0000)
committerplobbes <plobbes>
Mon, 12 Mar 2007 06:22:32 +0000 (06:22 +0000)
  to allow modules using this to set headers, etc. required for
  PayflowPro HTTP protocol support
- add support for setting Net::SSLeay "$mime_type6" argument
- now pass $DEBUG value to $Net::SSLeay::trace (more control in debugging)
- some minor changes: use 'base', set VERSION per modperlstyle

OnlinePayment/HTTPS.pm

index 0e5ecbd..9bbf28d 100644 (file)
@@ -1,17 +1,14 @@
 package Business::OnlinePayment::HTTPS;
 
 use strict;
-use vars qw($VERSION @ISA $DEBUG $ssl_module $skip_NetSSLeay);
-#use URI;
-#use URI::QueryParam;
+use vars qw($VERSION $DEBUG $ssl_module $skip_NetSSLeay);
 use URI::Escape;
 use Tie::IxHash;
+use base qw(Business::OnlinePayment);
 
-@ISA = qw( Business::OnlinePayment );
-
-$VERSION = '0.04';
-
-$DEBUG = 0;
+$VERSION = '0.05_01';
+$VERSION = eval $VERSION; # modperlstyle: convert the string into a number
+$DEBUG   = 0;
 
 BEGIN {
 
@@ -50,7 +47,7 @@ Business::OnlinePayment::HTTPS - Base class for HTTPS payment APIs
 =head1 SYNOPSIS
 
   package Business::OnlinePayment::MyProcessor
-  @ISA = qw( Business::OnlinePayment::HTTPS );
+  use base qw(Business::OnlinePayment::HTTPS);
 
   sub submit {
           my $self = shift;
@@ -80,7 +77,7 @@ It depends on Net::SSLeay _or_ ( Crypt::SSLeay and LWP::UserAgent ).
 
 =over 4
 
-=item https_get HASHREF | FIELD => VALUE, ...
+=item https_get [ \%options ] HASHREF | FIELD => VALUE, ...
 
 Accepts parameters as either a hashref or a list of fields and values.  In the
 latter case, ordering is preserved (see L<Tie::IxHash> to do so when passing a
@@ -94,15 +91,34 @@ code, and a list of key/value pairs representing the HTTP response headers.
 sub https_get {
   my $self = shift;
 
-  #accept a hashref or a list (keep it ordered)
+  # handle optional options hashref
+  my $opts;
+  if ( scalar(@_) > 1 and ref($_[0]) eq "HASH" ) {
+    $opts = shift;
+  }
+
+  # accept a hashref or a list (keep it ordered)
   my $post_data;
-  if ( ref($_[0]) ) {
+  if ( ref($_[0]) eq 'HASH' ) {
     $post_data = shift;
-  } else {
+  } elsif ( scalar(@_) > 1 ) {
     tie my %hash, 'Tie::IxHash', @_;
     $post_data = \%hash;
+  } elsif ( scalar(@_) == 1 ) {
+    $post_data = shift;
+  } else {
+    die "https_get called with no params\n";
   }
 
+  $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
+
+  ### XXX referer!!!
+  my %headers;
+  if ( ref( $opts->{headers} ) eq "HASH" ) {
+    %headers = %{ $opts->{headers} };
+  }
+  $headers{'Host'} ||= $self->server;
+
   my $path = $self->path;
   if ( keys %$post_data ) {
 
@@ -118,15 +134,11 @@ sub https_get {
 
   }
 
-  my $referer = ''; ### XXX referer!!!
-  my %headers;
-  $headers{'Referer'} = $referer if length($referer);
-
   if ( $ssl_module eq 'Net::SSLeay' ) {
 
     import Net::SSLeay qw(get_https make_headers);
     my $headers = make_headers(%headers);
-    get_https( $self->server, $self->port, $path, $referer, $headers );
+    get_https( $self->server, $self->port, $path, $headers, "", $opts->{"Content-Type"} );
 
   } elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
 
@@ -154,7 +166,7 @@ sub https_get {
 
 }
 
-=item https_post SCALAR | HASHREF | FIELD => VALUE, ...
+=item https_post [ \%options ] SCALAR | HASHREF | FIELD => VALUE, ...
 
 Accepts form fields and values as either a hashref or a list.  In the latter
 case, ordering is preserved (see L<Tie::IxHash> to do so when passing a
@@ -170,7 +182,13 @@ code, and a list of key/value pairs representing the HTTP response headers.
 sub https_post {
   my $self = shift;
 
-  #accept a hashref or a list (keep it ordered)
+  # handle optional options hashref
+  my $opts;
+  if ( scalar(@_) > 1 and ref($_[0]) eq "HASH" ) {
+    $opts = shift;
+  }
+
+  # accept a hashref or a list (keep it ordered)
   my $post_data;
   if ( ref($_[0]) eq 'HASH' ) {
     $post_data = shift;
@@ -183,13 +201,17 @@ sub https_post {
     die "https_post called with no params\n";
   }
 
-  my $referer = ''; ### XXX referer!!!
+  $opts->{"Content-Type"} ||= "application/x-www-form-urlencoded";
+
+  ### XXX referer!!!
   my %headers;
-  $headers{'Referer'} = $referer if length($referer);
-  $headers{'Host'} = $self->server;
+  if ( ref( $opts->{headers} ) eq "HASH" ) {
+    %headers = %{ $opts->{headers} };
+  }
+  $headers{'Host'} ||= $self->server;
 
   if ( $DEBUG && ref($post_data) ) {
-    warn join('', map { "  $_ => ". $post_data->{$_}. "\n" } keys %$post_data );
+    warn "post data:\n", join('', map { "  $_ => ". $post_data->{$_}. "\n" } keys %$post_data );
   }
 
   if ( $ssl_module eq 'Net::SSLeay' ) {
@@ -198,15 +220,14 @@ sub https_post {
     my $headers = make_headers(%headers);
 
     if ( $DEBUG ) {
+      no warnings 'uninitialized';
       warn $self->server. ':'. $self->port. $self->path. "\n";
-      $Net::SSLeay::trace = 2;
+      $Net::SSLeay::trace = $DEBUG;
     }
-    #post_https( $self->server, $self->port, $self->path,
-    #            $headers, make_form(%$post_data)  );
 
     my $raw_data = ref($post_data) ? make_form(%$post_data) : $post_data;
     post_https( $self->server, $self->port, $self->path,
-                $headers, $raw_data );
+                $headers, $raw_data, $opts->{"Content-Type"} );
 
   } elsif ( $ssl_module eq 'Crypt::SSLeay' ) {
 
@@ -228,7 +249,7 @@ sub https_post {
       $res = $ua->request( POST( $url, [ %$post_data ] ) );
     } else {
       my $req =new HTTP::Request( 'POST' => $url );
-      $req->content_type('application/x-www-form-urlencoded');
+      $req->content_type( $opts->{"Content-Type"} );
       $req->content($post_data);
       $res = $ua->request($req);
     }
@@ -249,11 +270,10 @@ sub https_post {
 
 =back
 
-=head1 SEE ALSO 
+=head1 SEE ALSO
 
 L<Business::OnlinePayment>
 
 =cut
 
 1;
-