Merge pull request #4883 from totten/master-cs4
[civicrm-core.git] / CRM / Financial / BAO / PaymentProcessor.php
index 2e478c973a51a125a2b5e7a4e749636d19720f9d..adb740cb33910371ee3bccc0a8a055a31e7ff740 100644 (file)
@@ -36,8 +36,7 @@
 /**
  * This class contains payment processor related functions.
  */
-class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProcessor
-{
+class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProcessor {
   /**
    * Static holder for the default payment processor
    */
@@ -46,7 +45,8 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
   /**
    * Create Payment Processor
    *
-   * @param array $params parameters for Processor entity
+   * @param array $params
+   *   Parameters for Processor entity.
    *
    * @return CRM_Financial_DAO_PaymentProcessor
    * @throws Exception
@@ -77,7 +77,7 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
         'entity_table' => 'civicrm_payment_processor',
         'entity_id' => $processor->id,
         'account_relationship' => $relationTypeId,
-        'financial_account_id' => $params['financial_account_id']
+        'financial_account_id' => $params['financial_account_id'],
       );
       CRM_Financial_BAO_FinancialTypeAccount::add($values);
     }
@@ -96,10 +96,12 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
    * retrieves the relevant objects. It also stores all the retrieved
    * values in the default array
    *
-   * @param array $params   (reference ) an assoc array of name/value pairs
-   * @param array $defaults (reference ) an assoc array to hold the flattened values
+   * @param array $params
+   *   (reference ) an assoc array of name/value pairs.
+   * @param array $defaults
+   *   (reference ) an assoc array to hold the flattened values.
    *
-     * @return CRM_Financial_DAO_PaymentProcessor object on success, null otherwise
+   * @return CRM_Financial_DAO_PaymentProcessor object on success, null otherwise
    * @static
    */
   public static function retrieve(&$params, &$defaults) {
@@ -115,8 +117,10 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
   /**
    * Update the is_active flag in the db
    *
-   * @param int      $id        id of the database record
-   * @param boolean  $is_active value we want to set the is_active field
+   * @param int $id
+   *   Id of the database record.
+   * @param bool $is_active
+   *   Value we want to set the is_active field.
    *
    * @return Object             DAO object on sucess, null otherwise
    *
@@ -174,8 +178,10 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
   /**
    * Get the payment processor details
    *
-   * @param  int    $paymentProcessorID payment processor id
-   * @param  string $mode               payment mode ie test or live
+   * @param int $paymentProcessorID
+   *   Payment processor id.
+   * @param string $mode
+   *   Payment mode ie test or live.
    *
    * @return array  associated array with payment processor related fields
    * @static
@@ -185,7 +191,7 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
       CRM_Core_Error::fatal(ts('Invalid value passed to getPayment function'));
     }
 
-    $dao            = new CRM_Financial_DAO_PaymentProcessor( );
+    $dao            = new CRM_Financial_DAO_PaymentProcessor();
     $dao->id        = $paymentProcessorID;
     $dao->is_active = 1;
     if (!$dao->find(TRUE)) {
@@ -193,7 +199,7 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
     }
 
     if ($mode == 'test') {
-      $testDAO = new CRM_Financial_DAO_PaymentProcessor( );
+      $testDAO = new CRM_Financial_DAO_PaymentProcessor();
       $testDAO->name      = $dao->name;
       $testDAO->is_active = 1;
       $testDAO->is_test   = 1;
@@ -219,7 +225,7 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
       CRM_Core_Error::fatal(ts('Invalid value passed to getPayment function'));
     }
 
-    $payments = array( );
+    $payments = array();
     foreach ($paymentProcessorIDs as $paymentProcessorID) {
       $payment = self::getPayment($paymentProcessorID, $mode);
       $payments[$payment['id']] = $payment;
@@ -236,20 +242,22 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
    * @param array_type $processor2
    * @return number
    */
-    public static function defaultComparison($processor1, $processor2){
-      $p1 = CRM_Utils_Array::value('is_default', $processor1);
-      $p2 = CRM_Utils_Array::value('is_default', $processor2);
-      if ($p1 == $p2) {
-        return 0;
-      }
-      return ($p1 > $p2) ? -1 : 1;
+  public static function defaultComparison($processor1, $processor2) {
+    $p1 = CRM_Utils_Array::value('is_default', $processor1);
+    $p2 = CRM_Utils_Array::value('is_default', $processor2);
+    if ($p1 == $p2) {
+      return 0;
     }
+    return ($p1 > $p2) ? -1 : 1;
+  }
 
   /**
    * Build payment processor details
    *
-   * @param object $dao   payment processor object
-   * @param  string $mode payment mode ie test or live
+   * @param object $dao
+   *   Payment processor object.
+   * @param string $mode
+   *   Payment mode ie test or live.
    *
    * @return array  associated array with payment processor related fields
    * @static
@@ -275,30 +283,42 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
   /**
    * Get all payment processors as an array of objects.
    *
-   * @param $isExcludeTest
+   * @param string|NULL $mode
+   * only return this mode - test|live or NULL for all
    * @param bool $reset
    *
    * @throws CiviCRM_API3_Exception
    * @return array
    */
-  public static function getAllPaymentProcessors($isExcludeTest, $reset = FALSE) {
-    /**
-     * $cacheKey = 'CRM_Financial_BAO_Payment_Processor_' . ($isExcludeTest ? 'test' : 'all');
-    if (!$reset) {
-      $processors = CRM_Utils_Cache::singleton()->get($cacheKey);
-      if (!empty($processors)) {
-        return $processors;
-      }
-    }
+  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;
+     }
+   }
      * */
     $retrievalParameters = array('is_active' => TRUE, 'options' => array('sort' => 'is_default DESC, name'), 'api.payment_processor_type.getsingle' => 1);
-    if ($isExcludeTest) {
+    if ($mode == 'test') {
+      $retrievalParameters['is_test'] = 1;
+    }
+    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');
+      foreach ($fieldsToProvide as $field) {
+        //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'];
-      $processors['values'][$processor['id']]['object'] = CRM_Core_Payment::singleton(empty($processor['is_test']) ? 'live' : 'test', $processor);
+      $mode = empty($processor['is_test']) ? 'live' : 'test';
+      $processors['values'][$processor['id']]['object'] = CRM_Core_Payment::singleton($mode, $processor);
     }
     /*
      CRM_Utils_Cache::singleton()->set($cacheKey, $processors);
@@ -312,14 +332,26 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
    * arguably this could go on the pseudoconstant class
    *
    * @param array $capabilities
-   * @param bool $isIncludeTest
+   * capabilities of processor e.g
+   *  - BackOffice
+   *  - TestMode
+   *  - LiveMode
+   *  - FutureStartDate
+   *  include test processors (we want to phase this out in favour of the testMode Capability)
    *
    * @param array $ids
    *
    * @return array available processors
    */
-  public static function getPaymentProcessors($capabilities = array(), $isIncludeTest = FALSE, $ids = array()) {
-    $processors = self::getAllPaymentProcessors(!$isIncludeTest);
+  public static function getPaymentProcessors($capabilities = array(), $ids = array()) {
+    $mode = NULL;
+    if (in_array('TestMode', $capabilities)) {
+      $mode = 'test';
+    }
+    elseif (in_array('LiveMode', $capabilities)) {
+      $mode = 'live';
+    }
+    $processors = self::getAllPaymentProcessors($mode);
     if ($capabilities) {
       foreach ($processors as $index => $processor) {
         if (!empty($ids) && !in_array($processor['id'], $ids)) {
@@ -348,7 +380,9 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
    * @return bool
    */
   public static function hasPaymentProcessorSupporting($capabilities = array(), $isIncludeTest = FALSE) {
-    $result = self::getPaymentProcessors($capabilities, $isIncludeTest);
+    $mode = $isIncludeTest ? 'Test' : 'Live';
+    $capabilities[] = $mode . 'Mode';
+    $result = self::getPaymentProcessors($capabilities);
     return (!empty($result)) ? TRUE : FALSE;
   }
 
@@ -356,8 +390,10 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces
    * Retrieve payment processor id / info/ object based on component-id.
    *
    * @param int $entityID
-   * @param string $component component
-   * @param string $type type of payment information to be retrieved
+   * @param string $component
+   *   Component.
+   * @param string $type
+   *   Type of payment information to be retrieved.
    *
    * @return int / array / object based on type
    * @static
@@ -422,4 +458,3 @@ INNER JOIN civicrm_contribution       con ON ( mp.contribution_id = con.id )
     return $result;
   }
 }
-