- test that required_fields() returns 0 if no required fields are missing
[Business-OnlinePayment.git] / OnlinePayment.pm
index 2e9d35d..d051c74 100644 (file)
@@ -1,24 +1,14 @@
 package Business::OnlinePayment;
 
 use strict;
-use vars qw($VERSION); # @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
+use vars qw($VERSION);
 use Carp;
 use Symbol;
 
 require 5.005;
-##use Data::Dumper;
-
-#require Exporter;
-
-#@ISA = (); #qw(Exporter AutoLoader);
-#@EXPORT = qw();
-#@EXPORT_OK = qw();
 
 $VERSION = '3.00_04';
-sub VERSION { #Argument "3.00_01" isn't numeric in subroutine entry
-  local($^W)=0;
-  UNIVERSAL::VERSION(@_);
-}
+$VERSION = eval $VERSION; # modperlstyle: convert the string into a number
 
 my %fields = (
     is_success       => undef,
@@ -32,15 +22,13 @@ my %fields = (
     server           => undef,
     port             => undef,
     path             => undef,
-    risk_management  => undef,
-    risk_management_params => undef,         
+    fraud_detect     => undef,
     server_response  => undef,
     maximum_risk     => undef,
 );
 
 
 sub new {
-
     my($class,$processor,%data) = @_;
 
     Carp::croak("unspecified processor") unless $processor;
@@ -61,7 +49,7 @@ sub new {
     foreach(keys %data) {
         my $key = lc($_);
         my $value = $data{$_};
-        $key =~ s/^\-//;
+        $key =~ s/^\-+//;
         $self->build_subs($key);
         $self->$key($value);
     }
@@ -73,7 +61,7 @@ sub new {
        $self->{_child_submit} = \&$submit;
        *{"${subclass}::submit"} = sub {
            my $self = shift;
-           $self->_pre_submit(@_);
+           $self->_pre_submit();
 
        }
     }
@@ -89,7 +77,7 @@ sub _risk_detect {
     $risk_transaction->content( %parent_content ); 
     $risk_transaction->submit();
     if ($risk_transaction->is_success()) {
-       if ( $risk_transaction->risk_level <= $self->maximum_risk()) {
+       if ( $risk_transaction->fraud_score <= $self->maximum_fraud_score()) {
            $self->{_child_submit}->($self);
        } else {
            $self->is_success(0);
@@ -103,41 +91,34 @@ sub _risk_detect {
 
 sub _pre_submit{
     my ($self) = @_;
-    my $risk_detection = $self->risk_management();
+    my $fraud_detection = $self->fraud_detect();
 
     #
     # early return if user does not want optional risk mgt
     #
 
-    return $self->{_child_submit}->($self,@_) unless $risk_detection && length $risk_detection;
+    return $self->{_child_submit}->($self,@_) unless $fraud_detection && length $fraud_detection;
 
     #
 
     # Search for an appropriate FD module
     #
     
-    foreach my $subclass ( q(Business::OnlinePayment::) . $risk_detection,
-                          q(Business::FraudDetect::).$risk_detection) {
+    foreach my $subclass ( q(Business::OnlinePayment::) . $fraud_detection,
+                          q(Business::FraudDetect::).$fraud_detection) {
 
        if (!defined(&$subclass)) {
            eval "use $subclass";
            if ($@) {
-               Carp::croak("serious problem loading risk_detection module ($@)") unless
+               Carp::croak("serious problem loading fraud_detection module ($@)") unless
                    $@ =~ m/^Can\'t locate/;
            } else {
-               my $risk_tx = bless ( { processor => $risk_detection } , $subclass );
+               my $risk_tx = bless ( { processor => $fraud_detection } , $subclass );
                $risk_tx->build_subs(keys %fields);
                if ($risk_tx->can('set_defaults')) {
                    $risk_tx->set_defaults();
                }
-               my %risk_params = %{$self->risk_management_params()};
-               foreach my $key ( keys %risk_params ) {
-                   my $value = $risk_params{$key};
-                   $key = lc($key);
-                   $key =~ s/^\-//;
-                   $risk_tx->build_subs($key);
-                   $risk_tx->$key($value);
-               }
+               $risk_tx->_glean_parameters_from_parent($self);
                return $self->_risk_detect($risk_tx);
            }
        }
@@ -151,16 +132,20 @@ sub content {
         if($params{'type'}) { $self->transaction_type($params{'type'}); }
         %{$self->{'_content'}} = %params;
     }
-    return %{$self->{'_content'}};
+    return exists $self->{'_content'} ? %{$self->{'_content'}} : ();
 }
 
 sub required_fields {
     my($self,@fields) = @_;
 
+    my @missing;
     my %content = $self->content();
     foreach(@fields) {
-        Carp::croak("missing required field $_") unless exists $content{$_};
+        push(@missing, $_) unless exists $content{$_};
     }
+
+    Carp::croak("missing required field(s): " . join(", ", @missing) . "\n")
+         if(@missing);
 }
 
 sub get_fields {
@@ -205,8 +190,9 @@ sub dump_contents {
 # AutoLoader::AUTOLOAD, instead of passing up the chain
 sub build_subs {
     my $self = shift;
-    no warnings 'redefine';
+
     foreach(@_) {
+        next if($self->can($_));
         eval "sub $_ { my \$self = shift; if(\@_) { \$self->{$_} = shift; } return \$self->{$_}; }";
     }
 }
@@ -225,11 +211,11 @@ Business::OnlinePayment - Perl extension for online payment processing
 
   my $transaction = new Business::OnlinePayment($processor, %processor_info);
   $transaction->content(
-                        type       => 'Visa',
-                        amount     => '49.95',
-                        cardnumber => '1234123412341238',
-                        expiration => '0100',
-                        name       => 'John Q Doe',
+                        type        => 'Visa',
+                        amount      => '49.95',
+                        card_number => '1234123412341238',
+                        expiration  => '0100',
+                        name        => 'John Q Doe',
                        );
   $transaction->submit();