From: Eileen McNaughton Date: Mon, 6 Jul 2015 10:42:08 +0000 (+1200) Subject: CRM-16803 fix non-creating on recurring contributio record on membership renewal X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=a70418a7a8ec2e7bdc4bb0d67331542d0459f663;p=civicrm-core.git CRM-16803 fix non-creating on recurring contributio record on membership renewal --- diff --git a/CRM/Member/Form.php b/CRM/Member/Form.php index 28295d2f7a..6f03adf305 100644 --- a/CRM/Member/Form.php +++ b/CRM/Member/Form.php @@ -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; + } + } diff --git a/CRM/Member/Form/MembershipRenewal.php b/CRM/Member/Form/MembershipRenewal.php index d5f43d5045..0504a0d637 100644 --- a/CRM/Member/Form/MembershipRenewal.php +++ b/CRM/Member/Form/MembershipRenewal.php @@ -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')) {