- Used feedback from MSCHWERN / Bug #22074 to cleanup new/_pre_submit more
authorplobbes <plobbes>
Mon, 20 Nov 2006 06:34:50 +0000 (06:34 +0000)
committerplobbes <plobbes>
Mon, 20 Nov 2006 06:34:50 +0000 (06:34 +0000)
  Definitely agree having modules use SUPER would be cleaner/safer/better.
  Ivan, we should consider this idea very seriously.
- Now always wrap submit() method with _pre_submit() (but only once)
- no longer populate _child_submit, code in anon sub was cleaned up
- use return values from _pre_submit to determine if real submit is called
  the return values from _pre_submit should be reviewed/verified still

OnlinePayment.pm

index 5e764a9..3f95cd1 100644 (file)
@@ -3,7 +3,6 @@ package Business::OnlinePayment;
 use strict;
 use vars qw($VERSION);
 use Carp;
-use Symbol;
 
 require 5.005;
 
@@ -11,7 +10,7 @@ $VERSION = '3.00_04';
 $VERSION = eval $VERSION; # modperlstyle: convert the string into a number
 
 # Remember subclasses we have "wrapped" submit() with _pre_submit()
-my %WrappedSubmitClassMethod = ();
+my %Presubmit_Added = ();
 
 my %fields = (
     authorization    => undef,
@@ -56,21 +55,18 @@ sub new {
         $self->$key($value);
     }
 
-    unless ( $subclass->can('submit') eq $class->can('submit') ) {
-        my $submit = qualify_to_ref('submit', $subclass);
+    # "wrap" submit with _pre_submit only once
+    unless ( $Presubmit_Added{$subclass} ) {
+        my $real_submit = $subclass->can('submit');
 
-       # "wrap" submit ONLY once, cache info for later calls to new()
-       if ( ! exists $WrappedSubmitClassMethod{$subclass} ) {
-           no warnings 'redefine';
-           no strict 'refs';
+       no warnings 'redefine';
+       no strict 'refs';
 
-           $WrappedSubmitClassMethod{$subclass} = \&$submit;
-           *{"${subclass}::submit"} = sub {
-               my $self = shift;
-               $self->_pre_submit();
-           }
+       *{"${subclass}::submit"} = sub {
+           my $self = shift;
+           return unless $self->_pre_submit(@_);
+           return $real_submit->($self, @_);
        }
-       $self->{_child_submit} = $WrappedSubmitClassMethod{$subclass};
     }
 
     return $self;
@@ -85,7 +81,7 @@ sub _risk_detect {
     $risk_transaction->submit();
     if ($risk_transaction->is_success()) {
        if ( $risk_transaction->fraud_score <= $self->maximum_fraud_score()) {
-           $self->{_child_submit}->($self);
+           return 1;
        } else {
            $self->is_success(0);
            $self->error_message('Excessive risk from risk management');
@@ -101,7 +97,7 @@ sub _pre_submit {
     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;
+    return 1 unless $fraud_detection;
 
     # Search for an appropriate FD module
     foreach my $subclass ( q(Business::OnlinePayment::) . $fraud_detection,
@@ -123,7 +119,7 @@ sub _pre_submit {
            }
        }
     }
-};
+}
 
 sub content {
     my($self,%params) = @_;