diff --git a/CHANGELOG.md b/CHANGELOG.md index 09388000..f4f9f1bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## [2.2.0] - 2024-??-?? ### NEW - provide content for smarty and twig diff --git a/src/Controller/OrderController.php b/src/Controller/OrderController.php index bb370936..9f4a3121 100644 --- a/src/Controller/OrderController.php +++ b/src/Controller/OrderController.php @@ -10,12 +10,14 @@ use OxidEsales\Eshop\Application\Model\Country; use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Application\Model\Payment; use OxidEsales\Eshop\Core\DatabaseProvider; use OxidEsales\Eshop\Core\Exception\DatabaseErrorException; use OxidEsales\Eshop\Core\Registry; use OxidSolutionCatalysts\Unzer\Exception\Redirect; use OxidSolutionCatalysts\Unzer\Exception\RedirectWithMessage; -use OxidSolutionCatalysts\Unzer\Model\Payment; +use OxidSolutionCatalysts\Unzer\Model\Order as UnzerOrder; +use OxidSolutionCatalysts\Unzer\Model\Payment as UnzerPayment; use OxidSolutionCatalysts\Unzer\Service\ModuleSettings; use OxidSolutionCatalysts\Unzer\Service\Payment as PaymentService; use OxidSolutionCatalysts\Unzer\Service\ResponseHandler; @@ -25,6 +27,7 @@ use OxidSolutionCatalysts\Unzer\Traits\ServiceContainer; use OxidSolutionCatalysts\Unzer\Service\UnzerDefinitions; use OxidSolutionCatalysts\Unzer\Core\UnzerDefinitions as CoreUnzerDefinitions; +use UnzerSDK\Constants\PaymentState; use UnzerSDK\Exceptions\UnzerApiException; /** @@ -109,10 +112,12 @@ public function unzerExecuteAfterRedirect(): void // get basket contents $oUser = $this->getUser(); $oBasket = Registry::getSession()->getBasket(); - if ($oBasket->getProductsCount()) { + if (!$oBasket->getProductsCount()) { + return; + } $oDB = DatabaseProvider::getDb(); - /** @var \OxidSolutionCatalysts\Unzer\Model\Order $oOrder */ + /** @var UnzerOrder $oOrder */ $oOrder = $this->getActualOrder(); $oDB->startTransaction(); @@ -127,28 +132,31 @@ public function unzerExecuteAfterRedirect(): void $unzerService = $this->getServiceFromContainer(Unzer::class); Registry::getSession()->setVariable('orderDisableSqlActiveSnippet', false); - if (stripos($nextStep, 'thankyou') !== false) { + if (stripos($nextStep, 'thankyou') !== false) { $oDB->commitTransaction(); + $paymentService = $this->getServiceFromContainer(PaymentService::class); - $paymentService = $this->getServiceFromContainer(PaymentService::class); - if ($unzerService->ifImmediatePostAuthCollect($paymentService)) { - $paymentService->doUnzerCollect( - $oOrder, - $oUser->getId(), - $oBasket->getDiscountedProductsBruttoPrice() - ); - } + if ($this->isPaymentCancelled($paymentService)) { + $this->redirectUserToCheckout($unzerService, $oOrder); + } - throw new Redirect($unzerService->prepareRedirectUrl($nextStep)); + if ($unzerService->ifImmediatePostAuthCollect($paymentService)) { + $paymentService->doUnzerCollect( + $oOrder, + $oUser->getId(), + $oBasket->getDiscountedProductsBruttoPrice() + ); } + throw new Redirect($unzerService->prepareRedirectUrl($nextStep)); + } + $oDB->rollbackTransaction(); $translator = $this->getServiceFromContainer(Translator::class); throw new RedirectWithMessage( $unzerService->prepareRedirectUrl($nextStep), $translator->translate('OSCUNZER_ERROR_DURING_CHECKOUT') ); - } } /** @@ -195,7 +203,7 @@ public function isSepaConfirmed(): ?bool */ public function saveUnzerTransaction(): void { - /** @var \OxidSolutionCatalysts\Unzer\Model\Order $order */ + /** @var UnzerOrder $order */ $order = $this->getActualOrder(); $order->initWriteTransactionToDB(); } @@ -244,7 +252,7 @@ public function getUserCountryIso(): string */ public function getActualOrder(): Order { - if (!($this->actualOrder instanceof \OxidSolutionCatalysts\Unzer\Model\Order)) { + if (!($this->actualOrder instanceof Order)) { $this->actualOrder = oxNew(Order::class); /** @var string $sess_challenge */ $sess_challenge = Registry::getSession()->getVariable('sess_challenge'); @@ -302,7 +310,7 @@ public function executeoscunzer(): ?string } $paymentService = $this->getServiceFromContainer(PaymentService::class); - /** @var \OxidEsales\Eshop\Application\Model\Payment $payment */ + /** @var Payment $payment */ $payment = $this->getPayment(); $paymentOk = $paymentService->executeUnzerPayment($payment); @@ -320,7 +328,7 @@ public function executeoscunzer(): ?string */ public function getExecuteFnc() { - /** @var Payment $payment */ + /** @var UnzerPayment $payment */ $payment = $this->getPayment(); if ( $payment->isUnzerPayment() @@ -380,4 +388,26 @@ protected function getTrancactionIds(): array } return $result; } + + private function isPaymentCancelled(PaymentService $paymentService): bool + { + $return = false; + $sessionUnzerPaymentObj = $paymentService->getSessionUnzerPayment(); + if ($sessionUnzerPaymentObj !== null) { + $return = $sessionUnzerPaymentObj->getState() === PaymentState::STATE_CANCELED; + } + + return $return; + } + + private function redirectUserToCheckout(Unzer $unzerService, Order $order): void + { + $translator = $this->getServiceFromContainer(Translator::class); + /** @var UnzerOrder $order */ + $unzerOrderNr = $order->getUnzerOrderNr(); + throw new RedirectWithMessage( + $unzerService->prepareRedirectUrl('payment?payerror=-6'), + sprintf($translator->translate('OSCUNZER_CANCEL_DURING_CHECKOUT'), $unzerOrderNr) + ); + } } diff --git a/tests/PhpMd/standard.xml b/tests/PhpMd/standard.xml index 1084f7b5..65ea6410 100644 --- a/tests/PhpMd/standard.xml +++ b/tests/PhpMd/standard.xml @@ -23,5 +23,5 @@ - + *OrderController.php diff --git a/translations/de/oscunzer_lang.php b/translations/de/oscunzer_lang.php index b551c8ef..bee22ea0 100644 --- a/translations/de/oscunzer_lang.php +++ b/translations/de/oscunzer_lang.php @@ -15,6 +15,7 @@ // Error 'OSCUNZER_ERROR_DURING_CHECKOUT' => 'Bei der Abwicklung der Zahlung ist ein Fehler aufgetreten. Der Prozess wurde rückgängig gemacht. Bitte wählen Sie alternativ eine andere Zahlart aus.', + 'OSCUNZER_CANCEL_DURING_CHECKOUT' => 'Die Zahlung wurde abgebrochen. Diese Bestellung ist als "nicht abgeschlossen" unter der Nummer %s, in Ihrem Kundenkonto gespeichert, damit Sie den Vorgang nachvollziehen können.', // Invoice 'OSCUNZER_BANK_DETAILS_AMOUNT' => 'Bitte überweisen sie den Betrag von %s %s auf folgendes Bankkonto:

', diff --git a/translations/en/oscunzer_lang.php b/translations/en/oscunzer_lang.php index e5b30353..9c0dae04 100644 --- a/translations/en/oscunzer_lang.php +++ b/translations/en/oscunzer_lang.php @@ -15,6 +15,7 @@ // Error 'OSCUNZER_ERROR_DURING_CHECKOUT' => 'An error occurred while processing the payment. The process was reversed. Alternatively, please select another payment method.', + 'OSCUNZER_CANCEL_DURING_CHECKOUT' => 'The payment was cancelled. This order is saved as "not finished" under the number %s in your customer account so that you can track the process.', //Invoice 'OSCUNZER_BANK_DETAILS_AMOUNT' => 'Please transfer the amount of %s %s to the following account:

',