Skip to content

Commit

Permalink
Merge pull request #139 from OXID-eSales/fix-noarticle-adyen-order
Browse files Browse the repository at this point in the history
request Adyen cancellation when finalizeOrder fails due to stock exceptions
  • Loading branch information
mariolorenz authored Jan 28, 2025
2 parents 742060a + 25da4f8 commit ddcd091
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Doctrine\DBAL\Query\QueryBuilder;
use OxidEsales\Eshop\Application\Model\Payment as EshopModelPayment;
use OxidEsales\Eshop\Application\Model\Basket;
use OxidEsales\Eshop\Core\Exception\ArticleInputException;
use OxidEsales\Eshop\Core\Exception\NoArticleException;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use OxidSolutionCatalysts\Adyen\Service\OrderIsAdyenCapturePossibleService;
Expand All @@ -21,6 +23,7 @@
use OxidSolutionCatalysts\Adyen\Service\PaymentCapture;
use OxidSolutionCatalysts\Adyen\Service\PaymentRefund;
use OxidSolutionCatalysts\Adyen\Service\Module as ModuleService;
use OxidSolutionCatalysts\Adyen\Service\SessionSettings;
use OxidSolutionCatalysts\Adyen\Traits\DataGetter;
use OxidSolutionCatalysts\Adyen\Traits\ServiceContainer;

Expand Down Expand Up @@ -92,7 +95,16 @@ public function isAdyenOrderPaid(): bool
*/
public function finalizeOrder(Basket $basket, $user, $recalcOrder = false)
{
$result = parent::finalizeOrder($basket, $user, $recalcOrder);
try {
$result = parent::finalizeOrder($basket, $user, $recalcOrder);
} catch (NoArticleException $oEx) {
$this->removeAdyenPaymentFromSession();
throw $oEx;
} catch (ArticleInputException $oEx) {
$this->removeAdyenPaymentFromSession();
throw $oEx;
}

$moduleService = $this->getServiceFromContainer(ModuleService::class);
if ($moduleService->isAdyenPayment($this->getAdyenStringData('oxpaymenttype'))) {
$pspReference = $this->getAdyenPSPReference();
Expand Down Expand Up @@ -470,4 +482,21 @@ public function setAdyenHistoryEntry(
$adyenHistory->setAdyenAction($action);
return (bool) $adyenHistory->save();
}

protected function removeAdyenPaymentFromSession(): void
{
$session = $this->getServiceFromContainer(SessionSettings::class);

// cancel authorization
$pspReference = $session->getPspReference();
$reference = $session->getOrderReference();
if ($pspReference && $reference) {
$paymentService = $this->getServiceFromContainer(PaymentCancel::class);
$paymentService->doAdyenCancel(
$pspReference,
$reference
);
$session->deletePaymentSession();
}
}
}

0 comments on commit ddcd091

Please sign in to comment.