* Add Class data %WrappedSubmitClassMethod to remember "wrapped" submits
authorplobbes <plobbes>
Mon, 20 Nov 2006 04:57:37 +0000 (04:57 +0000)
committerplobbes <plobbes>
Mon, 20 Nov 2006 04:57:37 +0000 (04:57 +0000)
* Fix new() to check %WrappedSubmitClassMethod to avoid creating deep recursion
* Minor documentation change in print statements use ',' instead of '.'

OnlinePayment.pm

index 28deb6d..5e764a9 100644 (file)
@@ -10,6 +10,9 @@ require 5.005;
 $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 %fields = (
     authorization    => undef,
     error_message    => undef,
@@ -27,7 +30,6 @@ my %fields = (
     transaction_type => undef,
 );
 
-
 sub new {
     my($class,$processor,%data) = @_;
 
@@ -55,15 +57,20 @@ sub new {
     }
 
     unless ( $subclass->can('submit') eq $class->can('submit') ) {
-        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();
-        }
+       # "wrap" submit ONLY once, cache info for later calls to new()
+       if ( ! exists $WrappedSubmitClassMethod{$subclass} ) {
+           no warnings 'redefine';
+           no strict 'refs';
+
+           $WrappedSubmitClassMethod{$subclass} = \&$submit;
+           *{"${subclass}::submit"} = sub {
+               my $self = shift;
+               $self->_pre_submit();
+           }
+       }
+       $self->{_child_submit} = $WrappedSubmitClassMethod{$subclass};
     }
 
     return $self;
@@ -89,7 +96,7 @@ sub _risk_detect {
     }
 }
 
-sub _pre_submit{
+sub _pre_submit {
     my ($self) = @_;
     my $fraud_detection = $self->fraud_detect();
 
@@ -213,9 +220,9 @@ Business::OnlinePayment - Perl extension for online payment processing
   $transaction->submit();
   
   if($transaction->is_success()) {
-    print "Card processed successfully: ".$transaction->authorization()."\n";
+    print "Card processed successfully: ", $transaction->authorization(), "\n";
   } else {
-    print "Card was rejected: ".$transaction->error_message()."\n";
+    print "Card was rejected: ", $transaction->error_message(), "\n";
   }
 
 =head1 DESCRIPTION