From 44b6505d5c061003fbc25cb1204d3c0f6c5b26d3 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 1 Nov 2014 10:39:52 +1300 Subject: [PATCH] move definition of payment fields to the processor --- CRM/Contribute/Form/AbstractEditPayment.php | 30 +++++++------- CRM/Contribute/Form/Contribution.php | 28 +++++++------- CRM/Core/Config.php | 2 +- CRM/Core/Payment.php | 40 +++++++++++++++++++ CRM/Core/Payment/Form.php | 43 +++++++++++++++------ 5 files changed, 100 insertions(+), 43 deletions(-) diff --git a/CRM/Contribute/Form/AbstractEditPayment.php b/CRM/Contribute/Form/AbstractEditPayment.php index 25d9469ec7..554a9eeee8 100644 --- a/CRM/Contribute/Form/AbstractEditPayment.php +++ b/CRM/Contribute/Form/AbstractEditPayment.php @@ -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); } /** diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index eb3a6d5ed5..cca070f757 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -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; } diff --git a/CRM/Core/Config.php b/CRM/Core/Config.php index d47da2e489..8a4ed0df0e 100644 --- a/CRM/Core/Config.php +++ b/CRM/Core/Config.php @@ -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')); } } diff --git a/CRM/Core/Payment.php b/CRM/Core/Payment.php index b9ee08d574..b865aea8ba 100644 --- a/CRM/Core/Payment.php +++ b/CRM/Core/Payment.php @@ -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 diff --git a/CRM/Core/Payment/Form.php b/CRM/Core/Payment/Form.php index e3be88183e..b7074407e8 100644 --- a/CRM/Core/Payment/Form.php +++ b/CRM/Core/Payment/Form.php @@ -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) { + } /** -- 2.25.1