Skip to content

Commit

Permalink
Create contribution before taking payment, per contribution page work…
Browse files Browse the repository at this point in the history
…flow
  • Loading branch information
eileenmcnaughton committed Mar 16, 2019
1 parent d7e2643 commit e1cf69b
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions CRM/Event/Form/Registration/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e1cf69b

Please sign in to comment.