[REF] Refactor adding payment processor radio section onto register and contribution...
authorSeamus Lee <seamuslee001@gmail.com>
Wed, 19 Feb 2020 21:13:43 +0000 (08:13 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 20 Feb 2020 20:32:21 +0000 (07:32 +1100)
Move Contribute form specific handling back to its own form

Move shared function to the trait

CRM/Contribute/Form/Contribution/Main.php
CRM/Event/Form/Registration/Register.php
CRM/Financial/Form/FrontEndPaymentFormTrait.php

index 2c6252061001d978d2fc739ab7e02e19ff341847..09524cef5c8c5e83046ab2943d8865c174e58055 100644 (file)
@@ -320,25 +320,11 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
       $this->add('text', 'total_amount', ts('Total Amount'), ['readonly' => TRUE], FALSE);
     }
     $pps = $this->getProcessors();
-    $optAttributes = [];
-    foreach ($pps as $ppKey => $ppval) {
-      if ($ppKey > 0) {
-        $optAttributes[$ppKey]['class'] = 'payment_processor_' . strtolower($this->_paymentProcessors[$ppKey]['payment_processor_type']);
-      }
-      else {
-        $optAttributes[$ppKey]['class'] = 'payment_processor_paylater';
-      }
-    }
-    if (count($pps) > 1) {
-      $this->addRadio('payment_processor_id', ts('Payment Method'), $pps,
-        NULL, "&nbsp;", FALSE, $optAttributes
-      );
-    }
-    elseif (!empty($pps)) {
-      $key = array_keys($pps);
-      $key = array_pop($key);
-      $this->addElement('hidden', 'payment_processor_id', $key);
-      if ($key === 0) {
+    $this->addPaymentProcessorFieldsToForm();
+    if (!empty($pps) && count($pps) === 1) {
+      $ppKeys = array_keys($pps);
+      $currentPP = array_pop($ppKeys);
+      if ($currentPP === 0) {
         $this->assign('is_pay_later', $this->_values['is_pay_later']);
         $this->assign('pay_later_text', $this->getPayLaterLabel());
       }
index a9020cec1b5e617db18b6afa575f930457acf0b7..2e2ad47610257e72c35fa8962be16170fbae8cca 100644 (file)
@@ -398,25 +398,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
 
     if ($this->_values['event']['is_monetary']) {
-      $optAttributes = [];
-      foreach ($pps as $ppKey => $ppval) {
-        if ($ppKey > 0) {
-          $optAttributes[$ppKey]['class'] = 'payment_processor_' . strtolower($this->_paymentProcessors[$ppKey]['payment_processor_type']);
-        }
-        else {
-          $optAttributes[$ppKey]['class'] = 'payment_processor_paylater';
-        }
-      }
-      if (count($pps) > 1) {
-        $this->addRadio('payment_processor_id', ts('Payment Method'), $pps,
-          NULL, "&nbsp;", FALSE, $optAttributes
-        );
-      }
-      elseif (!empty($pps)) {
-        $ppKeys = array_keys($pps);
-        $currentPP = array_pop($ppKeys);
-        $this->addElement('hidden', 'payment_processor_id', $currentPP);
-      }
+      $this->addPaymentProcessorFieldsToForm();
     }
 
     $this->addElement('hidden', 'bypass_payment', NULL, ['id' => 'bypass_payment']);
index ca5ef91949ff200b14f30da237e955051fd2738f..280ff492b8fe82b2ed29d4c57f42e1211c6f379a 100644 (file)
@@ -106,4 +106,30 @@ trait CRM_Financial_Form_FrontEndPaymentFormTrait {
     return $pps;
   }
 
+  /**
+   * Adds in either a set of radio buttons or hidden fields to contain the payment processors on a front end form
+   */
+  protected function addPaymentProcessorFieldsToForm() {
+    $paymentProcessors = $this->getProcessors();
+    $optAttributes = [];
+    foreach ($pamymentProcessors as $ppKey => $ppval) {
+      if ($ppKey > 0) {
+        $optAttributes[$ppKey]['class'] = 'payment_processor_' . strtolower($this->_paymentProcessors[$ppKey]['payment_processor_type']);
+      }
+      else {
+        $optAttributes[$ppKey]['class'] = 'payment_processor_paylater';
+      }
+    }
+    if (count($paymentProcessors) > 1) {
+      $this->addRadio('payment_processor_id', ts('Payment Method'), $paymentProcessors,
+        NULL, "&nbsp;", FALSE, $optAttributes
+      );
+    }
+    elseif (!empty($paymentProcessorss)) {
+      $ppKeys = array_keys($paymentProcessors);
+      $currentPP = array_pop($ppKeys);
+      $this->addElement('hidden', 'payment_processor_id', $currentPP);
+    }
+  }
+
 }