diff --git a/CRM/Event/Form/Registration/Confirm.php b/CRM/Event/Form/Registration/Confirm.php index 8557a30dec30..322d16b8d51d 100644 --- a/CRM/Event/Form/Registration/Confirm.php +++ b/CRM/Event/Form/Registration/Confirm.php @@ -583,16 +583,6 @@ public function postProcess() { if (empty($value['email'])) { $value['email'] = CRM_Utils_Array::valueByRegexKey('/^email-/', $value); } - - if (is_object($payment)) { - // Not quite sure why we don't just user $value since it contains the data - // from result - // @todo ditch $result & retest. - list($result, $value) = $this->processPayment($payment, $value); - } - else { - CRM_Core_Error::fatal($paymentObjError); - } } $value['receive_date'] = $now; @@ -620,7 +610,42 @@ public function postProcess() { } //passing contribution id is already registered. - $contribution = self::processContribution($this, $value, $result, $contactID, $pending, $isAdditionalAmount, $this->_paymentProcessor); + $contribution = self::processContribution($this, $value, $result, $contactID, TRUE, $isAdditionalAmount, $this->_paymentProcessor); + + try { + // @todo this should really be if $amount > 0, for pay later we should just load the + // manual pseudo processor (0) so there *should* always be a defined processor + if (!empty($payment)) { + $result = $payment->doPayment($value, 'event'); + if ($result['payment_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed')) { + try { + civicrm_api3('contribution', 'completetransaction', [ + 'id' => $contribution->id, + 'trxn_id' => $result['trxn_id'], + 'payment_processor_id' => $this->_paymentProcessor['id'], + 'is_transactional' => FALSE, + 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), + 'is_email_receipt' => FALSE, + 'card_type_id' => CRM_Utils_Array::value('card_type_id', $params), + 'pan_truncation' => CRM_Utils_Array::value('pan_truncation', $params), + ]); + // This has now been set to 1 in the DB - declare it here also + $contribution->contribution_status_id = 1; + } + catch (CiviCRM_API3_Exception $e) { + if ($e->getErrorCode() != 'contribution_completed') { + throw new CRM_Core_Exception('Failed to update contribution in database'); + } + } + } + } + } + catch (\Civi\Payment\Exception\PaymentProcessorException $e) { + Civi::log()->error('Payment processor exception: ' . $e->getMessage()); + CRM_Core_Session::singleton()->setStatus($e->getMessage()); + CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/register', "id={$this->_eventId}")); + } + $value['contributionID'] = $contribution->id; $value['contributionTypeID'] = $contribution->financial_type_id; $value['receive_date'] = $contribution->receive_date;