CRM-16803 fix non-creating on recurring contributio record on membership renewal
authorEileen McNaughton <eileen@fuzion.co.nz>
Mon, 6 Jul 2015 10:42:08 +0000 (22:42 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Mon, 6 Jul 2015 10:51:49 +0000 (22:51 +1200)
CRM/Member/Form.php
CRM/Member/Form/MembershipRenewal.php

index 28295d2f7a7356f9ac83d8e93e02e7ef612a6500..6f03adf305c7e0777e0ba487884acfd39f708272 100644 (file)
@@ -223,4 +223,50 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment {
     }
   }
 
+  /**
+   * Create a recurring contribution record.
+   *
+   * Recurring contribution parameters are set explicitly rather than merging paymentParams because it's hard
+   * to know the downstream impacts if we keep passing around the same array.
+   *
+   * @param $paymentParams
+   *
+   * @return array
+   * @throws \CiviCRM_API3_Exception
+   */
+  protected function processRecurringContribution($paymentParams) {
+    $membershipID = $paymentParams['membership_type_id'][1];
+    $contributionRecurParams = array(
+      'contact_id' => $paymentParams['contactID'],
+      'amount' => $paymentParams['total_amount'],
+      'payment_processor_id' => $paymentParams['payment_processor_id'],
+      'campaign_id' => $paymentParams['campaign_id'],
+      'financial_type_id' => $paymentParams['financial_type_id'],
+      'is_email_receipt' => $paymentParams['is_email_receipt'],
+      // This is not great as it could also be direct debit - but is consistent with elsewhere & all need fixing.
+      'payment_instrument_id' => 1,
+      'invoice_id' => $paymentParams['invoiceID '],
+    );
+
+    $mapping = array(
+      'frequency_interval' => 'duration_interval',
+      'frequency_unit' => 'duration_unit',
+    );
+    $membershipType = civicrm_api3('MembershipType', 'getsingle', array(
+      'id' => $membershipID,
+      'return' => $mapping,
+    ));
+
+    foreach ($mapping as $recurringFieldName => $membershipTypeFieldName) {
+      $contributionRecurParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName];
+    }
+
+    $contributionRecur = civicrm_api3('ContributionRecur', 'create', $contributionRecurParams);
+    $returnParams = array(
+      'contributionRecurID' => $contributionRecur['id'],
+      'is_recur' => TRUE,
+    );
+    return $returnParams;
+  }
+
 }
index d5f43d50454cad1dbb4f8f974dd046d9b7e06e23..0504a0d637b0758c2a0a9f14e00f0ef65733b4a8 100644 (file)
@@ -582,7 +582,7 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
       $this->_params['amount'] = $formValues['total_amount'];
       $this->_params['currencyID'] = $config->defaultCurrency;
       $this->_params['payment_action'] = 'Sale';
-      $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE));
+      $paymentParams['invoiceID'] = $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE));
 
       // at this point we've created a contact and stored its address etc
       // all the payment processors expect the name and address to be in the passed params
@@ -598,6 +598,10 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
 
       $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
 
+      if ($paymentParams['auto_renew']) {
+        $contributionRecurParams = $this->processRecurringContribution($paymentParams);
+        $paymentParams = array_merge($paymentParams, $contributionRecurParams);
+      }
       $result = &$payment->doDirectPayment($paymentParams);
 
       if (is_a($result, 'CRM_Core_Error')) {