From 0211c92473546cf8b25df21436fedd135c09b871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20N=C3=B8rgaard?= Date: Sun, 25 Jan 2026 15:19:55 +0100 Subject: [PATCH] Add Drupal 11 compatibility and fix bugs - Add ^11 to core_version_requirement in commerce_payfast.info.yml - Remove broken SiteUrlService from services.yml (class doesn't exist, functionality already in PaymentOffsiteForm::getSiteUrl()) - Fix logger channel from 'your_module' to 'commerce_payfast' in: - OffsiteRedirect.php (3 places) - PaymentOffsiteForm.php (2 places) - Fix uninitialized $storage variable in loadPaymentByRemoteId(): - Initialize $storage = NULL before try block - Add early return FALSE in catch blocks to prevent undefined var error - Add proper use statements for InvalidPluginDefinitionException and PluginNotFoundException Tested on Drupal 11.1.7 with Commerce 3.0 and PHP 8.3. All changes are backwards compatible with Drupal 9.3+ and 10.x. --- payfast/commerce_payfast.info.yml | 2 +- payfast/commerce_payfast.services.yml | 5 +---- .../Commerce/PaymentGateway/OffsiteRedirect.php | 17 ++++++++++++----- .../OffsiteRedirect/PaymentOffsiteForm.php | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/payfast/commerce_payfast.info.yml b/payfast/commerce_payfast.info.yml index 876bedd..60e0c28 100644 --- a/payfast/commerce_payfast.info.yml +++ b/payfast/commerce_payfast.info.yml @@ -8,5 +8,5 @@ dependencies: # Information added by Drupal.org packaging script version: '1.5.0' -core_version_requirement: ^9.3 || ^10 +core_version_requirement: ^9.3 || ^10 || ^11 project: 'commerce_payfast' diff --git a/payfast/commerce_payfast.services.yml b/payfast/commerce_payfast.services.yml index f4e0e09..ad189dd 100644 --- a/payfast/commerce_payfast.services.yml +++ b/payfast/commerce_payfast.services.yml @@ -1,4 +1 @@ -services: - your_module.site_url_service: - class: 'Drupal\commerce_payfast\SiteUrlService' - arguments: [ '@request_stack' ] +services: {} diff --git a/payfast/src/Plugin/Commerce/PaymentGateway/OffsiteRedirect.php b/payfast/src/Plugin/Commerce/PaymentGateway/OffsiteRedirect.php index d485bf3..7bbf773 100644 --- a/payfast/src/Plugin/Commerce/PaymentGateway/OffsiteRedirect.php +++ b/payfast/src/Plugin/Commerce/PaymentGateway/OffsiteRedirect.php @@ -12,6 +12,8 @@ use Drupal\commerce_payment\PaymentStorage; use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayBase; use Drupal\commerce_price\Price; +use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; +use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Form\FormStateInterface; use Payfast\PayfastCommon\Aggregator\Request\PaymentRequest; @@ -131,20 +133,25 @@ protected function getRequestDataArray(string $request_content): array protected function loadPaymentByRemoteId($remote_id): mixed { $message = '@message'; + $storage = NULL; + /** @var PaymentStorage $storage */ try { $storage = $this->entityTypeManager->getStorage('commerce_payment'); - } catch (Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException $e) { - Drupal::logger('your_module')->error( + } catch (InvalidPluginDefinitionException $e) { + Drupal::logger('commerce_payfast')->error( 'Invalid plugin definition exception: @message', [$message => $e->getMessage()] ); - } catch (Drupal\Component\Plugin\Exception\PluginNotFoundException $e) { - Drupal::logger('your_module')->error( + return FALSE; + } catch (PluginNotFoundException $e) { + Drupal::logger('commerce_payfast')->error( 'Plugin not found exception: @message', [$message => $e->getMessage()] ); + return FALSE; } + $payment_by_remote_id = $storage->loadByProperties(['order_id' => $remote_id]); return reset($payment_by_remote_id); @@ -273,7 +280,7 @@ public function processOrder($pfError, $pfData, $pfErrMsg): void // Mark order as completed in state storage \Drupal::state()->set('payfast_order_notified_' . $order_id, true); } catch (EntityStorageException $e) { - Drupal::logger('your_module')->error( + Drupal::logger('commerce_payfast')->error( 'Entity storage exception: @message', [$message => $e->getMessage()] ); diff --git a/payfast/src/PluginForm/OffsiteRedirect/PaymentOffsiteForm.php b/payfast/src/PluginForm/OffsiteRedirect/PaymentOffsiteForm.php index 31c6958..b26e99a 100644 --- a/payfast/src/PluginForm/OffsiteRedirect/PaymentOffsiteForm.php +++ b/payfast/src/PluginForm/OffsiteRedirect/PaymentOffsiteForm.php @@ -53,7 +53,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta try { $payment->save(); } catch (EntityStorageException $e) { - Drupal::logger('your_module')->error( + Drupal::logger('commerce_payfast')->error( 'Entity storage exception: @message', ['@message' => $e->getMessage()] ); @@ -115,7 +115,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta $redirect_form = []; $redirect_form = $this->buildRedirectForm($form, $form_state, $url, $data, 'post'); } catch (NeedsRedirectException $e) { - Drupal::logger('your_module')->error( + Drupal::logger('commerce_payfast')->error( 'Needs redirect exception: @message', ['@message' => $e->getMessage()] );