custom/plugins/UnzerPayment6/src/EventListeners/Checkout/FinishPageEventListener.php line 44

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace UnzerPayment6\EventListeners\Checkout;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use UnzerPayment6\Components\ClientFactory\ClientFactoryInterface;
  8. use UnzerPayment6\Components\Struct\InstallmentSecured\InstallmentInfo;
  9. use UnzerPayment6\Components\Struct\PageExtension\Checkout\FinishPageExtension;
  10. use UnzerPayment6\Components\TransactionSelectionHelper\TransactionSelectionHelperInterface;
  11. use UnzerSDK\Exceptions\UnzerApiException;
  12. use UnzerSDK\Resources\InstalmentPlan;
  13. use UnzerSDK\Resources\Payment;
  14. use UnzerSDK\Unzer;
  15. class FinishPageEventListener implements EventSubscriberInterface
  16. {
  17.     /** @var ClientFactoryInterface */
  18.     private $clientFactory;
  19.     /** @var LoggerInterface */
  20.     private $logger;
  21.     /** @var TransactionSelectionHelperInterface */
  22.     private $transactionSelectionHelper;
  23.     public function __construct(ClientFactoryInterface $clientFactoryLoggerInterface $loggerTransactionSelectionHelperInterface $transactionSelectionHelper)
  24.     {
  25.         $this->clientFactory              $clientFactory;
  26.         $this->logger                     $logger;
  27.         $this->transactionSelectionHelper $transactionSelectionHelper;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinish',
  33.         ];
  34.     }
  35.     public function onCheckoutFinish(CheckoutFinishPageLoadedEvent $event): void
  36.     {
  37.         $salesChannelContext $event->getSalesChannelContext();
  38.         $page                $event->getPage();
  39.         $unzerTransaction    $this->transactionSelectionHelper->getBestUnzerTransaction($page->getOrder());
  40.         if (!$unzerTransaction) {
  41.             return;
  42.         }
  43.         try {
  44.             $unzerClient $this->clientFactory->createClient($salesChannelContext->getSalesChannel()->getId());
  45.         } catch (\RuntimeException $ex) {
  46.             $this->logger->error($ex->getMessage());
  47.             return;
  48.         }
  49.         $extension = new FinishPageExtension();
  50.         $payment   $this->getPaymentByOrderId($unzerClient$unzerTransaction->getId());
  51.         if (!$payment) {
  52.             $payment $this->getPaymentByOrderId($unzerClient$unzerTransaction->getOrderId());
  53.             if (!$payment) {
  54.                 return;
  55.             }
  56.         }
  57.         $paymentType $payment->getPaymentType();
  58.         if ($paymentType instanceof InstalmentPlan) {
  59.             $installmentInfo = (new InstallmentInfo())->fromInstalmentPlan($paymentType);
  60.             $extension->addInstallmentInfo($installmentInfo);
  61.         }
  62.         $event->getPage()->addExtension(FinishPageExtension::EXTENSION_NAME$extension);
  63.     }
  64.     private function getPaymentByOrderId(Unzer $unzerClientstring $orderId): ?Payment
  65.     {
  66.         try {
  67.             return $unzerClient->fetchPaymentByOrderId($orderId);
  68.         } catch (UnzerApiException $exception) {
  69.             //catch payment not found exception so that shopware can handle its own errors
  70.             $this->logger->error($exception->getMessage(), [
  71.                 'code'          => $exception->getCode(),
  72.                 'clientMessage' => $exception->getClientMessage(),
  73.                 'file'          => $exception->getFile(),
  74.                 'trace'         => $exception->getTraceAsString(),
  75.             ]);
  76.         }
  77.         return null;
  78.     }
  79. }