From edce66df1c0f8da4f8d65008dc9cbcec1bdae7b3 Mon Sep 17 00:00:00 2001 From: Francis Whittle Date: Thu, 14 Dec 2023 15:28:53 +1100 Subject: [PATCH] CIVIEWAY-263 Refactor out more deprecations --- CRM/Core/Payment/eWAYRecurring.php | 9 +++-- CRM/eWAYRecurring/Form/Settings.php | 2 +- CRM/eWAYRecurring/Page/VerifyUpdateToken.php | 11 +++--- CRM/eWAYRecurring/ProcessTrait.php | 35 +++++++++----------- CRM/eWAYRecurring/Utils.php | 8 ++++- eWAYRecurring.php | 15 +++++---- tests/phpunit/CRM/EwayRecurring/TestCase.php | 2 +- 7 files changed, 43 insertions(+), 39 deletions(-) diff --git a/CRM/Core/Payment/eWAYRecurring.php b/CRM/Core/Payment/eWAYRecurring.php index 1f5046a..2dba510 100644 --- a/CRM/Core/Payment/eWAYRecurring.php +++ b/CRM/Core/Payment/eWAYRecurring.php @@ -35,7 +35,6 @@ public static function getInstance(&$paymentProcessor) { function __construct($mode, &$paymentProcessor) { $this->_mode = $mode; // live or test $this->_paymentProcessor = $paymentProcessor; - $this->_processorName = ts('eWay Recurring'); } /** @@ -58,7 +57,7 @@ function validateContribution($eWayAccessCode, $contribution, $qfKey, $paymentPr if ($hasTransactionFailed) { civicrm_api3('Contribution', 'create', [ 'id' => $contributionID, - 'contribution_status_id' => _contribution_status_id('Failed'), + 'contribution_status_id' => CRM_eWAYRecurring_Utils::contribution_status_id('Failed'), 'trxn_id' => $transactionID, ]); @@ -280,7 +279,7 @@ public function doPayment(&$params, $component = 'contribute') { } if (!defined('CURLOPT_SSLCERT')) { - CRM_Core_Error::fatal(ts('eWay - Gateway requires curl with SSL support')); + throw new CRM_Core_Exception(ts('eWay - Gateway requires curl with SSL support')); } $eWayClient = $this->getEWayClient(); @@ -739,10 +738,10 @@ function updateSubscriptionBillingInfo(&$message = '', $params = []) { //---------------------------------------------------------------------------------------------------- switch ($contribution['contribution_status_id']) { - case _contribution_status_id('Completed'): + case CRM_eWAYRecurring_Utils::contribution_status_id('Completed', TRUE): throw new Exception(ts('Attempted to update billing details for a completed contribution.')); break; - case _contribution_status_id('Cancelled'): + case CRM_eWAYRecurring_Utils::contribution_status_id('Cancelled', TRUE): throw new Exception(ts('Attempted to update billing details for a cancelled contribution.')); break; default: diff --git a/CRM/eWAYRecurring/Form/Settings.php b/CRM/eWAYRecurring/Form/Settings.php index 9b1e0e2..130d43c 100644 --- a/CRM/eWAYRecurring/Form/Settings.php +++ b/CRM/eWAYRecurring/Form/Settings.php @@ -40,7 +40,7 @@ public function addFormElements() { if (isset($setting['quick_form_type'])) { $add = 'add' . $setting['quick_form_type']; if ($add == 'addElement') { - $this->$add($setting['html_type'], $name, $setting['title'], $setting['html_attributes'] ?? [], TRUE); + $this->$add($setting['html_type'], $name, $setting['title'], $setting['html_attributes'], TRUE); } elseif ($setting['html_type'] == 'Select') { $optionValues = []; diff --git a/CRM/eWAYRecurring/Page/VerifyUpdateToken.php b/CRM/eWAYRecurring/Page/VerifyUpdateToken.php index b1ea156..ecd4cf3 100644 --- a/CRM/eWAYRecurring/Page/VerifyUpdateToken.php +++ b/CRM/eWAYRecurring/Page/VerifyUpdateToken.php @@ -3,7 +3,7 @@ class CRM_eWAYRecurring_Page_VerifyUpdateToken extends CRM_Core_Page { public function run() { - $store = NULL; + $store = $form = NULL; $recurringContributionID = CRM_Utils_Request::retrieve('recurringContributionID', 'String', $store, FALSE, ""); $eWayAccessCode = CRM_Utils_Request::retrieve('AccessCode', 'String', $form, FALSE, ""); @@ -28,7 +28,7 @@ public function run() { $paymentProcessorInfo = $paymentProcessorInfo[0]; //$paymentProcessorInfo['is_test'] = 1; - $response = CRM_eWAYRecurring_Utils::validateEwayAccessCode($eWayAccessCode, $paymentProcessorInfo, TRUE); + $response = \CRM_eWAYRecurring_Utils::validateEwayAccessCode($eWayAccessCode, $paymentProcessorInfo, TRUE); $hasTransactionFailed = $response['hasTransactionFailed']; $transactionResponseError = $response['transactionResponseError']; @@ -48,13 +48,14 @@ public function run() { // Updating the billing details should fixed failed contributions //---------------------------------------------------------------------------------------------------- - CRM_eWAYRecurring_Utils::updateCustomerDetails($response, $recurringContribution); + \CRM_eWAYRecurring_Utils::updateCustomerDetails($response, $recurringContribution); - if (_contribution_status_id('Failed') == $recurringContribution['contribution_status_id']) { + if ( \CRM_eWAYRecurring_Utils::contribution_status_id('Failed', TRUE) == $recurringContribution['contribution_status_id']) { CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_ContributionRecur', $recurringContribution['id'], 'contribution_status_id', - _contribution_status_id('In Progress')); + \CRM_eWAYRecurring_Utils::contribution_status_id('In Progress', TRUE) + ); } CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_ContributionRecur', diff --git a/CRM/eWAYRecurring/ProcessTrait.php b/CRM/eWAYRecurring/ProcessTrait.php index 64b1466..e73a7bf 100644 --- a/CRM/eWAYRecurring/ProcessTrait.php +++ b/CRM/eWAYRecurring/ProcessTrait.php @@ -79,12 +79,11 @@ public function process_recurring_payments() { else { $next_sched = NULL; /* Mark recurring contribution as complteted*/ - civicrm_api( + civicrm_api3( 'ContributionRecur', 'create', [ - 'version' => '3', 'id' => $contribution->id, - 'contribution_recur_status_id' => _contribution_status_id('Completed'), + 'contribution_recur_status_id' => CRM_eWAYRecurring_Utils::contribution_status_id('Completed', TRUE), ] ); } @@ -93,11 +92,7 @@ public function process_recurring_payments() { $amount_in_cents = preg_replace('/\.([0-9]{0,2}).*$/', '$1', $contribution->amount); - $addresses = civicrm_api('Address', 'get', - [ - 'version' => '3', - 'contact_id' => $contribution->contact_id, - ]); + $addresses = civicrm_api3('Address', 'get', ['contact_id' => $contribution->contact_id,]); $billing_address = array_shift($addresses['values']); @@ -118,7 +113,7 @@ public function process_recurring_payments() { $repeat_params = [ 'contribution_recur_id' => $contribution->id, - 'contribution_status_id' => _contribution_status_id('Pending'), + 'contribution_status_id' => CRM_eWAYRecurring_Utils::contribution_status_id('Pending'), 'total_amount' => $contribution->amount, 'is_email_receipt' => 0, ]; @@ -226,23 +221,23 @@ public function process_recurring_payments() { if (count($responseErrors)) { // Mark transaction as failed - $new_contribution_record['contribution_status_id'] = _contribution_status_id('Failed'); + $new_contribution_record['contribution_status_id'] = CRM_eWAYRecurring_Utils::contribution_status_id('Failed'); $this->mark_recurring_contribution_failed($contribution); } else { // $this->send_receipt_email($new_contribution_record->id); - $new_contribution_record['contribution_status_id'] = _contribution_status_id('Completed'); + $new_contribution_record['contribution_status_id'] = CRM_eWAYRecurring_Utils::contribution_status_id('Completed'); $new_contribution_record['is_email_receipt'] = Civi::settings() ->get('eway_recurring_keep_sending_receipts'); - if ($contribution->failure_count > 0 && $contribution->contribution_status_id == _contribution_status_id('Failed')) { + if ($contribution->failure_count > 0 && $contribution->contribution_status_id == CRM_eWAYRecurring_Utils::contribution_status_id('Failed')) { // Failed recurring contribution completed successfuly after several retry. $this->update_contribution_status($next_sched, $contribution); CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_ContributionRecur', $contribution->id, 'contribution_status_id', - _contribution_status_id('In Progress')); + CRM_eWAYRecurring_Utils::contribution_status_id('In Progress', TRUE)); try { civicrm_api3('Activity', 'create', [ @@ -266,7 +261,7 @@ public function process_recurring_payments() { } $api_action = ( - $new_contribution_record['contribution_status_id'] == _contribution_status_id('Completed') + $new_contribution_record['contribution_status_id'] == CRM_eWAYRecurring_Utils::contribution_status_id('Completed') ? 'completetransaction' : 'create' ); @@ -303,10 +298,10 @@ public function process_recurring_payments() { // already talk to eway? then we need to check the payment status if ($eWayResponse) { - $new_contribution_record['contribution_status_id'] = _contribution_status_id('Pending'); + $new_contribution_record['contribution_status_id'] = CRM_eWAYRecurring_Utils::contribution_status_id('Pending'); } else { - $new_contribution_record['contribution_status_id'] = _contribution_status_id('Failed'); + $new_contribution_record['contribution_status_id'] = CRM_eWAYRecurring_Utils::contribution_status_id('Failed'); } try { @@ -364,7 +359,7 @@ protected function update_contribution_status($next_sched, $contribution) { CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_ContributionRecur', $contribution->id, 'contribution_status_id', - _contribution_status_id('Completed')); + CRM_eWAYRecurring_Utils::contribution_status_id('Completed', TRUE)); CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_ContributionRecur', $contribution->id, 'end_date', @@ -422,8 +417,8 @@ protected function get_scheduled_contributions() { // Don't get cancelled or failed contributions $status_ids = implode(', ', [ - _contribution_status_id('In Progress'), - _contribution_status_id('Pending'), + CRM_eWAYRecurring_Utils::contribution_status_id('In Progress', TRUE), + CRM_eWAYRecurring_Utils::contribution_status_id('Pending', TRUE), ]); $scheduled_today->whereAdd("`contribution_status_id` IN ({$status_ids})"); @@ -469,7 +464,7 @@ public function get_scheduled_failed_contributions() { $scheduled_today->whereAdd("`failure_retry_date` <= now()"); - $scheduled_today->contribution_status_id = _contribution_status_id('In Progress'); + $scheduled_today->contribution_status_id = CRM_eWAYRecurring_Utils::contribution_status_id('In Progress', TRUE); $scheduled_today->whereAdd("`failure_count` < " . $maxFailRetry); $scheduled_today->whereAdd("`failure_count` > 0"); diff --git a/CRM/eWAYRecurring/Utils.php b/CRM/eWAYRecurring/Utils.php index 95662d9..fc5f142 100644 --- a/CRM/eWAYRecurring/Utils.php +++ b/CRM/eWAYRecurring/Utils.php @@ -417,7 +417,7 @@ private static function updateRecurringContribution($contribution, $customerToke $recurringContribution['processor_id'] = $customerTokenId; $recurringContribution['payment_token_id'] = $paymentTokenID; $recurringContribution['create_date'] = CRM_Utils_Date::isoToMysql(date('Y-m-d H:i:s')); - $recurringContribution['contribution_status_id'] = _contribution_status_id('In Progress'); + $recurringContribution['contribution_status_id'] = self::contribution_status_id('In Progress', TRUE); $recurringContribution['trxn_id'] = $transactionID; civicrm_api3('ContributionRecur', 'create', $recurringContribution); @@ -469,4 +469,10 @@ public static function validateEwayContribution($paymentProcessor, $invoiceID) { } return NULL; } + + public static function contribution_status_id($name, $for_recur = FALSE) { + $baoName = $for_recur ? 'CRM_Contribute_BAO_ContributionRecur' : 'CRM_Contribute_BAO_Contribution'; + return \CRM_Core_PseudoConstant::getKey($baoName, 'contribution_status_id', $name); + } + } diff --git a/eWAYRecurring.php b/eWAYRecurring.php index 6c8a1bb..0559cb5 100644 --- a/eWAYRecurring.php +++ b/eWAYRecurring.php @@ -171,10 +171,6 @@ function ewayrecurring_civicrm_entityTypes(&$entityTypes) { ]; } -function _contribution_status_id($name) { - return CRM_Utils_Array::key($name, \CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')); -} - /** * @param $formName * @param $form CRM_Core_Form @@ -186,7 +182,7 @@ function ewayrecurring_civicrm_buildForm($formName, &$form) { ($crid = $form->getVar('contributionRecurID')) || ($crid = $form->getVar('_crid')); if ($crid) { $sql = 'SELECT next_sched_contribution_date FROM civicrm_contribution_recur WHERE id = %1'; - $form->addDateTime('next_scheduled_date', ts('Next Scheduled Date'), FALSE, ['formatType' => 'activityDateTime']); + $form->add('datepicker', 'next_scheduled_date', ts('Next Scheduled Date'), NULL, FALSE, ['formatType' => 'activityDateTime']); if ($default_nsd = CRM_Core_DAO::singleValueQuery($sql, [ 1 => [ $crid, @@ -222,7 +218,14 @@ function ewayrecurring_civicrm_buildForm($formName, &$form) { } function ewayrecurring_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) { - $paymentProcessorID = CRM_Core_ManagedEntities::singleton()->get('au.com.agileware.ewayrecurring', 'eWay_Recurring')['id']; + $paymentProcessorID = \Civi\Api4\Managed::get(FALSE) + ->addSelect('entity_id') + ->addWhere('module', '=', 'au.com.agileware.ewayrecurring') + ->addWhere('name', '=', 'eWay_Recurring') + ->setLimit(1) + ->execute() + ->first() + ['entity_id']; switch($formName) { case'CRM_Admin_Form_PaymentProcessor': diff --git a/tests/phpunit/CRM/EwayRecurring/TestCase.php b/tests/phpunit/CRM/EwayRecurring/TestCase.php index d43a4f9..86fa31c 100644 --- a/tests/phpunit/CRM/EwayRecurring/TestCase.php +++ b/tests/phpunit/CRM/EwayRecurring/TestCase.php @@ -354,7 +354,7 @@ protected function eventPriceSetCreate($feeTotal, $minAmt = 0, $type = 'Text') { $priceSet = CRM_Price_BAO_PriceSet::findById($result['id']); } else { - $priceSet = CRM_Price_BAO_PriceSet::create($paramsSet); + $priceSet = CRM_Price_BAO_PriceSet::writeRecord($paramsSet); } $this->_ids['price_set'] = $priceSet->id;