From 0546941df27e2899baf60b8104cc9c47b9d82e6d Mon Sep 17 00:00:00 2001 From: Tadas Labutis Date: Tue, 3 Mar 2026 10:37:13 +0200 Subject: [PATCH] Fix zero amount sent to SaferPay API for products with dynamic pricing Pre-compute cart total before order creation to prevent third-party modules from affecting the amount sent to SaferPay Initialize API. When order creation rule is "Before authorization", validateOrder triggers hooks that can clean up dynamic pricing data, causing subsequent cart total reads to return 0. --- src/Processor/CheckoutProcessor.php | 11 ++++++++--- .../Request/InitializeRequestObjectCreator.php | 11 +++++++---- src/Service/SaferPayInitialize.php | 6 ++++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Processor/CheckoutProcessor.php b/src/Processor/CheckoutProcessor.php index b3b1df31a..4347a24a2 100644 --- a/src/Processor/CheckoutProcessor.php +++ b/src/Processor/CheckoutProcessor.php @@ -88,6 +88,8 @@ public function run(CheckoutData $data) throw CouldNotProcessCheckout::failedToFindCart($data->getCartId()); } + $cartTotal = (int) round($cart->getOrderTotal(true) * SaferPayConfig::AMOUNT_MULTIPLIER_FOR_API); + if (!$data->getCreateAfterAuthorization()) { $this->processCreateOrder($cart, $data->getPaymentMethod()); } @@ -109,7 +111,8 @@ public function run(CheckoutData $data) $data->getSelectedCard(), $data->getFieldToken(), $data->getSuccessController(), - $data->getIsWebhook() + $data->getIsWebhook(), + $cartTotal ); } catch (\Exception $exception) { throw new SaferPayApiException('Failed to initialize payment API', SaferPayApiException::INITIALIZE); @@ -196,7 +199,8 @@ private function processInitializePayment( $selectedCard, $fieldToken, $successController, - $isWebhook + $isWebhook, + $cartTotal = null ) { $request = $this->saferPayInitialize->buildRequest( $paymentMethod, @@ -204,7 +208,8 @@ private function processInitializePayment( $selectedCard, $fieldToken, $successController, - $isWebhook + $isWebhook, + $cartTotal ); return $this->saferPayInitialize->initialize($request, $isBusinessLicense); diff --git a/src/Service/Request/InitializeRequestObjectCreator.php b/src/Service/Request/InitializeRequestObjectCreator.php index 4278d9ed8..5abb8db55 100755 --- a/src/Service/Request/InitializeRequestObjectCreator.php +++ b/src/Service/Request/InitializeRequestObjectCreator.php @@ -57,14 +57,17 @@ public function create( $customerId, $isBusinessLicence, $alias = null, - $fieldToken = null + $fieldToken = null, + $cartTotal = null ) { $requestHeader = $this->requestObjectCreator->createRequestHeader(); $terminalId = Configuration::get(SaferPayConfig::TERMINAL_ID . SaferPayConfig::getConfigSuffix()); - $cartDetails = $cart->getSummaryDetails(); - $totalPrice = $cartDetails['total_price'] * SaferPayConfig::AMOUNT_MULTIPLIER_FOR_API; - $totalPrice = (int) (round($totalPrice)); + $totalPrice = $cartTotal; + if ($cartTotal === null) { + $cartDetails = $cart->getSummaryDetails(); + $totalPrice = (int) round($cartDetails['total_price'] * SaferPayConfig::AMOUNT_MULTIPLIER_FOR_API); + } $payment = $this->requestObjectCreator->createPayment($cart, $totalPrice); $payer = new Payer(); diff --git a/src/Service/SaferPayInitialize.php b/src/Service/SaferPayInitialize.php index 4e4befa11..cdc5ca9fd 100755 --- a/src/Service/SaferPayInitialize.php +++ b/src/Service/SaferPayInitialize.php @@ -111,7 +111,8 @@ public function buildRequest( $selectedCard = -1, $fieldToken = null, $successController = null, - $isWebhook = 1 + $isWebhook = 1, + $cartTotal = null ) { $customerEmail = $this->context->customer->email; $cartId = $this->context->cart->id; @@ -155,7 +156,8 @@ public function buildRequest( $this->context->cart->id_customer, $isBusinessLicence, $alias, - $fieldToken + $fieldToken, + $cartTotal ); return $initializeRequest;