From bf7ae8132f687f04138aa260c30cd5efc3dfc750 Mon Sep 17 00:00:00 2001 From: eileen <emcnaughton@wikimedia.org> Date: Sat, 26 Oct 2019 16:57:02 +1300 Subject: [PATCH] dev/financial#2 Support payment processor title if configured on Contribution Pages Per https://lab.civicrm.org/dev/financial/issues/2 we agreed & added a 'title' field to the civicrm_payment_processor table some time ago for exposure on front end forms. We haven't actually done this, or made it UI configurable. This fixes that for contribution pages & adds comments pointing out the tidy up needed to make it similarly available on the events page. Once done we should add to the payment processor form --- CRM/Contribute/Form/Contribution/Main.php | 12 +----- CRM/Contribute/Form/ContributionBase.php | 3 ++ CRM/Event/Form/Registration/Register.php | 16 ++++--- .../Form/FrontEndPaymentFormTrait.php | 43 +++++++++++++++++++ .../CRM/Contribute/Form/ContributionTest.php | 4 ++ tests/phpunit/api/v3/ContributionPageTest.php | 3 ++ 6 files changed, 65 insertions(+), 16 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index 69fc0192cb7..e80359561ae 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -335,15 +335,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $this->addElement('hidden', "email-{$this->_bltID}", 1); $this->add('text', 'total_amount', ts('Total Amount'), ['readonly' => TRUE], FALSE); } - $pps = []; - if (!empty($this->_paymentProcessors)) { - foreach ($this->_paymentProcessors as $key => $name) { - $pps[$key] = $name['name']; - } - } - if (!empty($this->_values['is_pay_later'])) { - $pps[0] = $this->_values['pay_later_text']; - } + $pps = $this->getProcessors(); if (count($pps) > 1) { $this->addRadio('payment_processor_id', ts('Payment Method'), $pps, @@ -356,7 +348,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $this->addElement('hidden', 'payment_processor_id', $key); if ($key === 0) { $this->assign('is_pay_later', $this->_values['is_pay_later']); - $this->assign('pay_later_text', $this->_values['pay_later_text']); + $this->assign('pay_later_text', $this->getPayLaterLabel()); } } diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index 0584c055b0c..f548666ada0 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -344,6 +344,9 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { $this->_values['is_pay_later'] = FALSE; } } + if ($isPayLater) { + $this->setPayLaterLabel($this->_values['pay_later_text']); + } if ($isMonetary) { $this->_paymentProcessorIDs = array_filter(explode( diff --git a/CRM/Event/Form/Registration/Register.php b/CRM/Event/Form/Registration/Register.php index ac1388a35b3..3d3c748cc83 100644 --- a/CRM/Event/Form/Registration/Register.php +++ b/CRM/Event/Form/Registration/Register.php @@ -411,22 +411,26 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { } $pps = []; - //@todo this processor adding fn is another one duplicated on contribute - a shared - // common class would make this sort of thing extractable + //@todo this could call the function on FrontEndPaymentTrait which also respects the payment processor + // title if configured. From my testing we don't need the 'if' around adding payment processor 0 + // (below) - if someone else tests & confirms we can remove it, use the FrontEndPaymentTrait + // like the contribution page does & use the getPaymentLabel (provided we also set it) + // and we will get the title, if configured. if (!empty($this->_paymentProcessors)) { foreach ($this->_paymentProcessors as $key => $name) { $pps[$key] = $name['name']; } } - if ($this->getContactID() === 0 && !$this->_values['event']['is_multiple_registrations']) { - //@todo we are blocking for multiple registrations because we haven't tested - $this->addCIDZeroOptions(); - } + // see @todo above if (!empty($this->_values['event']['is_pay_later']) && ($this->_allowConfirmation || (!$this->_requireApproval && !$this->_allowWaitlist)) ) { $pps[0] = $this->_values['event']['pay_later_text']; } + if ($this->getContactID() === 0 && !$this->_values['event']['is_multiple_registrations']) { + //@todo we are blocking for multiple registrations because we haven't tested + $this->addCIDZeroOptions(); + } if ($this->_values['event']['is_monetary']) { if (count($pps) > 1) { diff --git a/CRM/Financial/Form/FrontEndPaymentFormTrait.php b/CRM/Financial/Form/FrontEndPaymentFormTrait.php index 7380cdd857b..30f9b0d0cb0 100644 --- a/CRM/Financial/Form/FrontEndPaymentFormTrait.php +++ b/CRM/Financial/Form/FrontEndPaymentFormTrait.php @@ -36,6 +36,31 @@ */ trait CRM_Financial_Form_FrontEndPaymentFormTrait { + /** + * The label for the pay later pseudoprocessor option. + * + * @var string + */ + protected $payLaterLabel; + + /** + * @return string + */ + public function getPayLaterLabel(): string { + if ($this->payLaterLabel) { + return $this->payLaterLabel; + } + return $this->get('payLaterLabel') ?? ''; + } + + /** + * @param string $payLaterLabel + */ + public function setPayLaterLabel(string $payLaterLabel) { + $this->set('payLaterLabel', $payLaterLabel); + $this->payLaterLabel = $payLaterLabel; + } + /** * Alter line items for template. * @@ -79,4 +104,22 @@ trait CRM_Financial_Form_FrontEndPaymentFormTrait { $this->assign('lineItem', $tplLineItems); } + /** + * Get the configured processors, including the pay later processor. + * + * @return array + */ + protected function getProcessors(): array { + $pps = []; + if (!empty($this->_paymentProcessors)) { + foreach ($this->_paymentProcessors as $key => $processor) { + $pps[$key] = $processor['title'] ?? $processor['name']; + } + } + if ($this->getPayLaterLabel()) { + $pps[0] = $this->getPayLaterLabel(); + } + return $pps; + } + } diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php index a66eb2cefd0..5ca110720db 100644 --- a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php @@ -1315,6 +1315,9 @@ Price Field - Price Field 1 1 $ 100.00 $ 100.00 /** * Check payment processor is correctly assigned for a contribution page. + * + * @throws \CRM_Core_Exception + * @throws \CRM_Contribute_Exception_InactiveContributionPageException */ public function testContributionBasePreProcess() { //Create contribution page with only pay later enabled. @@ -1324,6 +1327,7 @@ Price Field - Price Field 1 1 $ 100.00 $ 100.00 'currency' => 'NZD', 'goal_amount' => 100, 'is_pay_later' => 1, + 'pay_later_text' => 'Send check', 'is_monetary' => TRUE, 'is_active' => TRUE, 'is_email_receipt' => TRUE, diff --git a/tests/phpunit/api/v3/ContributionPageTest.php b/tests/phpunit/api/v3/ContributionPageTest.php index 5521a1bba39..0aca68c71eb 100644 --- a/tests/phpunit/api/v3/ContributionPageTest.php +++ b/tests/phpunit/api/v3/ContributionPageTest.php @@ -68,6 +68,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { 'currency' => 'NZD', 'goal_amount' => $this->testAmount, 'is_pay_later' => 1, + 'pay_later_text' => 'Send check', 'is_monetary' => TRUE, 'is_email_receipt' => TRUE, 'receipt_from_email' => 'yourconscience@donate.com', @@ -1932,6 +1933,8 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { /** * Test validating a contribution page submit. + * + * @throws \CRM_Core_Exception */ public function testValidate() { $this->setUpContributionPage(); -- GitLab