move definition of payment fields to the processor
authorEileen McNaughton <eileen@fuzion.co.nz>
Fri, 31 Oct 2014 21:39:52 +0000 (10:39 +1300)
committerEileen McNaughton <eileen@fuzion.co.nz>
Fri, 31 Oct 2014 21:39:52 +0000 (10:39 +1300)
CRM/Contribute/Form/AbstractEditPayment.php
CRM/Contribute/Form/Contribution.php
CRM/Core/Config.php
CRM/Core/Payment.php
CRM/Core/Payment/Form.php

index 25d9469ec7aa60b6cb386d9d750a6df0dcb98caf..554a9eeee8c744a77416e312fad931e0753fd6da 100644 (file)
@@ -355,30 +355,21 @@ LEFT JOIN  civicrm_contribution on (civicrm_contribution.contact_id = civicrm_co
       $capabilities[] = 'LiveMode';
     }
     $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors($capabilities);
-    foreach ($processors as $id => $processor) {
-       if ($processor['is_default']) {
-         $defaultID = $id;
-       }
-       $validProcessors[$id] = ts($processor['name']);
-    }
-    if (empty($validProcessors)) {
-      throw new CRM_Core_Exception(ts('You will need to configure the %1 settings for your Payment Processor before you can submit a credit card transactions.', array(1 => $this->_mode)));
-    }
-    else {
-      return array($validProcessors, $processors[$defaultID]['object']);
-    }
+    return $processors;
+
   }
 
   /**
    * Assign billing type id to bltID
    *
+   * @throws CRM_Core_Exception
    * @return void
    */
   public function assignBillingType() {
     $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
     $this->_bltID = array_search('Billing', $locationTypes);
     if (!$this->_bltID) {
-      CRM_Core_Error::fatal(ts('Please set a location type of %1', array(1 => 'Billing')));
+      throw new CRM_Core_Exception(ts('Please set a location type of %1', array(1 => 'Billing')));
     }
     $this->set('bltID', $this->_bltID);
     $this->assign('bltID', $this->_bltID);
@@ -390,10 +381,17 @@ LEFT JOIN  civicrm_contribution on (civicrm_contribution.contact_id = civicrm_co
   public function assignProcessors() {
     //ensure that processor has a valid config
     //only valid processors get display to user
+
     if ($this->_mode) {
       $this->assign(CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(array('supportsFutureRecurStartDate')), TRUE);
-      list($this->_processors, $paymentProcessor) = $this->getValidProcessors();
-
+      $processors = $this->getValidProcessors();
+      if (empty($processors)) {
+        throw new CRM_Core_Exception(ts('You will need to configure the %1 settings for your Payment Processor before you can submit a credit card transactions.', array(1 => $this->_mode)));
+      }
+      $this->_processors  = array();
+      foreach ($processors as $id => $processor) {
+        $this->_processors[$id] = ts($processor['name']);
+      }
       //get the valid recurring processors.
       $recurring = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
       $this->_recurPaymentProcessors = array_intersect_assoc($this->_processors, $recurring);
@@ -404,7 +402,7 @@ LEFT JOIN  civicrm_contribution on (civicrm_contribution.contact_id = civicrm_co
 
     // this required to show billing block
     // @todo remove this assignment the billing block is now designed to be always included but will not show fieldsets unless those sets of fields are assigned
-    $this->assign_by_ref('paymentProcessor', $paymentProcessor);
+    $this->assign_by_ref('paymentProcessor', $processor);
   }
 
   /**
index eb3a6d5ed5695c4b40fc8718ac88d1c0f7ca0ed1..cca070f75773a0d37e1da183230990bad9721939 100644 (file)
@@ -228,8 +228,8 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       CRM_Core_Error::fatal(ts('You do not have permission to access this page'));
     }
 
+    //@todo - if anyone ever figures out what this cdtype subroutine is about (or even if it still applies) please add comments
     $this->_cdType = CRM_Utils_Array::value('type', $_GET);
-
     $this->assign('cdType', FALSE);
     if ($this->_cdType) {
       $this->assign('cdType', TRUE);
@@ -277,21 +277,21 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
     $this->assign('showCheckNumber', TRUE);
 
     $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail();
-    $this->assignProcessors();
-
-    if ($this->_contactID) {
-      list($this->userDisplayName, $this->userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
-      $this->assign('displayName', $this->userDisplayName);
-    }
-
-    // also check for billing information
-    // get the billing location type
-    $this->assignBillingType();
-
-    $this->_fields = array();
+    try {
+      $this->assignProcessors();
+      if ($this->_contactID) {
+        list($this->userDisplayName, $this->userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
+        $this->assign('displayName', $this->userDisplayName);
+      }
 
-    CRM_Core_Payment_Form::setPaymentFieldsByType(CRM_Utils_Array::value('payment_type', $this->_processors), $this);
+      $this->assignBillingType();
 
+      $this->_fields = array();
+      CRM_Core_Payment_Form::setPaymentFieldsByType(CRM_Utils_Array::value('payment_type', $this->_processors), $this);
+    }
+    catch (CRM_Core_Exception $e) {
+      CRM_Core_Error::fatal($e->getMessage());
+    }
     if ($this->_action & CRM_Core_Action::DELETE) {
       return;
     }
index d47da2e48967692982692b7e17cb13305eb670ed..8a4ed0df0e48961cc08ee17b64b2407c29cc2036 100644 (file)
@@ -866,6 +866,6 @@ AND
    * @return bool
    */
   static function isEnabledBackOfficeCreditCardPayments() {
-    return CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting('BackOffice');
+    return CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(array('BackOffice'));
   }
 }
index b9ee08d5740edf0c5bb728122caa988a6551539a..b865aea8bae24087321c3bbe3f2555553c488e64 100644 (file)
@@ -211,6 +211,46 @@ abstract class CRM_Core_Payment {
     return isset($this->$name) ? $this->$name : NULL;
   }
 
+  /**
+   * get array of fields that should be displayed on the payment form
+   * @todo make payment type an option value & use it in the function name - currently on debit & credit card work
+   * @return array
+   * @throws CiviCRM_API3_Exception
+   */
+  public function getPaymentFormFields() {
+    if ($this->_paymentProcessor['payment_type'] == 4) {
+      return array();
+    }
+    return $this->_paymentProcessor['payment_type'] == 1 ? $this->getCreditCardFormFields() : $this->getDirectDebitFormFields();
+  }
+
+  /**
+   * get array of fields that should be displayed on the payment form for credit cards
+   * @return array
+   */
+  protected function getCreditCardFormFields() {
+    return array(
+      'credit_card_type',
+      'credit_card_number',
+      'cvv2',
+      'credit_card_exp_date',
+    );
+  }
+
+  /**
+   * get array of fields that should be displayed on the payment form for direct debits
+   * @return array
+   */
+  protected function getDirectDebitFormFields() {
+    return array(
+      'account_holder',
+      'bank_account_number',
+      'bank_identification_number',
+      'bank_name',
+    );
+  }
+
+
   /**
    * This function collects all the information from a web/api form and invokes
    * the relevant payment processor specific functions to perform the transaction
index e3be88183e429e79cfc75a26d706ab89f714498e..b7074407e8fc7a636d3969fad2d8c8459946f6e2 100644 (file)
@@ -202,12 +202,7 @@ class CRM_Core_Payment_Form {
     $smarty = CRM_Core_Smarty::singleton();
     $smarty->assign('paymentTypeName', 'credit_card');
     $smarty->assign('paymentTypeLabel', ts('Credit Card Information'));
-    $smarty->assign('paymentFields', array(
-      'credit_card_type',
-      'credit_card_number',
-      'cvv2',
-      'credit_card_exp_date',
-    ));
+    $smarty->assign('paymentFields', self::getPaymentFields($form->_paymentProcessor));
   }
 
   /**
@@ -286,12 +281,36 @@ class CRM_Core_Payment_Form {
     // replace these payment type names with an option group - moving name & label assumptions out of the tpl is a step towards that
     $smarty->assign('paymentTypeName', 'direct_debit');
     $smarty->assign('paymentTypeLabel', ts('Direct Debit Information'));
-    $smarty->assign('paymentFields', array(
-      'account_holder',
-      'bank_account_number',
-      'bank_identification_number',
-      'bank_name',
-    ));
+    $smarty->assign('paymentFields', self::getPaymentFields($form->_paymentProcessor));
+  }
+
+  /**
+   * @param array $paymentProcessor
+   * @todo it may be necessary to set details that affect it - mostly likely take Country as a param
+   *
+   * @return mixed
+   */
+  static function getPaymentFields($paymentProcessor) {
+    return array();
+    $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor);
+    return $paymentProcessorObject->getPaymentFields();
+  }
+
+  /**
+   * @param $form
+   *
+   * @param array $paymentProcessor
+   * @todo it may be necessary to set details that affect it - mostly likely take Country as a param
+   *
+   * @return mixed
+   */
+  protected static function setPaymentFields(&$form, $paymentProcessor) {
+    $fields = self::getPaymentFields($paymentProcessor);
+
+  }
+
+  static function buildPaymentForm(&$form, $useRequired = FALSE) {
+
   }
 
   /**