CRM-16755 do not load invalid payment processors
authorEileen McNaughton <eileen@fuzion.co.nz>
Sun, 28 Jun 2015 12:45:17 +0000 (00:45 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Sun, 28 Jun 2015 12:45:17 +0000 (00:45 +1200)
CRM/Contribute/Form/Contribution/Main.php
CRM/Contribute/Form/ContributionBase.php
CRM/Core/Form.php
CRM/Core/Payment/PayPalImpl.php
CRM/Financial/BAO/PaymentProcessor.php
Civi/Payment/System.php

index a5b870b4015f7d4153257125630ba280899569e5..82dca91b893566dcf7093488171bdbd5f25ade1b 100644 (file)
@@ -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);
index ba850bd534e4ad76f0d5f7986a956c9974da5924..a5d93d1655bc39aa45ca97215df8aae34f6cc259 100644 (file)
@@ -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
index b7fa74c12b6e4384bbc5aaa87530893d8f96755d..38204edb3a4f5ff119a793fe3f58ec5f0105d371 100644 (file)
@@ -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.
    *
index 5c2d4592ecf46d168016badf8d7c03926801ad7a..87e04c1de0c44e3a4507c4a35fd7ace5f80be4b5 100644 (file)
@@ -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 &raquo; System Settings &raquo; 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 &raquo; System Settings &raquo; Payment Processors.');
       }
     }
+    if (!$this->_paymentProcessor['user_name']) {
+      $error[] = ts('User Name is not set in the Administer &raquo; System Settings &raquo; Payment Processors.');
+    }
 
     if (!empty($error)) {
       return implode('<p>', $error);
index f7b40a5436b3c76e9c31434a4842119c25e5fa47..dab67d793bc8a329cb93194f570b8f21ee256b66 100644 (file)
@@ -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;
   }
 
index c8e842e9607cd38dee213004b53e1f31ee4386c0..42b1152806c84d35f109a5864cbfcfca94d1f80b 100644 (file)
@@ -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) {