From e9dee70bb8156baa5c20f4f24a25e073f5593928 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 22 Dec 2023 16:03:37 +1300 Subject: [PATCH] Consolidate payment creation into 1 function --- CRM/Event/Form/Participant.php | 55 +++++++++++++++++----------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index a1d4eb4083..72dcf45d54 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -560,7 +560,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $this->assign('eventTypeID', $this->_eventTypeId); - $this->assign('event_is_test', CRM_Utils_Array::value('event_is_test', $defaults)); + $this->assign('event_is_test', $this->isTest()); return $defaults; } @@ -926,13 +926,16 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment } $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $this->_contactId, NULL, NULL, $ctype); } - $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->_id, $this->getDefaultEntity()); //do cleanup line items if participant edit the Event Fee. if (($this->getLineItems() || !isset($params['proceSetId'])) && !$this->_paymentId && $this->_id) { CRM_Price_BAO_LineItem::deleteLineItems($this->_id, 'civicrm_participant'); } - + $participants = []; + $contactIDS = $this->_contactIds ?: [$this->getContactID()]; + foreach ($contactIDS as $contactID) { + $participants[] = $this->addParticipant($contactID); + } if ($this->_mode) { // add all the additional payment params we need $paymentParams = $this->prepareParamsForPaymentProcessor($this->getSubmittedValues()); @@ -979,10 +982,6 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $this->_paymentProcessor ); - // add participant record - $participants = []; - $participants[] = $this->addParticipant($contactID); - // Add participant payment $participantPaymentParams = [ 'participant_id' => $participants[0]->id, @@ -994,15 +993,9 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment } else { if ($this->_single) { + // Still needed? $this->_contactIds[] = $this->_contactId; } - $participants = []; - foreach ($this->_contactIds as $contactID) { - $commonParams = $params; - $commonParams['source'] = $this->getSourceText(); - $commonParams['contact_id'] = $contactID; - $participants[] = CRM_Event_BAO_Participant::create($commonParams); - } $contributions = []; if (!empty($params['record_contribution'])) { @@ -1157,11 +1150,6 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $result = $this->sendReceipts($params, $participants); } - // set the participant id if it is not set - if (!$this->_id) { - $this->_id = $participants[0]->id; - } - return $this->getStatusMsg($params, $result['sent'] ?? 0, $result['not_sent'] ?? 0, (string) $updateStatusMsg); } @@ -1572,10 +1560,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $contribParams['contribution_status_id'] = array_search('Pending', $allStatuses); } - $contribParams['is_test'] = 0; - if ($this->getAction() & CRM_Core_Action::PREVIEW || ($this->_mode ?? NULL) === 'test') { - $contribParams['is_test'] = 1; - } + $contribParams['is_test'] = $this->isTest(); if (!empty($contribParams['invoice_id'])) { $contribParams['id'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', @@ -1618,14 +1603,17 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment 'role_id' => $this->getSubmittedValue('role_id'), 'register_date' => $this->getSubmittedValue('register_date'), 'source' => $this->getSourceText(), - 'fee_level' => $this->order->getAmountLevel(), 'is_pay_later' => FALSE, - 'fee_amount' => $this->order->getTotalAmount(), 'fee_currency' => $this->getCurrency(), 'campaign_id' => $this->getSubmittedValue('campaign_id'), 'note' => $this->getSubmittedValue('note'), - 'is_test' => ($this->_mode === 'test)'), + 'is_test' => $this->isTest(), ]; + $order = $this->getOrder(); + if ($order) { + $participantParams['fee_level'] = $order->getAmountLevel(); + $participantParams['fee_amount'] = $order->getTotalAmount(); + } $participantParams['discount_id'] = $this->getSubmittedValue('discount_id'); $participant = CRM_Event_BAO_Participant::create($participantParams); @@ -1637,7 +1625,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment 'Participant' ); $transaction->commit(); - + $this->_id = $participant->id; return $participant; } @@ -1867,7 +1855,7 @@ INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_ $sendTemplateParams = [ 'workflow' => 'event_offline_receipt', 'contactId' => $contactID, - 'isTest' => !empty($this->_defaultValues['is_test']), + 'isTest' => $this->isTest(), 'PDFFilename' => ts('confirmation') . '.pdf', 'modelProps' => [ 'participantID' => $participantID, @@ -1987,6 +1975,17 @@ INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_ return (bool) ($_GET['eventId'] ?? NULL); } + /** + * Is the form being submitted in test mode. + * + * @api this function is supported for external use. + * + * @return bool + */ + public function isTest(): bool { + return $this->_mode === 'test'; + } + /** * Get the contact ID in use. * -- 2.25.1