PHp8.x notice fix - remove use of legacy paymentObject
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 3 Nov 2023 02:23:03 +0000 (15:23 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 3 Nov 2023 03:10:27 +0000 (16:10 +1300)
CRM/Contribute/Form/AbstractEditPayment.php
CRM/Contribute/Form/Contribution.php
CRM/Contribute/Form/Contribution/Main.php
CRM/Contribute/Form/ContributionBase.php
CRM/Core/Form.php
CRM/Core/Payment/ProcessorForm.php
CRM/Event/Form/Registration/Register.php
CRM/Financial/Form/PaymentProcessorFormTrait.php [new file with mode: 0644]
ext/eventcart/CRM/Event/Cart/Form/Checkout/Payment.php

index 6b9b5472ef3c8ff348d043a749b5bed4305130d8..aa378a4ec4e99718d0a265441859348e6978e6e8 100644 (file)
@@ -70,13 +70,6 @@ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task {
    */
   protected $_paymentProcessors = [];
 
-  /**
-   * Instance of the payment processor object.
-   *
-   * @var CRM_Core_Payment
-   */
-  protected $_paymentObject;
-
   /**
    * Entity that $this->_id relates to.
    *
index 8227fb63af04f0aa09618459818a45de0fdecfb1..7edb7b92d67de67867cb0a892f2da0fd02148a28 100644 (file)
@@ -19,6 +19,7 @@ use Civi\Payment\Exception\PaymentProcessorException;
 class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditPayment {
   use CRM_Contact_Form_ContactFormTrait;
   use CRM_Contribute_Form_ContributeFormTrait;
+  use CRM_Financial_Form_PaymentProcessorFormTrait;
 
   /**
    * The id of the contribution that we are processing.
@@ -1154,8 +1155,8 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       $this->_lineItem = $lineItem;
     }
 
-    $this->_paymentObject = Civi\Payment\System::singleton()->getById($submittedValues['payment_processor_id']);
-    $this->_paymentProcessor = $this->_paymentObject->getPaymentProcessor();
+    $paymentObject = Civi\Payment\System::singleton()->getById($submittedValues['payment_processor_id']);
+    $this->_paymentProcessor = $paymentObject->getPaymentProcessor();
 
     // Set source if not set
     if (empty($submittedValues['source'])) {
@@ -2363,7 +2364,7 @@ WHERE  contribution_id = {$id}
 
     $form->assign('is_recur_interval', $form->_values['is_recur_interval'] ?? NULL);
     $form->assign('is_recur_installments', $form->_values['is_recur_installments'] ?? NULL);
-    $paymentObject = $form->getVar('_paymentObject');
+    $paymentObject = $this->getPaymentProcessorObject();
     if ($paymentObject) {
       $form->assign('recurringHelpText', $paymentObject->getText('contributionPageRecurringHelp', [
         'is_recur_installments' => !empty($form->_values['is_recur_installments']),
index 5f7d9aa04ab010ae639cde16e0a0b093d03d0f39..d213af0d618518aae23c35e3adc3f0139fce390b 100644 (file)
@@ -747,7 +747,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
 
     $form->assign('is_recur_interval', $this->getContributionPageValue('is_recur_interval'));
     $form->assign('is_recur_installments', $this->getContributionPageValue('is_recur_installments'));
-    $paymentObject = $form->getVar('_paymentObject');
+    $paymentObject = $this->_paymentProcessor['object'];
     if ($paymentObject) {
       $form->assign('recurringHelpText', $paymentObject->getText('contributionPageRecurringHelp', [
         'is_recur_installments' => !empty($form->_values['is_recur_installments']),
index 46fb52bf9d64c165eaaf130a97af9e01cf2aa833..f1f4a242cd8a0eadf361804f6db8dab083fd4757 100644 (file)
@@ -23,6 +23,7 @@ use Civi\Api4\PriceSet;
 class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
   use CRM_Financial_Form_FrontEndPaymentFormTrait;
   use CRM_Contribute_Form_ContributeFormTrait;
+  use CRM_Financial_Form_PaymentProcessorFormTrait;
 
   /**
    * The id of the contribution page that we are processing.
@@ -60,8 +61,6 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    */
   public $_paymentProcessor;
 
-  public $_paymentObject = NULL;
-
   /**
    * Order object, used to calculate amounts, line items etc.
    *
@@ -869,8 +868,8 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
 
     // The concept of contributeMode is deprecated.
     // The payment processor object can provide info about the fields it shows.
-    if ($isMonetary && $this->_paymentProcessor['object'] instanceof \CRM_Core_Payment) {
-      $paymentProcessorObject = $this->_paymentProcessor['object'];
+    if ($isMonetary) {
+      $paymentProcessorObject = $this->getPaymentProcessorObject();
       $this->assign('paymentAgreementTitle', $paymentProcessorObject->getText('agreementTitle', []));
       $this->assign('paymentAgreementText', $paymentProcessorObject->getText('agreementText', []));
       $paymentFields = $paymentProcessorObject->getPaymentFormFields();
@@ -1055,18 +1054,6 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     }
   }
 
-  /**
-   * Get the payment processor object for the submission, returning the manual one for offline payments.
-   *
-   * @return CRM_Core_Payment
-   */
-  protected function getPaymentProcessorObject() {
-    if (!empty($this->_paymentProcessor)) {
-      return $this->_paymentProcessor['object'];
-    }
-    return new CRM_Core_Payment_Manual();
-  }
-
   /**
    * Get the amount for the main contribution.
    *
index 85e98bc7585cca802eac8449c2ea3a73d275c54c..3b46f59f0d076041035d4f17a4329caf6755ec1f 100644 (file)
@@ -945,8 +945,6 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
         ) {
           $this->_paymentProcessor = $paymentProcessorDetail;
           $this->assign('paymentProcessor', $this->_paymentProcessor);
-          // Setting this is a bit of a legacy overhang.
-          $this->_paymentObject = $paymentProcessorDetail['object'];
         }
       }
       // It's not clear why we set this on the form.
index 381090f7aa93fc56926131dbd2a9ea3bbcc1baaf..35715f69db68e5f1208e6df4b3790a70da3b5063 100644 (file)
@@ -42,14 +42,14 @@ class CRM_Core_Payment_ProcessorForm {
       return;
     }
     $form->set('paymentProcessor', $form->_paymentProcessor);
-    $form->_paymentObject = System::singleton()->getByProcessor($form->_paymentProcessor);
+    $paymentObject = System::singleton()->getByProcessor($form->_paymentProcessor);
     if ($form->paymentInstrumentID) {
-      $form->_paymentObject->setPaymentInstrumentID($form->paymentInstrumentID);
+      $paymentObject->setPaymentInstrumentID($form->paymentInstrumentID);
     }
-    $form->_paymentObject->setBackOffice($form->isBackOffice);
+    $paymentObject->setBackOffice($form->isBackOffice);
     $form->assign('isBackOffice', $form->isBackOffice);
 
-    $form->assign('suppressSubmitButton', $form->_paymentObject->isSuppressSubmitButtons());
+    $form->assign('suppressSubmitButton', $paymentObject->isSuppressSubmitButtons());
 
     CRM_Financial_Form_Payment::addCreditCardJs($form->getPaymentProcessorID());
     $form->assign('paymentProcessorID', $form->getPaymentProcessorID());
@@ -61,7 +61,7 @@ class CRM_Core_Payment_ProcessorForm {
 
     // also set cancel subscription url
     if (!empty($form->_paymentProcessor['is_recur']) && !empty($form->_values['is_recur'])) {
-      $form->_values['cancelSubscriptionUrl'] = $form->_paymentObject->subscriptionURL(NULL, NULL, 'cancel');
+      $form->_values['cancelSubscriptionUrl'] = $paymentObject->subscriptionURL(NULL, NULL, 'cancel');
     }
 
     $paymentProcessorBillingFields = array_keys($form->_paymentProcessor['object']->getBillingAddressFields());
@@ -110,7 +110,7 @@ class CRM_Core_Payment_ProcessorForm {
 
     if (!empty($form->_membershipBlock) && !empty($form->_membershipBlock['is_separate_payment']) &&
       (!empty($form->_paymentProcessor['class_name']) &&
-        !$form->_paymentObject->supports('MultipleConcurrentPayments')
+        !$paymentObject->supports('MultipleConcurrentPayments')
       )
     ) {
 
index a3614cfba5216d44338b05a9050360caf47969e6..971b0cd269d9593288b3294591beb7e9d55e852d 100644 (file)
@@ -18,6 +18,7 @@
  * This class generates form components for processing Event.
  */
 class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
+  use CRM_Financial_Form_PaymentProcessorFormTrait;
 
   /**
    * The fields involved in this page.
diff --git a/CRM/Financial/Form/PaymentProcessorFormTrait.php b/CRM/Financial/Form/PaymentProcessorFormTrait.php
new file mode 100644 (file)
index 0000000..8a6451e
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+/**
+ * Trait implements functions to retrieve payment processor related values.
+ *
+ * Note that any functions on this class that are supported to be used from
+ * outside of core are specifically tagged.
+ */
+trait CRM_Financial_Form_PaymentProcessorFormTrait {
+
+  /**
+   * Get the payment processors that are available on the form.
+   *
+   * @return array
+   * @throws \CRM_Core_Exception
+   */
+  protected function getAvailablePaymentProcessors(): array {
+    return CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors([ucfirst($this->getPaymentProcessorMode()) . 'Mode'], $this->getAvailablePaymentProcessorIDS());
+  }
+
+  /**
+   * Get the payment processor IDs available on the form.
+   *
+   * @return false|array
+   */
+  protected function getAvailablePaymentProcessorIDS() {
+    return FALSE;
+  }
+
+  /**
+   * Get the mode (test or live) of the payment processor.
+   *
+   * @api This function will not change in a minor release and is supported for
+   * use outside of core. This annotation / external support for properties
+   * is only given where there is specific test cover.
+   *
+   * @return string|null
+   *   test or live
+   * @throws \CRM_Core_Exception
+   */
+  public function getPaymentProcessorMode(): ?string {
+    return CRM_Utils_Request::retrieve('mode', 'Alphanumeric', $this);
+  }
+
+  /**
+   * Get the payment processor object for the submission, returning the manual one for offline payments.
+   *
+   * @return CRM_Core_Payment
+   */
+  protected function getPaymentProcessorObject() {
+    if (!empty($this->_paymentProcessor)) {
+      return $this->_paymentProcessor['object'];
+    }
+    return new CRM_Core_Payment_Manual();
+  }
+
+}
index ba283646d69de66f12d24c94d4de55e30fc8b425..ed358fedce8e26fab1fbeaba29fb11d29de316d3 100644 (file)
@@ -162,8 +162,6 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
           ) {
             $this->_paymentProcessor = $paymentProcessorDetail;
             $this->assign('paymentProcessor', $this->_paymentProcessor);
-            // Setting this is a bit of a legacy overhang.
-            $this->_paymentObject = $paymentProcessorDetail['object'];
           }
         }
         // It's not clear why we set this on the form.