From a1278f71bd804c8013600793916a692b4345e4a2 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Tue, 1 Feb 2022 23:37:30 +0000 Subject: [PATCH] Add paymentstatus helpers for doPayment and separate return params --- CRM/Core/Payment.php | 37 +++++++++++++++++++++++-------- CRM/Core/Payment/AuthorizeNet.php | 11 +++++---- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/CRM/Core/Payment.php b/CRM/Core/Payment.php index 26a62af3a9..7658750b99 100644 --- a/CRM/Core/Payment.php +++ b/CRM/Core/Payment.php @@ -1366,14 +1366,12 @@ abstract class CRM_Core_Payment { public function doPayment(&$params, $component = 'contribute') { $propertyBag = \Civi\Payment\PropertyBag::cast($params); $this->_component = $component; - $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'validate'); // If we have a $0 amount, skip call to processor and set payment_status to Completed. // Conceivably a processor might override this - perhaps for setting up a token - but we don't // have an example of that at the moment. if ($propertyBag->getAmount() == 0) { - $result['payment_status_id'] = array_search('Completed', $statuses); - $result['payment_status'] = 'Completed'; + $result = $this->setStatusPaymentCompleted([]); return $result; } @@ -1381,8 +1379,7 @@ abstract class CRM_Core_Payment { CRM_Core_Error::deprecatedFunctionWarning('doPayment', 'doTransferCheckout'); $result = $this->doTransferCheckout($params, $component); if (is_array($result) && !isset($result['payment_status_id'])) { - $result['payment_status_id'] = array_search('Pending', $statuses); - $result['payment_status'] = 'Pending'; + $result = $this->setStatusPaymentPending($result); } } else { @@ -1391,12 +1388,10 @@ abstract class CRM_Core_Payment { if (is_array($result) && !isset($result['payment_status_id'])) { if (!empty($params['is_recur'])) { // See comment block. - $result['payment_status_id'] = array_search('Pending', $statuses); - $result['payment_status'] = 'Pending'; + $result = $this->setStatusPaymentPending($result); } else { - $result['payment_status_id'] = array_search('Completed', $statuses); - $result['payment_status'] = 'Completed'; + $result = $this->setStatusPaymentCompleted($result); } } } @@ -1407,6 +1402,30 @@ abstract class CRM_Core_Payment { return $result; } + /** + * Set the payment status to Pending + * @param \Civi\Payment\PropertyBag|array $params + * + * @return array + */ + protected function setStatusPaymentPending($params) { + $params['payment_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'); + $params['payment_status'] = 'Pending'; + return $params; + } + + /** + * Set the payment status to Completed + * @param \Civi\Payment\PropertyBag|array $params + * + * @return array + */ + protected function setStatusPaymentCompleted($params) { + $params['payment_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); + $params['payment_status'] = 'Completed'; + return $params; + } + /** * Cancel a recurring subscription. * diff --git a/CRM/Core/Payment/AuthorizeNet.php b/CRM/Core/Payment/AuthorizeNet.php index ef44058b12..ed28adc948 100644 --- a/CRM/Core/Payment/AuthorizeNet.php +++ b/CRM/Core/Payment/AuthorizeNet.php @@ -184,13 +184,13 @@ class CRM_Core_Payment_AuthorizeNet extends CRM_Core_Payment { // fetch available contribution statuses $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); + $result = []; // check for application errors // TODO: // AVS, CVV2, CAVV, and other verification results switch ($response_fields[0]) { case self::AUTH_REVIEW: - $params['payment_status_id'] = array_search('Pending', $contributionStatus); - $params['payment_status'] = 'Pending'; + $result = $this->setStatusPaymentPending($result); break; case self::AUTH_ERROR: @@ -203,13 +203,12 @@ class CRM_Core_Payment_AuthorizeNet extends CRM_Core_Payment { default: // Success - $params['trxn_id'] = !empty($response_fields[6]) ? $response_fields[6] : $this->getTestTrxnID(); - $params['payment_status_id'] = array_search('Completed', $statuses); - $params['payment_status'] = 'Completed'; + $result['trxn_id'] = !empty($response_fields[6]) ? $response_fields[6] : $this->getTestTrxnID(); + $result = $this->setStatusPaymentCompleted($result); break; } - return $params; + return $result; } /** -- 2.25.1