From 1b9f9ca3b7229ce376aaae55919e22df22deb1db Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 29 Jun 2015 00:45:17 +1200 Subject: [PATCH] CRM-16755 do not load invalid payment processors --- CRM/Contribute/Form/Contribution/Main.php | 2 +- CRM/Contribute/Form/ContributionBase.php | 36 +++------------ CRM/Core/Form.php | 54 +++++++++++++++++++++++ CRM/Core/Payment/PayPalImpl.php | 14 ++---- CRM/Financial/BAO/PaymentProcessor.php | 34 +++++++------- Civi/Payment/System.php | 4 ++ 6 files changed, 86 insertions(+), 58 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index a5b870b401..82dca91b89 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -1404,7 +1404,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $params['invoiceID'] = $invoiceID; $params['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $this->_values['title']); - $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); + $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); $token = $payment->setExpressCheckout($params); if (is_a($token, 'CRM_Core_Error')) { CRM_Core_Error::displaySessionError($token); diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index ba850bd534..a5d93d1655 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -73,6 +73,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { * @var array */ public $_paymentProcessor; + public $_paymentObject = NULL; /** @@ -309,41 +310,14 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { $isMonetary = CRM_Utils_Array::value('is_monetary', $this->_values); $isPayLater = CRM_Utils_Array::value('is_pay_later', $this->_values); - //FIXME: to support multiple payment processors if ($isMonetary && (!$isPayLater || !empty($this->_values['payment_processor'])) ) { - $ppID = CRM_Utils_Array::value('payment_processor', $this->_values); - if (!$ppID) { - CRM_Core_Error::fatal(ts('A payment processor must be selected for this contribution page (contact the site administrator for assistance).')); - } - - $paymentProcessorIDs = explode(CRM_Core_DAO::VALUE_SEPARATOR, $ppID); - - $this->_paymentProcessors = CRM_Financial_BAO_PaymentProcessor::getPayments($paymentProcessorIDs, $this->_mode); - - $this->set('paymentProcessors', $this->_paymentProcessors); - - 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; - } + $this->_paymentProcessorIDs = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Utils_Array::value + ('payment_processor', $this->_values) + ); - if (empty($this->_paymentProcessor) && $paymentProcessorDetail['is_default'] == 1 || (count($this->_paymentProcessors) == 1) - ) { - $this->_paymentProcessor = $paymentProcessorDetail; - $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).')); - } + $this->assignPaymentProcessor(); } // get price info diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index b7fa74c12b..38204edb3a 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -73,6 +73,28 @@ class CRM_Core_Form extends HTML_QuickForm_Page { */ protected $_action; + /** + * Available payment processors. + * + * As part of trying to consolidate various payment pages we store processors here & have functions + * at this level to manage them. + * + * @var array + * An array of payment processor details with objects loaded in the 'object' field. + */ + public $_paymentProcessors; + + /** + * Available payment processors (IDS). + * + * As part of trying to consolidate various payment pages we store processors here & have functions + * at this level to manage them. + * + * @var array + * An array of the IDS available on this form. + */ + public $_paymentProcessorIDs; + /** * The renderer used for this form * @@ -636,6 +658,38 @@ class CRM_Core_Form extends HTML_QuickForm_Page { $this->assign('bltID', $this->_bltID); } + /** + * This if a front end form function for setting the payment processor. + * + * It would be good to sync it with the back-end function on abstractEditPayment & use one everywhere. + * + * @throws \CRM_Core_Exception + */ + protected function assignPaymentProcessor() { + $this->_paymentProcessors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors( + array(ucfirst($this->_mode) . 'Mode'), + $this->_paymentProcessorIDs + ); + + if (!empty($this->_paymentProcessors)) { + foreach ($this->_paymentProcessors as $paymentProcessorID => $paymentProcessorDetail) { + if (empty($this->_paymentProcessor) && $paymentProcessorDetail['is_default'] == 1 || (count($this->_paymentProcessors) == 1) + ) { + $this->_paymentProcessor = $paymentProcessorDetail; + $this->assign('paymentProcessor', $this->_paymentProcessor); + // Setting this is a bit of a legacy overhang. + $this->_paymentObject = $paymentProcessorDetail['object']; + } + } + // It's not clear why we set this on the form. + $this->set('paymentProcessors', $this->_paymentProcessors); + } + else { + throw new CRM_Core_Exception(ts('A payment processor configured for this page might be disabled (contact the site administrator for assistance).')); + } + } + + /** * Setter function for options. * diff --git a/CRM/Core/Payment/PayPalImpl.php b/CRM/Core/Payment/PayPalImpl.php index 5c2d4592ec..87e04c1de0 100644 --- a/CRM/Core/Payment/PayPalImpl.php +++ b/CRM/Core/Payment/PayPalImpl.php @@ -69,9 +69,6 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { $this->_processorName = ts('PayPal Express'); } - if (!$this->_paymentProcessor['user_name']) { - CRM_Core_Error::fatal(ts('Could not find user name for payment processor')); - } } /** @@ -432,14 +429,6 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { public function checkConfig() { $error = array(); $paymentProcessorType = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, NULL, 'name'); - if ( - $this->_paymentProcessor['payment_processor_type_id'] == CRM_Utils_Array::key('PayPal_Standard', $paymentProcessorType) || - $this->_paymentProcessor['payment_processor_type_id'] == CRM_Utils_Array::key('PayPal', $paymentProcessorType) - ) { - if (empty($this->_paymentProcessor['user_name'])) { - $error[] = ts('User Name is not set in the Administer » System Settings » Payment Processors.'); - } - } if ($this->_paymentProcessor['payment_processor_type_id'] != CRM_Utils_Array::key('PayPal_Standard', $paymentProcessorType)) { if (empty($this->_paymentProcessor['signature'])) { @@ -450,6 +439,9 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { $error[] = ts('Password is not set in the Administer » System Settings » Payment Processors.'); } } + if (!$this->_paymentProcessor['user_name']) { + $error[] = ts('User Name is not set in the Administer » System Settings » Payment Processors.'); + } if (!empty($error)) { return implode('

', $error); diff --git a/CRM/Financial/BAO/PaymentProcessor.php b/CRM/Financial/BAO/PaymentProcessor.php index f7b40a5436..dab67d793b 100644 --- a/CRM/Financial/BAO/PaymentProcessor.php +++ b/CRM/Financial/BAO/PaymentProcessor.php @@ -231,6 +231,10 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces } /** + * User getPaymentProcessors. + * + * @deprecated + * * @param $paymentProcessorIDs * @param $mode * @@ -388,25 +392,25 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces } $processors = self::getAllPaymentProcessors($mode); - if ($capabilities) { - foreach ($processors as $index => $processor) { - if (!empty($ids) && !in_array($processor['id'], $ids)) { - unset ($processors[$index]); - continue; - } - // Invalid processors will store a null value in 'object' (e.g. if not all required config fields are present). - // This is determined by calling when loading the processor via the $processorObject->checkConfig() function. - if (!is_a($processor['object'], 'CRM_Core_Payment')) { + foreach ($processors as $index => $processor) { + if (!empty($ids) && !in_array($processor['id'], $ids)) { + unset ($processors[$index]); + continue; + } + // Invalid processors will store a null value in 'object' (e.g. if not all required config fields are present). + // This is determined by calling when loading the processor via the $processorObject->checkConfig() function. + if (!is_a($processor['object'], 'CRM_Core_Payment')) { + unset ($processors[$index]); + continue; + } + foreach ($capabilities as $capability) { + if (($processor['object']->supports($capability)) == FALSE) { unset ($processors[$index]); - continue; - } - foreach ($capabilities as $capability) { - if (($processor['object']->supports($capability)) == FALSE) { - unset ($processors[$index]); - } + continue 1; } } } + return $processors; } diff --git a/Civi/Payment/System.php b/Civi/Payment/System.php index c8e842e960..42b1152806 100644 --- a/Civi/Payment/System.php +++ b/Civi/Payment/System.php @@ -74,6 +74,8 @@ class System { /** * @param int $id + * + * @return \Civi\Payment\CRM_Core_Payment|NULL * @throws \CiviCRM_API3_Exception */ public function getById($id) { @@ -84,6 +86,8 @@ class System { /** * @param string $name * @param bool $is_test + * + * @return \Civi\Payment\CRM_Core_Payment|NULL * @throws \CiviCRM_API3_Exception */ public function getByName($name, $is_test) { -- 2.25.1