From 7036a6d0ed1fcfbe41dfef8aaafede8c2cd02895 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 24 Jun 2015 23:38:21 +1200 Subject: [PATCH] towards CRM-16402, improve payment processor loading. In this case we stop calling a deprecated function & filter through loaded in to get the recurring processors and re-introduce caching of payment processors --- CRM/Contribute/Form/AbstractEditPayment.php | 15 ++++++--- CRM/Financial/BAO/PaymentProcessor.php | 37 +++++++++++---------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/CRM/Contribute/Form/AbstractEditPayment.php b/CRM/Contribute/Form/AbstractEditPayment.php index 03f8dccf1b..68a96bcd40 100644 --- a/CRM/Contribute/Form/AbstractEditPayment.php +++ b/CRM/Contribute/Form/AbstractEditPayment.php @@ -58,7 +58,13 @@ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task { * @var array current payment processor including a copy of the object in 'object' key */ public $_paymentProcessor; - public $_recurPaymentProcessors; + + /** + * Available recurring processors. + * + * @var array + */ + public $_recurPaymentProcessors = array(); /** * Array of processor options in the format id => array($id => $label) @@ -435,11 +441,10 @@ LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_co if (!empty($processor['description'])) { $this->_processors[$id] .= ' : ' . ts($processor['description']); } + if ($processor['is_recur']) { + $this->_recurPaymentProcessors[$id] = $this->_processors[$id]; + } } - //get the valid recurring processors. - $test = strtolower($this->_mode) == 'test' ? TRUE : FALSE; - $recurring = CRM_Core_PseudoConstant::paymentProcessor(FALSE, $test, 'is_recur = 1'); - $this->_recurPaymentProcessors = array_intersect_key($this->_processors, $recurring); } $this->assign('recurringPaymentProcessorIds', empty($this->_recurPaymentProcessors) ? '' : implode(',', array_keys($this->_recurPaymentProcessors)) diff --git a/CRM/Financial/BAO/PaymentProcessor.php b/CRM/Financial/BAO/PaymentProcessor.php index cbe6c24114..f7b40a5436 100644 --- a/CRM/Financial/BAO/PaymentProcessor.php +++ b/CRM/Financial/BAO/PaymentProcessor.php @@ -322,15 +322,15 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces * @return array */ public static function getAllPaymentProcessors($mode, $reset = FALSE) { - /* - * $cacheKey = 'CRM_Financial_BAO_Payment_Processor_' . ($mode ? 'test' : 'all'); - * if (!$reset) { - * $processors = CRM_Utils_Cache::singleton()->get($cacheKey); - * if (!empty($processors)) { - * return $processors; - * } - * } - */ + + $cacheKey = 'CRM_Financial_BAO_Payment_Processor_' . ($mode ? 'test' : 'all'); + if (!$reset) { + $processors = CRM_Utils_Cache::singleton()->get($cacheKey); + if (!empty($processors)) { + return $processors; + } + } + $retrievalParameters = array( 'is_active' => TRUE, 'options' => array('sort' => 'is_default DESC, name'), @@ -342,22 +342,22 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces elseif ($mode == 'live') { $retrievalParameters['is_test'] = 0; } + $processors = civicrm_api3('payment_processor', 'get', $retrievalParameters); foreach ($processors['values'] as $processor) { - $fieldsToProvide = array('user_name', 'password', 'signature', 'subject'); + $fieldsToProvide = array('user_name', 'password', 'signature', 'subject', 'is_recur'); foreach ($fieldsToProvide as $field) { - //prevent e-notices in processor classes when not configured + // Prevent e-notices in processor classes when not configured. if (!isset($processor[$field])) { $processor[$field] = NULL; } } $processors['values'][$processor['id']]['payment_processor_type'] = $processor['payment_processor_type'] = $processors['values'][$processor['id']]['api.payment_processor_type.getsingle']['name']; - $mode = empty($processor['is_test']) ? 'live' : 'test'; - $processors['values'][$processor['id']]['object'] = CRM_Core_Payment::singleton($mode, $processor); + $processors['values'][$processor['id']]['object'] = Civi\Payment\System::singleton()->getByProcessor($processor); } - /* - CRM_Utils_Cache::singleton()->set($cacheKey, $processors); - */ + + CRM_Utils_Cache::singleton()->set($cacheKey, $processors['values']); + return $processors['values']; } @@ -387,13 +387,16 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces $mode = 'live'; } $processors = self::getAllPaymentProcessors($mode); + if ($capabilities) { foreach ($processors as $index => $processor) { if (!empty($ids) && !in_array($processor['id'], $ids)) { unset ($processors[$index]); continue; } - if (($error = $processor['object']->checkConfig()) != NULL) { + // 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; } -- 2.25.1