CRM-16402 instantiation of payment processor simplification
authorEileen McNaughton <eileen@fuzion.co.nz>
Tue, 12 May 2015 12:21:47 +0000 (00:21 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Thu, 14 May 2015 22:16:38 +0000 (10:16 +1200)
CRM/Contribute/Form/ContributionBase.php
Civi/Payment/System.php

index 67c6877df7520cd6b993c14f21328aa3a84e95dd..da26df31e684ef02957bc267253bba92db7768f7 100644 (file)
@@ -332,35 +332,32 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
           CRM_Core_Error::fatal(ts('A payment processor must be selected for this contribution page (contact the site administrator for assistance).'));
         }
 
-        $ppIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $ppID);
-        $this->_paymentProcessors = CRM_Financial_BAO_PaymentProcessor::getPayments($ppIds, $this->_mode);
+        $paymentProcessorIDs = explode(CRM_Core_DAO::VALUE_SEPARATOR, $ppID);
 
-        $this->set('paymentProcessors', $this->_paymentProcessors);
-
-        //set default payment processor
-        if (!empty($this->_paymentProcessors) && empty($this->_paymentProcessor)) {
-          foreach ($this->_paymentProcessors as $ppId => $values) {
-            if ($values['is_default'] == 1 || (count($this->_paymentProcessors) == 1)) {
-              $defaultProcessorId = $ppId;
-              break;
-            }
-          }
-        }
+        $this->_paymentProcessors = CRM_Financial_BAO_PaymentProcessor::getPayments($paymentProcessorIDs, $this->_mode);
 
-        if (isset($defaultProcessorId)) {
-          $this->_paymentProcessor = Civi\Payment\System::singleton()->getByProcessor($defaultProcessorId, $this->_mode);
-          $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor);
-        }
-
-        if (!CRM_Utils_System::isNull($this->_paymentProcessors)) {
-          foreach ($this->_paymentProcessors as $eachPaymentProcessor) {
-            // check selected payment processor is active
-            if (empty($eachPaymentProcessor)) {
-              CRM_Core_Error::fatal(ts('A payment processor configured for this page might be disabled (contact the site administrator for assistance).'));
-            }
+        $this->set('paymentProcessors', $this->_paymentProcessors);
 
-            $this->_paymentObject = Civi\Payment\System::singleton()->getByProcessor($eachPaymentProcessor);
-          }
+         if (!empty($this->_paymentProcessors)) {
+           foreach ($this->_paymentProcessors as $paymentProcessorID => $paymentProcessorDetail) {
+             if (($processor = Civi\Payment\System::singleton()->getByProcessor($paymentProcessorDetail)) != FALSE) {
+               // We don't really know why we do this.
+               $this->_paymentObject = $processor;
+             }
+
+             if (empty($this->_paymentProcessor) && $paymentProcessorDetail['is_default'] == 1 || (count
+                 ($this->_paymentProcessors) == 1)
+             ) {
+               $this->_paymentProcessor = $processor;
+               $this->assign('paymentProcessor', $this->_paymentProcessor);
+             }
+           }
+           if (empty($this->_paymentObject)) {
+             throw new CRM_Core_Exception(ts('No valid payment processor'));
+           }
+         }
+        else {
+          throw new CRM_Core_Exception(ts('A payment processor configured for this page might be disabled (contact the site administrator for assistance).'));
         }
       }
 
index 1f552f605f924ef16b7a7b7e793513d71897ca3e..3f10f10484fce602f9d6dbfdb342031f542fce56 100644 (file)
@@ -52,11 +52,15 @@ class System {
           require_once str_replace('_', DIRECTORY_SEPARATOR, $paymentClass) . '.php';
         }
 
-        $this->cache[$id] = new $paymentClass(!empty($processor['is_test']) ? 'test' : 'live', $processor);
-        if ($this->cache[$id]->checkConfig()) {
-          $this->cache[$id] = NULL;
+        $processorObject = new $paymentClass(!empty($processor['is_test']) ? 'test' : 'live', $processor);
+        if ($processorObject->checkConfig()) {
+          $processorObject = NULL;
         }
-      }
+        else {
+          $processorObject->setPaymentProcessor($processor);
+        }
+}
+        $this->cache[$id] = $processorObject;
     }
     return $this->cache[$id];
   }