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 #13763

Closed
Closed
Show file tree
Hide file tree
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
5 changes: 1 addition & 4 deletions CRM/Event/Form/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -1270,10 +1270,7 @@ public function submit($params) {

//add contribution record
$contributions[] = $contribution = CRM_Event_Form_Registration_Confirm::processContribution(
$this, $this->_params,
$result, $contactID,
FALSE, FALSE,
$this->_paymentProcessor
$this, $this->_params, $contactID, FALSE, FALSE, $this->_paymentProcessor
);

// add participant record
Expand Down
88 changes: 35 additions & 53 deletions CRM/Event/Form/Registration/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,40 +560,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 ||
// The concept of contributeMode is deprecated.
$this->_contributeMode == 'checkout' ||
$this->_contributeMode == 'notify'
) {
if ($value['amount'] != 0) {
$pending = TRUE;
//get the participant statuses.
$pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'");
$status = !empty($value['is_pay_later']) ? 'Pending from pay later' : 'Pending from incomplete transaction';
$value['participant_status_id'] = $value['participant_status'] = array_search($status, $pendingStatuses);
}
}
elseif (!empty($value['is_primary'])) {
CRM_Core_Payment_Form::mapParams($this->_bltID, $value, $value, TRUE);
// payment email param can be empty for _bltID mapping
// thus provide mapping for it with a different email value
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;
if ($this->_allowConfirmation) {
Expand All @@ -620,7 +586,7 @@ 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, $contactID, $pending, $isAdditionalAmount, $this->_paymentProcessor);
$value['contributionID'] = $contribution->id;
$value['contributionTypeID'] = $contribution->financial_type_id;
$value['receive_date'] = $contribution->receive_date;
Expand All @@ -631,6 +597,36 @@ public function postProcess() {
$value['contactID'] = $contactID;
$value['eventID'] = $this->_eventId;
$value['item_name'] = $value['description'];

if (!empty($value['is_pay_later']) ||
$value['amount'] == 0 ||
// The concept of contributeMode is deprecated.
$this->_contributeMode == 'checkout' ||
$this->_contributeMode == 'notify'
) {
if ($value['amount'] != 0) {
$pending = TRUE;
//get the participant statuses.
$pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'");
$status = !empty($value['is_pay_later']) ? 'Pending from pay later' : 'Pending from incomplete transaction';
$value['participant_status_id'] = $value['participant_status'] = array_search($status, $pendingStatuses);
}
}
elseif (!empty($value['is_primary'])) {
CRM_Core_Payment_Form::mapParams($this->_bltID, $value, $value, TRUE);
// payment email param can be empty for _bltID mapping
// thus provide mapping for it with a different email value
if (empty($value['email'])) {
$value['email'] = CRM_Utils_Array::valueByRegexKey('/^email-/', $value);
}

if (is_object($payment)) {
$value = $this->processPayment($payment, $value);
}
else {
CRM_Core_Error::fatal($paymentObjError);
}
}
}

if (!empty($value['contributionID'])) {
Expand Down Expand Up @@ -946,7 +942,6 @@ public function postProcess() {
*
* @param CRM_Core_Form $form
* @param array $params
* @param array $result
* @param int $contactID
* @param bool $pending
* @param bool $isAdditionalAmount
Expand All @@ -956,18 +951,14 @@ public function postProcess() {
* @throws \CRM_Core_Exception
*/
public static function processContribution(
&$form, $params, $result, $contactID,
&$form, $params, $contactID,
$pending = FALSE, $isAdditionalAmount = FALSE,
$paymentProcessor = NULL
) {
$transaction = new CRM_Core_Transaction();

$now = date('YmdHis');
$receiptDate = NULL;

if (!empty($form->_values['event']['is_email_confirm'])) {
$receiptDate = $now;
}
//CRM-4196
if ($isAdditionalAmount) {
$params['amount_level'] = $params['amount_level'] . ts(' (multiple participants)') . CRM_Core_DAO::VALUE_SEPARATOR;
Expand Down Expand Up @@ -997,15 +988,6 @@ public static function processContribution(
$contribParams['payment_processor'] = $paymentProcessor['id'];
}

if (!$pending && $result) {
$contribParams += array(
'fee_amount' => CRM_Utils_Array::value('fee_amount', $result),
'net_amount' => CRM_Utils_Array::value('net_amount', $result, $params['amount']),
'trxn_id' => $result['trxn_id'],
'receipt_date' => $receiptDate,
);
}

$allStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$contribParams['contribution_status_id'] = array_search('Completed', $allStatuses);
if ($pending) {
Expand Down Expand Up @@ -1336,15 +1318,15 @@ public static function testSubmit($params) {
*/
private function processPayment($payment, $value) {
try {
$result = $payment->doPayment($value, 'event');
return array($result, $value);
$payment->doPayment($value, 'event');
return $value;
}
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}"));
}
return array();
return NULL;
}

/**
Expand Down