From 1421b010e53d7ac070773e59343a61952e29e29b Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Thu, 20 Feb 2020 08:13:43 +1100 Subject: [PATCH] [REF] Refactor adding payment processor radio section onto register and contribution main forms Move Contribute form specific handling back to its own form Move shared function to the trait --- CRM/Contribute/Form/Contribution/Main.php | 24 ++++------------- CRM/Event/Form/Registration/Register.php | 20 +------------- .../Form/FrontEndPaymentFormTrait.php | 26 +++++++++++++++++++ 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index 2c62520610..09524cef5c 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -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, " ", 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()); } diff --git a/CRM/Event/Form/Registration/Register.php b/CRM/Event/Form/Registration/Register.php index a9020cec1b..2e2ad47610 100644 --- a/CRM/Event/Form/Registration/Register.php +++ b/CRM/Event/Form/Registration/Register.php @@ -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, " ", 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']); diff --git a/CRM/Financial/Form/FrontEndPaymentFormTrait.php b/CRM/Financial/Form/FrontEndPaymentFormTrait.php index ca5ef91949..280ff492b8 100644 --- a/CRM/Financial/Form/FrontEndPaymentFormTrait.php +++ b/CRM/Financial/Form/FrontEndPaymentFormTrait.php @@ -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, " ", FALSE, $optAttributes + ); + } + elseif (!empty($paymentProcessorss)) { + $ppKeys = array_keys($paymentProcessors); + $currentPP = array_pop($ppKeys); + $this->addElement('hidden', 'payment_processor_id', $currentPP); + } + } + } -- 2.25.1