From c3408d9308fb2b0104ffd879165dab39e2b2f39a Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Thu, 19 Mar 2020 22:20:08 +0000 Subject: [PATCH] Load contribution page if live payment processor is disabled but test is available --- CRM/Financial/BAO/PaymentProcessor.php | 26 +++++++++++++------ .../Financial/BAO/PaymentProcessorTest.php | 1 + 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/CRM/Financial/BAO/PaymentProcessor.php b/CRM/Financial/BAO/PaymentProcessor.php index ed37743c5a..a24cde25ea 100644 --- a/CRM/Financial/BAO/PaymentProcessor.php +++ b/CRM/Financial/BAO/PaymentProcessor.php @@ -278,11 +278,13 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces * @param bool $reset * @param bool $isCurrentDomainOnly * Do we only want to load payment processors associated with the current domain. + * @param bool|NULL $isActive + * Do we only want active processors, only inactive (FALSE) or all processors (NULL) * * @throws CiviCRM_API3_Exception * @return array */ - public static function getAllPaymentProcessors($mode = 'all', $reset = FALSE, $isCurrentDomainOnly = TRUE) { + public static function getAllPaymentProcessors($mode = 'all', $reset = FALSE, $isCurrentDomainOnly = TRUE, $isActive = TRUE) { $cacheKey = 'CRM_Financial_BAO_Payment_Processor_' . $mode . '_' . $isCurrentDomainOnly . '_' . CRM_Core_Config::domainID(); if (!$reset) { @@ -293,10 +295,13 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces } $retrievalParameters = [ - 'is_active' => TRUE, 'options' => ['sort' => 'is_default DESC, name', 'limit' => 0], 'api.payment_processor_type.getsingle' => 1, ]; + if (isset($isActive)) { + // We use isset because we don't want to set the is_active parameter at all is $isActive is NULL + $retrievalParameters['is_active'] = $isActive; + } if ($isCurrentDomainOnly) { $retrievalParameters['domain_id'] = CRM_Core_Config::domainID(); } @@ -377,17 +382,19 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces * * @return array * available processors + * + * @throws \CiviCRM_API3_Exception */ public static function getPaymentProcessors($capabilities = [], $ids = FALSE) { if (is_array($ids)) { - $testProcessors = in_array('TestMode', $capabilities) ? self::getAllPaymentProcessors('test') : []; - $processors = self::getAllPaymentProcessors('all', FALSE, FALSE); - if (in_array('TestMode', $capabilities)) { + if (in_array('TestMode', $capabilities, TRUE)) { + $testProcessors = in_array('TestMode', $capabilities) ? self::getAllPaymentProcessors('test') : []; + $allProcessors = self::getAllPaymentProcessors('all', FALSE, FALSE, NULL); $possibleLiveIDs = array_diff($ids, array_keys($testProcessors)); foreach ($possibleLiveIDs as $possibleLiveID) { - if (isset($processors[$possibleLiveID]) && ($liveProcessorName = $processors[$possibleLiveID]['name']) != FALSE) { + if (isset($allProcessors[$possibleLiveID]) && ($liveProcessorName = $allProcessors[$possibleLiveID]['name']) != FALSE) { foreach ($testProcessors as $index => $testProcessor) { - if ($testProcessor['name'] == $liveProcessorName) { + if ($testProcessor['name'] === $liveProcessorName) { $ids[] = $testProcessor['id']; } } @@ -395,6 +402,9 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces } $processors = $testProcessors; } + else { + $processors = self::getAllPaymentProcessors('all', FALSE, FALSE); + } } else { $processors = self::getAllPaymentProcessors('all'); @@ -407,7 +417,7 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces } // 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')) { + if (!$processor['object'] instanceof \CRM_Core_Payment) { unset($processors[$index]); continue; } diff --git a/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php b/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php index f9b02b641c..7c92305122 100644 --- a/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php +++ b/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php @@ -10,6 +10,7 @@ */ use Civi\Api4\PaymentProcessor; + /** * Class CRM_Financial_BAO_PaymentProcessorTypeTest * @group headless -- 2.25.1