Fixup parameter passing, finish changing names & doco to match
[Business-OnlinePayment.git] / OnlinePayment.pm
index 991bd7e..2b4e17b 100644 (file)
@@ -3,15 +3,18 @@ package Business::OnlinePayment;
 use strict;
 use vars qw($VERSION); # @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
 use Carp;
+use Symbol;
+
+require 5.005;
+use Data::Dumper;
 
-require 5.004;
 #require Exporter;
 
 #@ISA = (); #qw(Exporter AutoLoader);
 #@EXPORT = qw();
 #@EXPORT_OK = qw();
 
-$VERSION = '3.00_03';
+$VERSION = '3.00_04';
 sub VERSION { #Argument "3.00_01" isn't numeric in subroutine entry
   local($^W)=0;
   UNIVERSAL::VERSION(@_);
@@ -19,6 +22,7 @@ sub VERSION { #Argument "3.00_01" isn't numeric in subroutine entry
 
 my %fields = (
     is_success       => undef,
+    failure_status   => undef,
     result_code      => undef,
     test_transaction => undef,
     require_avs      => undef,
@@ -28,11 +32,14 @@ my %fields = (
     server           => undef,
     port             => undef,
     path             => undef,
+    fraud_detect     => undef,
     server_response  => undef,
+    maximum_risk     => undef,
 );
 
 
 sub new {
+
     my($class,$processor,%data) = @_;
 
     Carp::croak("unspecified processor") unless $processor;
@@ -58,9 +65,77 @@ sub new {
         $self->$key($value);
     }
 
+    {
+       no strict 'refs';
+       no warnings 'redefine';
+       my $submit = qualify_to_ref('submit', $subclass);
+       $self->{_child_submit} = \&$submit;
+       *{"${subclass}::submit"} = sub {
+           my $self = shift;
+           $self->_pre_submit();
+
+       }
+    }
+
     return $self;
 }
 
+sub _risk_detect {
+    my ($self, $risk_transaction) = @_;
+
+    my %parent_content = $self->content();
+    $parent_content{action} = 'Fraud Detect';
+    $risk_transaction->content( %parent_content ); 
+    $risk_transaction->submit();
+    if ($risk_transaction->is_success()) {
+       if ( $risk_transaction->fraud_score <= $self->maximum_fraud_score()) {
+           $self->{_child_submit}->($self);
+       } else {
+           $self->is_success(0);
+           $self->error_message('Excessive risk from risk management');
+       }
+    } else {
+       $self->error_message('Error in risk detection stage: ' .  $risk_transaction->error_message);
+       $self->is_success(0);
+    }
+}
+
+sub _pre_submit{
+    my ($self) = @_;
+    my $fraud_detection = $self->fraud_detect();
+
+    #
+    # early return if user does not want optional risk mgt
+    #
+
+    return $self->{_child_submit}->($self,@_) unless $fraud_detection && length $fraud_detection;
+
+    #
+
+    # Search for an appropriate FD module
+    #
+    
+    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 fraud_detection module ($@)") unless
+                   $@ =~ m/^Can\'t locate/;
+           } else {
+               my $risk_tx = bless ( { processor => $fraud_detection } , $subclass );
+               $risk_tx->build_subs(keys %fields);
+               if ($risk_tx->can('set_defaults')) {
+                   $risk_tx->set_defaults();
+               }
+               $risk_tx->_glean_parameters_from_parent($self);
+               return $self->_risk_detect($risk_tx);
+           }
+       }
+    }
+};
+
 sub content {
     my($self,%params) = @_;
 
@@ -122,6 +197,7 @@ sub dump_contents {
 # AutoLoader::AUTOLOAD, instead of passing up the chain
 sub build_subs {
     my $self = shift;
+    no warnings 'redefine';
     foreach(@_) {
         eval "sub $_ { my \$self = shift; if(\@_) { \$self->{$_} = shift; } return \$self->{$_}; }";
     }
@@ -271,6 +347,17 @@ Submit the transaction to the processor for completion
 
 Returns true if the transaction was submitted successfully, false if it failed (or undef if it has not been submitted yet).
 
+=head2 failure_status();
+
+If the transactdion failed, it can optionally return a specific failure status
+(normalized, not gateway-specific).  Currently defined statuses are: "expired",
+"nsf" (non-sufficient funds), "stolen", "pickup", "blacklisted" and
+"declined" (card/transaction declines only, not other errors).
+
+Note that (as of Aug 2006) this is only supported by some of the newest
+processor modules, and that, even if supported, a failure status is an entirely
+optional field that is only set for specific kinds of failures.
+
 =head2 result_code();
 
 Returns the precise result code that the processor returned, these are normally one letter codes that don't mean much unless you understand the protocol they speak, you probably don't need this, but it's there just in case.