Load contribution page if live payment processor is disabled but test is available
authorMatthew Wire <mjw@mjwconsult.co.uk>
Thu, 19 Mar 2020 22:20:08 +0000 (22:20 +0000)
committereileen <emcnaughton@wikimedia.org>
Thu, 16 Jul 2020 07:02:50 +0000 (19:02 +1200)
CRM/Financial/BAO/PaymentProcessor.php
tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php

index ed37743c5ab4fb746e0b9e90a52ba28fc4208435..a24cde25ea7403ca6e5babfc52aa1cf04dc73deb 100644 (file)
@@ -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;
       }
index f9b02b641c584283560bbc404860467bc404e9cd..7c92305122c5108ad9beed8c5137c993fc153046 100644 (file)
@@ -10,6 +10,7 @@
  */
 
 use Civi\Api4\PaymentProcessor;
+
 /**
  * Class CRM_Financial_BAO_PaymentProcessorTypeTest
  * @group headless