CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • basie IPN abstraction
Pages: [1]

Author Topic: basie IPN abstraction  (Read 1481 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
basie IPN abstraction
March 28, 2011, 08:50:44 pm
Hi,

I've just written a payment processor which contains the third identical implementation of the 'single' function.

I'd like to suggest abstracting it into BaseIPN per below - can submit patch on JIRA if OK. NB as I work on other functions I will look at them too but I can't dive in & do wholesale.

Also - dunno why membership & participant objects are duplicated in this code

Code: [Select]
Index: CRM/Core/Payment/BaseIPN.php
===================================================================
--- CRM/Core/Payment/BaseIPN.php (revision 33400)
+++ CRM/Core/Payment/BaseIPN.php (working copy)
@@ -981,6 +981,89 @@
         
         return $statusId;
     }
+   
+    /*
+     * process a one off recurring notification update
+     * @param array $input array of values relating to the contribution
+     * @param array $ids array of ids of relevant objects
+     * @param array $objects array of objects affected by the transaction
+     * @param bool  $recur is the transaction part of a recurring sequence
+     * @param bool $first is this the first in the series of recurring
+     *
+     */
+   
+    function processSingleTransaction( &$input, &$ids, &$objects,
+                     $recur = false,
+                     $first = false )
+    {
+        $contribution =& $objects['contribution'];
+
+        // make sure the invoice is valid and matches what we have in the contribution record
+        if ( ( ! $recur ) || ( $recur && $first ) ) {
+            if ( $contribution->invoice_id != $input['invoice'] ) {
+                CRM_Core_Error::debug_log_message( "Invoice values dont match between database and IPN request" );
+                echo "Failure: Invoice values dont match between database and IPN request<p>";
+                return false;
+            }
+        } else {
+            $contribution->invoice_id = md5( uniqid( rand( ), true ) );
+        }
+
+        if ( ! $recur ) {
+            if ( $contribution->total_amount != $input['amount'] ) {
+                CRM_Core_Error::debug_log_message( "Amount values dont match between database and IPN request" );
+                echo "Failure: Amount values dont match between database and IPN request<p>";
+                return false;
+            }
+        } else {
+            $contribution->total_amount = $input['amount'];
+        }
+
+        require_once 'CRM/Core/Transaction.php';
+        $transaction = new CRM_Core_Transaction( );
+
+        $participant =& $objects['participant'];
+        $membership  =& $objects['membership' ];
+
+        $status = $this->mapProcessorStatus($input['paymentStatus']);
+        if (!empty( $status) ) {
+            return $this->$status( $objects, $transaction );
+        }
+
+        // check if contribution is already completed, if so we ignore this ipn
+        if ( $contribution->contribution_status_id == 1 ) {
+            $transaction->commit( );
+            CRM_Core_Error::debug_log_message( "returning since contribution has already been handled" );
+            echo "Success: Contribution has already been handled<p>";
+            return true;
+        }
+
+        $this->completeTransaction( $input, $ids, $objects, $transaction, $recur );
+    }
+   
+    /*
+     * map the status as retu
+     */
+    function mapProcessorStatus($paymentStatus){
+      switch ($paymentStatus){
+        case 'Denied':
+        case 'Voided':
+        case 'Failed':
+          return 'failed';
+     
+        case 'Pending':
+          return 'pending';
+
+        case 'Refunded':
+        case 'Reversed':
+          return 'cancelled';
+         
+        case 'Completed':
+            return 'unhandled';       
+     
+      }
+     
+    }
 }
 
 
Index: CRM/Core/Payment/PayPalIPN.php
===================================================================
--- CRM/Core/Payment/PayPalIPN.php (revision 33400)
+++ CRM/Core/Payment/PayPalIPN.php (working copy)
@@ -190,70 +190,11 @@
             $objects['contribution'] =& $contribution;
         }
 
-        $this->single( $input, $ids, $objects,
+        $this->processSingleTransaction( $input, $ids, $objects,
                        true, $first );
     }
 
-    function single( &$input, &$ids, &$objects,
-                     $recur = false,
-                     $first = false )
-    {
-        $contribution =& $objects['contribution'];
-
-        // make sure the invoice is valid and matches what we have in the contribution record
-        if ( ( ! $recur ) || ( $recur && $first ) ) {
-            if ( $contribution->invoice_id != $input['invoice'] ) {
-                CRM_Core_Error::debug_log_message( "Invoice values dont match between database and IPN request" );
-                echo "Failure: Invoice values dont match between database and IPN request<p>";
-                return false;
-            }
-        } else {
-            $contribution->invoice_id = md5( uniqid( rand( ), true ) );
-        }
-
-        if ( ! $recur ) {
-            if ( $contribution->total_amount != $input['amount'] ) {
-                CRM_Core_Error::debug_log_message( "Amount values dont match between database and IPN request" );
-                echo "Failure: Amount values dont match between database and IPN request<p>";
-                return false;
-            }
-        } else {
-            $contribution->total_amount = $input['amount'];
-        }
-
-        require_once 'CRM/Core/Transaction.php';
-        $transaction = new CRM_Core_Transaction( );
-
-        // fix for CRM-2842
-        //  if ( ! $this->createContact( $input, $ids, $objects ) ) {
-        //       return false;
-        //  }
-
-        $participant =& $objects['participant'];
-        $membership  =& $objects['membership' ];
-
-        $status = $input['paymentStatus'];
-        if ( $status == 'Denied' || $status == 'Failed' || $status == 'Voided' ) {
-            return $this->failed( $objects, $transaction );
-        } else if ( $status == 'Pending' ) {
-            return $this->pending( $objects, $transaction );
-        } else if ( $status == 'Refunded' || $status == 'Reversed' ) {
-            return $this->cancelled( $objects, $transaction );
-        } else if ( $status != 'Completed' ) {
-            return $this->unhandled( $objects, $transaction );
-        }
-
-        // check if contribution is already completed, if so we ignore this ipn
-        if ( $contribution->contribution_status_id == 1 ) {
-            $transaction->commit( );
-            CRM_Core_Error::debug_log_message( "returning since contribution has already been handled" );
-            echo "Success: Contribution has already been handled<p>";
-            return true;
-        }
-
-        $this->completeTransaction( $input, $ids, $objects, $transaction, $recur );
-    }
-
+
     function main( $component = 'contribute' )
     {
         // CRM_Core_Error::debug_var( 'GET' , $_GET , true, true );
@@ -296,10 +237,10 @@
                 }
                 return $this->recur( $input, $ids, $objects, $first );
             } else {
-                return $this->single( $input, $ids, $objects, false, false );
+                return $this->processSingleTransaction( $input, $ids, $objects, false, false );
             }
         } else {
-            return $this->single( $input, $ids, $objects, false, false );
+            return $this->processSingleTransaction( $input, $ids, $objects, false, false );
         }
     }
 
Index: CRM/Core/Payment/PayPalProIPN.php
===================================================================
--- CRM/Core/Payment/PayPalProIPN.php (revision 33400)
+++ CRM/Core/Payment/PayPalProIPN.php (working copy)
@@ -210,67 +210,11 @@
             $objects['contribution'] =& $contribution;
         }
 
-        $this->single( $input, $ids, $objects,
+        $this->processSingleTransaction( $input, $ids, $objects,
                        true, $first );
     }
     
-    function single( &$input, &$ids, &$objects, $recur = false, $first = false )
-    {
-        $contribution =& $objects['contribution'];
-       
-        // make sure the invoice is valid and matches what we have in the contribution record
-        if ( ( ! $recur ) || ( $recur && $first ) ) {
-            if ( $contribution->invoice_id != $input['invoice'] ) {
-                CRM_Core_Error::debug_log_message( "Invoice values dont match between database and IPN request" );
-                echo "Failure: Invoice values dont match between database and IPN request<p>contribution is" . $contribution->invoice_id  . " and input is " .$input['invoice']  ;
-                return false;
-            }
-        } else {
-            $contribution->invoice_id = md5( uniqid( rand( ), true ) );
-        }
 
-        if ( ! $recur ) {
-            if ( $contribution->total_amount != $input['amount'] ) {
-                CRM_Core_Error::debug_log_message( "Amount values dont match between database and IPN request" );
-                echo "Failure: Amount values dont match between database and IPN request<p>";
-                return false;
-            }
-        } else {
-            $contribution->total_amount = $input['amount'];
-        }
-       
-        require_once 'CRM/Core/Transaction.php';
-        $transaction = new CRM_Core_Transaction( );
-       
-        // fix for CRM-2842
-        //  if ( ! $this->createContact( $input, $ids, $objects ) ) {
-        //       return false;
-        //  }
-       
-        $participant =& $objects['participant'];
-        $membership  =& $objects['membership' ];
-       
-        $status = $input['paymentStatus'];
-        if ( $status == 'Denied' || $status == 'Failed' || $status == 'Voided' ) {
-            return $this->failed( $objects, $transaction );
-        } else if ( $status == 'Pending' ) {
-            return $this->pending( $objects, $transaction );
-        } else if ( $status == 'Refunded' || $status == 'Reversed' ) {
-            return $this->cancelled( $objects, $transaction );
-        } else if ( $status != 'Completed' ) {
-            return $this->unhandled( $objects, $transaction );
-        }
-       
-        // check if contribution is already completed, if so we ignore this ipn
-        if ( $contribution->contribution_status_id == 1 ) {
-            $transaction->commit( );
-            CRM_Core_Error::debug_log_message( "returning since contribution has already been handled" );
-            echo "Success: Contribution has already been handled<p>";
-            return true;
-        }
-       
-        $this->completeTransaction( $input, $ids, $objects, $transaction, $recur );
-    }
     
     function main( $component = 'contribute' ) {
         CRM_Core_Error::debug_var('GET' , $_GET , true , true);
@@ -329,10 +273,10 @@
                 }
                 return $this->recur( $input, $ids, $objects, $first );
             } else {
-                return $this->single( $input, $ids, $objects, false, false );
+                return $this->processSingleTransaction( $input, $ids, $objects, false, false );
             }
         } else {
-            return $this->single( $input, $ids, $objects, false, false );
+            return $this->processSingleTransaction( $input, $ids, $objects, false, false );
         }
     }
     
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: basie IPN abstraction
March 28, 2011, 09:15:40 pm

wanna go ahead and clean things up and submit a patch. we'll look into it for 4.1

might want to choose another name rather than single in the base function to allow legacy payment processors to continue to work and to keep function names a bit cleaner

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: basie IPN abstraction
March 28, 2011, 09:18:28 pm
processSingleTransaction was what I put in - do you mean different to that?
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • basie IPN abstraction

This forum was archived on 2017-11-26.