custom/plugins/SwagPayPal/src/Checkout/PUI/PUISubscriber.php line 88

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\PayPal\Checkout\PUI;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  11. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  12. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  13. use Swag\PayPal\Checkout\Payment\Method\PUIHandler;
  14. use Swag\PayPal\Checkout\PUI\Service\PUIFraudNetDataService;
  15. use Swag\PayPal\Checkout\PUI\Service\PUIPaymentInstructionDataService;
  16. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  17. use Swag\PayPal\Setting\Service\SettingsValidationServiceInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. /**
  20.  * @internal
  21.  */
  22. class PUISubscriber implements EventSubscriberInterface
  23. {
  24.     public const PAYPAL_PUI_FRAUDNET_PAGE_EXTENSION_ID 'payPalPUIFraudNetPageData';
  25.     public const PAYPAL_PUI_PAYMENT_INSTRUCTIONS_PAGE_EXTENSION_ID 'payPalPUIFraudNetPageData';
  26.     private SettingsValidationServiceInterface $settingsValidationService;
  27.     private PUIFraudNetDataService $puiFraudNetDataService;
  28.     private PUIPaymentInstructionDataService $puiPaymentInstructionDataService;
  29.     private LoggerInterface $logger;
  30.     public function __construct(
  31.         SettingsValidationServiceInterface $settingsValidationService,
  32.         PUIFraudNetDataService $puiFraudNetDataService,
  33.         PUIPaymentInstructionDataService $puiPaymentInstructionDataService,
  34.         LoggerInterface $logger
  35.     ) {
  36.         $this->settingsValidationService $settingsValidationService;
  37.         $this->puiFraudNetDataService $puiFraudNetDataService;
  38.         $this->puiPaymentInstructionDataService $puiPaymentInstructionDataService;
  39.         $this->logger $logger;
  40.     }
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             AccountEditOrderPageLoadedEvent::class => 'onAccountOrderEditLoaded',
  45.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
  46.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishLoaded',
  47.         ];
  48.     }
  49.     public function onAccountOrderEditLoaded(AccountEditOrderPageLoadedEvent $event): void
  50.     {
  51.         if (!$this->checkSettings($event->getSalesChannelContext())) {
  52.             return;
  53.         }
  54.         $this->logger->debug('Adding data');
  55.         $buttonData $this->puiFraudNetDataService->buildCheckoutData($event->getSalesChannelContext());
  56.         $event->getPage()->addExtension(self::PAYPAL_PUI_FRAUDNET_PAGE_EXTENSION_ID$buttonData);
  57.         $this->logger->debug('Added data');
  58.     }
  59.     public function onCheckoutConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
  60.     {
  61.         if (!$this->checkSettings($event->getSalesChannelContext())) {
  62.             return;
  63.         }
  64.         if ($event->getPage()->getCart()->getErrors()->blockOrder()) {
  65.             return;
  66.         }
  67.         $this->logger->debug('Adding data');
  68.         $puiFraudNetData $this->puiFraudNetDataService->buildCheckoutData($event->getSalesChannelContext());
  69.         $event->getPage()->addExtension(self::PAYPAL_PUI_FRAUDNET_PAGE_EXTENSION_ID$puiFraudNetData);
  70.     }
  71.     public function onCheckoutFinishLoaded(CheckoutFinishPageLoadedEvent $event): void
  72.     {
  73.         if (!$this->checkSettings($event->getSalesChannelContext(), false)) {
  74.             return;
  75.         }
  76.         $transactions $event->getPage()->getOrder()->getTransactions();
  77.         if (!$transactions || !($transaction $transactions->last())) {
  78.             return;
  79.         }
  80.         $this->logger->debug('Adding data');
  81.         $puiPaymentInstructionData $this->puiPaymentInstructionDataService->buildFinishData($transaction$event->getSalesChannelContext());
  82.         if (!$puiPaymentInstructionData) {
  83.             return;
  84.         }
  85.         $event->getPage()->addExtension(self::PAYPAL_PUI_PAYMENT_INSTRUCTIONS_PAGE_EXTENSION_ID$puiPaymentInstructionData);
  86.     }
  87.     private function checkSettings(SalesChannelContext $salesChannelContextbool $checkPaymentMethod true): bool
  88.     {
  89.         if ($checkPaymentMethod && $salesChannelContext->getPaymentMethod()->getHandlerIdentifier() !== PUIHandler::class) {
  90.             return false;
  91.         }
  92.         try {
  93.             $this->settingsValidationService->validate($salesChannelContext->getSalesChannelId());
  94.         } catch (PayPalSettingsInvalidException $e) {
  95.             return false;
  96.         }
  97.         return true;
  98.     }
  99. }