Skip to content

Commit

Permalink
Merge pull request #31404 from mattwire/extractpay
Browse files Browse the repository at this point in the history
Extract payment function
  • Loading branch information
eileenmcnaughton authored Nov 1, 2024
2 parents e453a76 + eac0548 commit db98500
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 40 deletions.
92 changes: 53 additions & 39 deletions CRM/Contribute/Form/Contribution/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2405,8 +2405,7 @@ protected function doMembershipProcessing($contactID, $membershipParams, $premiu
*/
protected function bounceOnError($message): void {
CRM_Core_Session::singleton()
->setStatus(ts('Payment Processor Error message :') .
$message);
->setStatus(ts('Payment Processor Error message') . ': ' . $message);
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact',
'_qf_Main_display=true&qfKey=' . ($this->_params['qfKey'] ?? NULL)
));
Expand Down Expand Up @@ -2559,43 +2558,7 @@ public function processConfirm(
$form->_values['contribution_page_id'] = $contribution->contribution_page_id;

if (!empty($form->_paymentProcessor)) {
try {
$payment = Civi\Payment\System::singleton()->getByProcessor($form->_paymentProcessor);
if ($contribution->contribution_recur_id && $this->getPaymentProcessorObject()->supports('noReturnForRecurring')) {
// We want to get rid of this & make it generic - eg. by making payment processing the last thing
// and always calling it first.
$form->postProcessHook();
}
$paymentParams['currency'] = $this->getCurrency();
$result = $payment->doPayment($paymentParams);
$form->_params = array_merge($form->_params, $result);
$form->assign('trxn_id', $result['trxn_id'] ?? '');
$contribution->trxn_id = $result['trxn_id'] ?? $contribution->trxn_id ?? '';
$contribution->payment_status_id = $result['payment_status_id'];
$result['contribution'] = $contribution;
if ($result['payment_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending')
&& $payment->isSendReceiptForPending()) {
CRM_Contribute_BAO_ContributionPage::sendMail($contactID,
$form->_values,
$contribution->is_test
);
}
return $result;
}
catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
// Clean up DB as appropriate.
if (!empty($paymentParams['contributionID'])) {
CRM_Contribute_BAO_Contribution::failPayment($paymentParams['contributionID'],
$paymentParams['contactID'], $e->getMessage());
}
if (!empty($paymentParams['contributionRecurID'])) {
CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($paymentParams['contributionRecurID']);
}

$result['is_payment_failure'] = TRUE;
$result['error'] = $e;
return $result;
}
return $this->processConfirmPayment($contribution, $contactID, $paymentParams);
}
}

Expand All @@ -2616,6 +2579,57 @@ public function processConfirm(
throw new CRM_Core_Exception('code is unreachable, exception is for clarity for refactoring');
}

/**
* This was extracted from processConfirm() as part of a refactoring process.
* Signature/params subject to change!
* This should probably throw the exception instead of catching it and putting it in an array
* @internal
*
* @param \CRM_Contribute_DAO_Contribution $contribution
* @param int $contactID
* @param array $paymentParams
*
* @return array
* @throws \CRM_Core_Exception
*/
private function processConfirmPayment(\CRM_Contribute_DAO_Contribution $contribution, int $contactID, array $paymentParams): array {
$form = $this;
try {
$payment = Civi\Payment\System::singleton()->getByProcessor($form->_paymentProcessor);
if ($contribution->contribution_recur_id && $this->getPaymentProcessorObject()->supports('noReturnForRecurring')) {
// We want to get rid of this & make it generic - eg. by making payment processing the last thing
// and always calling it first.
$form->postProcessHook();
}
$paymentParams['currency'] = $this->getCurrency();
$result = $payment->doPayment($paymentParams);
$form->_params = array_merge($form->_params, $result);
$form->assign('trxn_id', $result['trxn_id'] ?? '');
$contribution->trxn_id = $result['trxn_id'] ?? $contribution->trxn_id ?? '';
$contribution->payment_status_id = $result['payment_status_id'];
$result['contribution'] = $contribution;
if ($result['payment_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending')
&& $payment->isSendReceiptForPending()) {
CRM_Contribute_BAO_ContributionPage::sendMail($contactID, $form->_values, $contribution->is_test);
}
return $result;
}
catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
// Clean up DB as appropriate.
if (!empty($paymentParams['contributionID'])) {
CRM_Contribute_BAO_Contribution::failPayment($paymentParams['contributionID'],
$paymentParams['contactID'], $e->getMessage());
}
if (!empty($paymentParams['contributionRecurID'])) {
CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($paymentParams['contributionRecurID']);
}

$result['is_payment_failure'] = TRUE;
$result['error'] = $e;
return $result;
}
}

/**
* Temporary function to allow unit tests to access function being refactored away.
*
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static function getMessages(&$error, $separator = '<br />') {
public static function displaySessionError(&$error, $separator = '<br />') {
$message = self::getMessages($error, $separator);
if ($message) {
$status = ts("Payment Processor Error message") . "{$separator} $message";
$status = ts('Payment Processor Error message') . "{$separator} $message";
$session = CRM_Core_Session::singleton();
$session->setStatus($status);
}
Expand Down

0 comments on commit db98500

Please sign in to comment.