From: Eileen McNaughton Date: Tue, 12 May 2015 12:21:47 +0000 (+1200) Subject: CRM-16402 instantiation of payment processor simplification X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=576c0410b1744e98402d2195efbfa4a2813d6997;p=civicrm-core.git CRM-16402 instantiation of payment processor simplification --- diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index 67c6877df7..da26df31e6 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -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).')); } } diff --git a/Civi/Payment/System.php b/Civi/Payment/System.php index 1f552f605f..3f10f10484 100644 --- a/Civi/Payment/System.php +++ b/Civi/Payment/System.php @@ -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]; }