Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create contribution before taking payment, per contribution page workflow #13837

Merged
merged 1 commit into from
Apr 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 40 additions & 12 deletions CRM/Event/Form/Registration/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,6 @@ public function postProcess() {
$preApprovalParams = $this->_paymentProcessor['object']->getPreApprovalDetails($this->get('pre_approval_parameters'));
$value = array_merge($value, $preApprovalParams);
}
$result = NULL;

if (!empty($value['is_pay_later']) ||
$value['amount'] == 0 ||
Expand All @@ -582,16 +581,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);
}
eileenmcnaughton marked this conversation as resolved.
Show resolved Hide resolved
}

$value['receive_date'] = $now;
Expand All @@ -618,8 +607,44 @@ public function postProcess() {
$isAdditionalAmount = TRUE;
}

CRM_Contribute_Form_AbstractEditPayment::formatCreditCardDetails($params[0]);
//passing contribution id is already registered.
$contribution = self::processContribution($this, $value, $result, $contactID, $pending, $isAdditionalAmount, $this->_paymentProcessor);
$contribution = self::processContribution($this, $value, NULL, $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[0]),
'pan_truncation' => CRM_Utils_Array::value('pan_truncation', $params[0]),
]);
// 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 Expand Up @@ -973,6 +998,9 @@ public static function processContribution(
}

// CRM-20264: fetch CC type ID and number (last 4 digit) and assign it back to $params
// @todo remove this once backoffice form is doing it - front end is doing it twice.
// (in general they are sharing much more code than they should - anything that truly should be shared
// should not be on this class).
CRM_Contribute_Form_AbstractEditPayment::formatCreditCardDetails($params);

$contribParams = array(
Expand Down