From d335b8f6ec8332c107d83c4d7515fe2c6987f9e1 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 21 Jun 2023 20:23:48 -0700 Subject: [PATCH] Use API for failure updates This - switches to use the api rather than DAO save (calls hooks) - adds a cancel_reason - cos why not & the field was added since the IPN was written - specifies the authorize_net log. This puts it in a different log file and allows listeners if sites want. Feels like better practice --- CRM/Core/Payment/AuthorizeNetIPN.php | 43 ++++++++++++---------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/CRM/Core/Payment/AuthorizeNetIPN.php b/CRM/Core/Payment/AuthorizeNetIPN.php index c6e1b981d2..75b5592bd8 100644 --- a/CRM/Core/Payment/AuthorizeNetIPN.php +++ b/CRM/Core/Payment/AuthorizeNetIPN.php @@ -10,6 +10,7 @@ */ use Civi\Api4\Contribution; +use Civi\Api4\ContributionRecur; use Civi\Api4\PaymentProcessor; /** @@ -34,21 +35,31 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN { /** * Main IPN processing function. - * - * @return bool|void - * - * @throws \CRM_Core_Exception */ public function main() { try { //we only get invoice num as a key player from payment gateway response. //for ARB we get x_subscription_id and x_subscription_paynum + // @todo - no idea what the above comment means. The do-nothing line below + // this is only still here as it might relate??? $x_subscription_id = $this->getRecurProcessorID(); + + if (!$this->isSuccess()) { + $errorMessage = ts('Subscription payment failed - %1', [1 => htmlspecialchars($input['response_reason_text'])]); + ContributionRecur::update(FALSE) + ->addWhere('id', '=', $this->getContributionRecurID()) + ->setValues([ + 'contribution_status_id:name' => 'Failed', + 'cancel_date' => 'now', + 'cancel_reason' => $errorMessage, + ])->execute(); + \Civi::log('authorize_net')->info($errorMessage); + return; + } $this->recur(); - return TRUE; } catch (CRM_Core_Exception $e) { - Civi::log()->debug($e->getMessage()); + Civi::log('authorize_net')->debug($e->getMessage()); echo 'Invalid or missing data'; } } @@ -58,10 +69,8 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN { */ public function recur() { $recur = $this->getContributionRecur(); - $paymentProcessorID = $this->getPaymentProcessorID(); $input = $this->getInput(); - $input['payment_processor_id'] = $paymentProcessorID; - $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); + $input['payment_processor_id'] = $this->getPaymentProcessorID(); $now = date('YmdHis'); @@ -84,22 +93,6 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN { $recur->save(); } } - else { - // Declined - // failed status - $recur->contribution_status_id = array_search('Failed', $contributionStatus); - $recur->cancel_date = $now; - $recur->save(); - - $message = ts('Subscription payment failed - %1', [1 => htmlspecialchars($input['response_reason_text'])]); - CRM_Core_Error::debug_log_message($message); - - // the recurring contribution has declined a payment or has failed - // so we just fix the recurring contribution and not change any of - // the existing contributions - // CRM-9036 - return; - } CRM_Contribute_BAO_Contribution::completeOrder($input, $recur->id, $this->getContributionStatus() !== 'Completed' ? $this->getContributionID() : NULL); if ($isFirstOrLastRecurringPayment) { -- 2.25.1