[REF] Unshared another function back onto Membership_Form
authoreileen <emcnaughton@wikimedia.org>
Wed, 23 Dec 2020 20:36:10 +0000 (09:36 +1300)
committereileen <emcnaughton@wikimedia.org>
Thu, 24 Dec 2020 02:02:30 +0000 (15:02 +1300)
As with the other functions there is more not in common than in common & subsequent cleanup can remove
much of the copied function

Remove code unrelated to membership form

CRM/Contribute/Form/Contribution/Confirm.php
CRM/Member/Form/Membership.php
tests/phpunit/CRM/Member/Form/MembershipTest.php

index e0db8bbe3c31f29d7043b3980403e88e743d1d93..e69795941453df768f6d6bc70a4f5d74f6eb69d7 100644 (file)
@@ -1096,10 +1096,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
       }
       CRM_Utils_System::redirect(CRM_Utils_System::url($urlString, $urlParams));
     }
-    // Only set contribution recur ID for contributions since offline membership recur payments are handled somewhere else.
-    if (!is_a($form, "CRM_Member_Form_Membership")) {
-      $form->_params['contributionRecurID'] = $recurring->id;
-    }
+    $form->_params['contributionRecurID'] = $recurring->id;
 
     return $recurring->id;
   }
index 94aaa3fdb5b5566e49b2f8e457e4bc8065de44e0..3fcf805c3b9f1340b97fd3a82ed7db264b6fd585 100644 (file)
@@ -1886,7 +1886,7 @@ DESC limit 1");
     }
     $params['is_recur'] = TRUE;
     $params['payment_instrument_id'] = $contributionParams['payment_instrument_id'] ?? NULL;
-    $recurringContributionID = CRM_Contribute_Form_Contribution_Confirm::processRecurringContribution($form, $params, $contactID, $financialType);
+    $recurringContributionID = $this->legacyProcessRecurringContribution($params, $contactID, $financialType);
 
     $now = date('YmdHis');
     $receiptDate = $params['receipt_date'] ?? NULL;
@@ -1953,4 +1953,74 @@ DESC limit 1");
     return $contribution;
   }
 
+  /**
+   * Create the recurring contribution record.
+   *
+   * This function was copied from another form & needs cleanup.
+   *
+   * @param array $params
+   * @param int $contactID
+   * @param string $contributionType
+   *
+   * @return int|null
+   */
+  protected function legacyProcessRecurringContribution(&$params, $contactID, $contributionType) {
+    $form = $this;
+    if (empty($params['is_recur'])) {
+      return NULL;
+    }
+
+    $recurParams = ['contact_id' => $contactID];
+    $recurParams['amount'] = $params['amount'] ?? NULL;
+    $recurParams['auto_renew'] = $params['auto_renew'] ?? NULL;
+    $recurParams['frequency_unit'] = $params['frequency_unit'] ?? NULL;
+    $recurParams['frequency_interval'] = $params['frequency_interval'] ?? NULL;
+    $recurParams['installments'] = $params['installments'] ?? NULL;
+    $recurParams['financial_type_id'] = $params['financial_type_id'] ?? NULL;
+    $recurParams['currency'] = $params['currency'] ?? NULL;
+    $recurParams['payment_instrument_id'] = $params['payment_instrument_id'];
+
+    // CRM-14354: For an auto-renewing membership with an additional contribution,
+    // if separate payments is not enabled, make sure only the membership fee recurs
+    if (!empty($form->_membershipBlock)
+      && $form->_membershipBlock['is_separate_payment'] === '0'
+      && isset($params['selectMembership'])
+      && $form->_values['is_allow_other_amount'] == '1'
+      // CRM-16331
+      && !empty($form->_membershipTypeValues)
+      && !empty($form->_membershipTypeValues[$params['selectMembership']]['minimum_fee'])
+    ) {
+      $recurParams['amount'] = $form->_membershipTypeValues[$params['selectMembership']]['minimum_fee'];
+    }
+
+    $recurParams['is_test'] = 0;
+    if (($form->_action & CRM_Core_Action::PREVIEW) ||
+      (isset($form->_mode) && ($form->_mode == 'test'))
+    ) {
+      $recurParams['is_test'] = 1;
+    }
+
+    $recurParams['start_date'] = $recurParams['create_date'] = $recurParams['modified_date'] = date('YmdHis');
+    if (!empty($params['receive_date'])) {
+      $recurParams['start_date'] = date('YmdHis', strtotime($params['receive_date']));
+    }
+    $recurParams['invoice_id'] = $params['invoiceID'] ?? NULL;
+    $recurParams['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');
+    $recurParams['payment_processor_id'] = $params['payment_processor_id'] ?? NULL;
+    $recurParams['is_email_receipt'] = $params['is_email_receipt'] ?? NULL;
+    // we need to add a unique trxn_id to avoid a unique key error
+    // in paypal IPN we reset this when paypal sends us the real trxn id, CRM-2991
+    $recurParams['trxn_id'] = $params['trxn_id'] ?? $params['invoiceID'];
+    $recurParams['financial_type_id'] = $contributionType->id;
+
+    $campaignId = $params['campaign_id'] ?? $form->_values['campaign_id'] ?? NULL;
+    $recurParams['campaign_id'] = $campaignId;
+    $recurring = CRM_Contribute_BAO_ContributionRecur::add($recurParams);
+    if (is_a($recurring, 'CRM_Core_Error')) {
+      throw new CRM_Core_Exception(CRM_Core_Error::getMessages($recurring));
+    }
+
+    return $recurring->id;
+  }
+
 }
index d461b49afdc0fc336824a7579e71a5c4ce38a77b..4ed9865863fb6d2a61ab5d4c54772be802f496b5 100644 (file)
@@ -177,7 +177,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
    * @throws \CiviCRM_API3_Exception
    * @throws \CRM_Core_Exception
    */
-  public function testFormRuleEmptyContact() {
+  public function testFormRuleEmptyContact(): void {
     $params = [
       'contact_select_id' => 0,
       'membership_type_id' => [1 => NULL],