custom/plugins/RpayPayments/src/Components/InstallmentCalculator/Subscriber/CheckoutSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Ratepay GmbH
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Ratepay\RpayPayments\Components\InstallmentCalculator\Subscriber;
  9. use Ratepay\RpayPayments\Components\Checkout\Event\PaymentDataExtensionBuilt;
  10. use Ratepay\RpayPayments\Components\InstallmentCalculator\Model\InstallmentCalculatorContext;
  11. use Ratepay\RpayPayments\Components\InstallmentCalculator\Service\InstallmentService;
  12. use Ratepay\RpayPayments\Util\MethodHelper;
  13. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class CheckoutSubscriber implements EventSubscriberInterface
  16. {
  17.     private InstallmentService $installmentService;
  18.     private CartService $cartService;
  19.     public function __construct(
  20.         InstallmentService $installmentService,
  21.         CartService $cartService
  22.     )
  23.     {
  24.         $this->installmentService $installmentService;
  25.         $this->cartService $cartService;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             PaymentDataExtensionBuilt::class => 'buildCheckoutExtension',
  31.         ];
  32.     }
  33.     public function buildCheckoutExtension(PaymentDataExtensionBuilt $event): void
  34.     {
  35.         $paymentMethod $event->getSalesChannelContext()->getPaymentMethod();
  36.         $salesChannelContext $event->getSalesChannelContext();
  37.         $order $event->getOrderEntity();
  38.         $extension $event->getExtension();
  39.         if (MethodHelper::isInstallmentMethod($paymentMethod->getHandlerIdentifier())) {
  40.             $calcContext = (new InstallmentCalculatorContext($salesChannelContext''''))
  41.                 ->setPaymentMethodId($paymentMethod->getId())
  42.                 ->setOrder($order);
  43.             if (!$order) {
  44.                 $calcContext->setTotalAmount($this->cartService->getCart($salesChannelContext->getToken(), $salesChannelContext)->getPrice()->getTotalPrice());
  45.             }
  46.             $installmentCalculator $this->installmentService->getInstallmentCalculatorData($calcContext);
  47.             $calcContext->setCalculationType($installmentCalculator['defaults']['type']);
  48.             $calcContext->setCalculationValue($installmentCalculator['defaults']['value']);
  49.             $vars $this->installmentService->getInstallmentPlanTwigVars($calcContext);
  50.             $vars['calculator'] = $installmentCalculator;
  51.             $extension->offsetSet('installment'$vars);
  52.         }
  53.     }
  54. }