Fixes to functions to determine if isMembership, isSeparatePayment
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 22 Nov 2023 22:49:13 +0000 (11:49 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 22 Nov 2023 23:56:36 +0000 (12:56 +1300)
In the case of isSeparatePayment we have a function to determine if the form
supports separate payments - but are using this interchangeably with whether the
user has selected more than one option (if they only selected 1 there is only 1 payment)

CRM/Contribute/Form/Contribution/Confirm.php
CRM/Contribute/Form/ContributionBase.php
tests/phpunit/api/v3/ContributionPageTest.php

index b84a3e6b27dfba83909aa8415976360232fd98f4..3ea55fbdae5371ed95f656ad27c1e61011efd215 100644 (file)
@@ -1407,7 +1407,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
     $totalAmount = $membershipParams['amount'];
 
     if ($isPaidMembership) {
-      if ($isProcessSeparateMembershipTransaction) {
+      if ($this->isSeparatePaymentSelected()) {
         // If we have 2 transactions only one can use the invoice id.
         $membershipParams['invoiceID'] .= '-2';
         if (!empty($membershipParams['auto_renew'])) {
@@ -1441,7 +1441,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
       }
     }
 
-    if ($isProcessSeparateMembershipTransaction) {
+    if ($this->isSeparatePaymentSelected()) {
       try {
         $this->_lineItem = $unprocessedLineItems;
         if (empty($this->_params['auto_renew']) && !empty($membershipParams['is_recur'])) {
@@ -2201,7 +2201,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
     $this->_useForMember = $this->get('useForMember');
 
     // store the fact that this is a membership and membership type is selected
-    if ($this->isMembershipSelected($membershipParams)) {
+    if ($this->isMembershipSelected()) {
       $this->doMembershipProcessing($contactID, $membershipParams, $premiumParams);
     }
     else {
@@ -2266,20 +2266,11 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
 
   /**
    * Return True/False if we have a membership selected on the contribution page
-   * @param array $membershipParams
    *
    * @return bool
    */
-  private function isMembershipSelected($membershipParams) {
-    $priceFieldIds = $this->get('memberPriceFieldIDS');
-    if ((!empty($membershipParams['selectMembership']) && $membershipParams['selectMembership'] != 'no_thanks')
-        && empty($priceFieldIds)) {
-      return TRUE;
-    }
-    else {
-      $membershipParams = $this->getMembershipParamsFromPriceSet($membershipParams);
-    }
-    return !empty($membershipParams['selectMembership']);
+  private function isMembershipSelected(): bool {
+    return !empty($this->getMembershipLineItems());
   }
 
   /**
@@ -2380,10 +2371,10 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
     }
 
     $membershipParams = $this->getMembershipParamsFromPriceSet($membershipParams);
-    if (!empty($membershipParams['selectMembership'])) {
+    if ($this->isMembershipSelected()) {
       // CRM-12233
       $membershipLineItems = [$this->getPriceSetID() => $this->getLineItems()];;
-      if ($this->_separateMembershipPayment && $this->isFormSupportsNonMembershipContributions()) {
+      if ($this->isSeparatePaymentSelected()) {
         $membershipLineItems = [];
         foreach ($this->_values['fee'] as $key => $feeValues) {
           if ($feeValues['name'] == 'membership_amount') {
index cef3b2fe24eea9486615420a13f90bedc5c3d0d3..236cfde3974621179c2cce65e6194bd92e87c840 100644 (file)
@@ -580,6 +580,16 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     return $mainContributionLineItems;
   }
 
+  /**
+   * Is the form separate payment AND has the user selected 2 options,
+   * resulting in 2 payments.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  protected function isSeparatePaymentSelected(): bool {
+    return (bool) $this->getSecondaryMembershipContributionLineItems();
+  }
+
   /**
    * Set the line items for the secondary membership contribution.
    *
@@ -602,7 +612,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
         $lineItems[$index] = $lineItem;
       }
     }
-    if (empty($lineItems) || count($lineItems) === $this->getLineItems()) {
+    if (empty($lineItems) || count($lineItems) === count($this->getLineItems())) {
       return FALSE;
     }
     return $lineItems;
index c05b9f0774bd7a35e5b74d2e2a1ce1f00b5ae014..c992a3a3b0fa1641fa8268844af9792cc45eab7a 100644 (file)
@@ -976,12 +976,14 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test non-recur contribution with membership payment
+   * Test non-recur contribution with membership payment selected.
+   *
+   * In this scenario the contribution option was not selected so only
+   * one contribution is actually created.
    *
    * @throws \CRM_Core_Exception
-   * @throws \Civi\API\Exception\UnauthorizedException
    */
-  public function testSubmitMembershipIsSeparatePaymentNotRecur(): void {
+  public function testSubmitMembershipIsSeparatePaymentNotRecurMembershipOnly(): void {
     $this->setUpMembershipContributionPage(TRUE, TRUE);
     $dummyPP = Civi\Payment\System::singleton()->getById($this->ids['PaymentProcessor']['dummy']);
     $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_first_success']);