}
}
+ /**
+ * 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;
+ }
+
}
$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
$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')) {