custom/plugins/EasyCreditRatenkauf/src/Payment/Checkout.php line 77

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) NETZKOLLEKTIV GmbH <kontakt@netzkollektiv.com>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Netzkollektiv\EasyCredit\Payment;
  9. use Netzkollektiv\EasyCredit\Api\IntegrationFactory;
  10. use Netzkollektiv\EasyCredit\Api\Storage;
  11. use Netzkollektiv\EasyCredit\Helper\Payment as PaymentHelper;
  12. use Netzkollektiv\EasyCredit\Helper\Quote as QuoteHelper;
  13. use Netzkollektiv\EasyCredit\Setting\Exception\SettingsInvalidException;
  14. use Netzkollektiv\EasyCredit\Setting\Service\SettingsServiceInterface;
  15. use Netzkollektiv\EasyCredit\Cart\InterestError;
  16. use Netzkollektiv\EasyCredit\Service\FlexpriceService;
  17. use Netzkollektiv\EasyCredit\Payment\Handler\BillPaymentHandler;
  18. use Netzkollektiv\EasyCredit\Payment\Handler\InstallmentPaymentHandler;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  20. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  21. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Psr\Log\LoggerInterface;
  24. use Netzkollektiv\EasyCredit\Service\WebshopInfoService;
  25. class Checkout implements EventSubscriberInterface
  26. {
  27.     private PaymentHelper $paymentHelper;
  28.     private SettingsServiceInterface $settings;
  29.     private IntegrationFactory $integrationFactory;
  30.     private QuoteHelper $quoteHelper;
  31.     private Storage $storage;
  32.     private FlexpriceService $flexpriceService;
  33.     private LoggerInterface $logger;
  34.     private WebshopInfoService $webshopInfoService;
  35.     public function __construct(
  36.         Storage $storage,
  37.         PaymentHelper $paymentHelper,
  38.         QuoteHelper $quoteHelper,
  39.         SettingsServiceInterface $settings,
  40.         IntegrationFactory $integrationFactory,
  41.         WebshopInfoService $webshopInfoService,
  42.         LoggerInterface $logger,
  43.         FlexpriceService $flexpriceService
  44.     ) {
  45.         $this->storage $storage;
  46.         $this->paymentHelper $paymentHelper;
  47.         $this->quoteHelper $quoteHelper;
  48.         $this->settings $settings;
  49.         $this->integrationFactory $integrationFactory;
  50.         $this->webshopInfoService $webshopInfoService;
  51.         $this->logger $logger;
  52.         $this->flexpriceService $flexpriceService;
  53.     }
  54.     public static function getSubscribedEvents(): array
  55.     {
  56.         return [
  57.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
  58.         ];
  59.     }
  60.     /**
  61.      * @throws InconsistentCriteriaIdsException
  62.      */
  63.     public function onCheckoutConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
  64.     {
  65.         if (
  66.             $this->storage->get('redirect_url')
  67.             || $this->storage->get('init')
  68.         ) {
  69.             return;
  70.         }
  71.         $salesChannelContext $event->getSalesChannelContext();
  72.         $context $event->getContext();
  73.         $cart $event->getPage()->getCart();
  74.         if (!$this->paymentHelper->isEasyCreditInSalesChannel($salesChannelContext)) {
  75.             return;
  76.         }
  77.         $error null;
  78.         if ($this->storage->get('error')) {
  79.             $error $this->storage->get('error');
  80.             $this->storage->set('error'null)->persist();
  81.         }
  82.         foreach ($cart->getErrors()->getElements() as $cartError) {
  83.             if ($cartError instanceof InterestError) {
  84.                 $this->storage->clear();
  85.             }
  86.         }
  87.         try {
  88.             $salesChannelId $salesChannelContext->getSalesChannel()->getId();
  89.             $settings $this->settings->getSettings($salesChannelId);
  90.             $checkout $this->integrationFactory->createCheckout($salesChannelId);
  91.         } catch (SettingsInvalidException $e) {
  92.             $this->removePaymentMethodFromConfirmPage($event);
  93.             return;
  94.         }
  95.         try {
  96.             $this->webshopInfoService->getWebshopInfo($salesChannelId);
  97.         } catch (\Throwable $e) {
  98.             $this->logger->error($e->getMessage());
  99.             $this->removePaymentMethodFromConfirmPage($event);
  100.             return;
  101.         }
  102.         $isSelected $this->paymentHelper->isSelected($salesChannelContext);
  103.         if ($isSelected && !$this->storage->get('summary')) {
  104.             if ($error === null) {
  105.                 try {
  106.                     $quote $this->quoteHelper->getQuote($salesChannelContext$cart);
  107.                 } catch (\Throwable $e) {
  108.                     $error $e->getMessage();
  109.                 }
  110.             }
  111.         }
  112.         $paymentMethods $this->paymentHelper->getEasyCreditMethods($context);
  113.         $event->getPage()->addExtension('easycredit', (new CheckoutData())->assign([
  114.             'grandTotal' => isset($quote) ? $quote->getOrderDetails()->getOrderValue() : null,
  115.             'selectedPaymentMethod' => $salesChannelContext->getPaymentMethod()->getId(),
  116.             'paymentMethodIds' => [
  117.                 'installmentPaymentId' => $paymentMethods->filterByProperty('handlerIdentifier'InstallmentPaymentHandler::class)->first()->get('id'),
  118.                 'billPaymentId' => $paymentMethods->filterByProperty('handlerIdentifier'BillPaymentHandler::class)->first()->get('id')
  119.             ],
  120.             'approved' => $checkout->isApproved(),
  121.             'paymentPlan' => $this->buildPaymentPlan($this->storage->get('summary')),
  122.             'disableFlexprice' => $this->flexpriceService->shouldDisableFlexprice($salesChannelContext$cart),
  123.             'error' => $error,
  124.             'webshopId' => $settings->getWebshopId()
  125.         ]));
  126.     }
  127.     protected function buildPaymentPlan($summary)
  128.     {
  129.         if (empty($summary)) {
  130.             return null;
  131.         }
  132.         try {
  133.             $decoded \json_decode((string)$summarytrue512JSON_THROW_ON_ERROR);
  134.             return \json_encode($decoded);
  135.         } catch (\JsonException $e) {
  136.             return null;
  137.         }
  138.     }
  139.     private function removePaymentMethodFromConfirmPage(CheckoutConfirmPageLoadedEvent $event): void
  140.     {
  141.         $paymentMethodCollection $event->getPage()->getPaymentMethods();
  142.         foreach ($this->paymentHelper->getEasyCreditMethods($event->getContext()) as $method) {
  143.             $paymentMethodCollection->remove($method->get('id'));
  144.         }
  145.     }
  146. }