vendor/store.shopware.com/swagpaypal/src/Checkout/PUI/PUISubscriber.php line 85

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. class PUISubscriber implements EventSubscriberInterface
  20. {
  21.     public const PAYPAL_PUI_FRAUDNET_PAGE_EXTENSION_ID 'payPalPUIFraudNetPageData';
  22.     public const PAYPAL_PUI_PAYMENT_INSTRUCTIONS_PAGE_EXTENSION_ID 'payPalPUIFraudNetPageData';
  23.     private SettingsValidationServiceInterface $settingsValidationService;
  24.     private PUIFraudNetDataService $puiFraudNetDataService;
  25.     private PUIPaymentInstructionDataService $puiPaymentInstructionDataService;
  26.     private LoggerInterface $logger;
  27.     public function __construct(
  28.         SettingsValidationServiceInterface $settingsValidationService,
  29.         PUIFraudNetDataService $puiFraudNetDataService,
  30.         PUIPaymentInstructionDataService $puiPaymentInstructionDataService,
  31.         LoggerInterface $logger
  32.     ) {
  33.         $this->settingsValidationService $settingsValidationService;
  34.         $this->puiFraudNetDataService $puiFraudNetDataService;
  35.         $this->puiPaymentInstructionDataService $puiPaymentInstructionDataService;
  36.         $this->logger $logger;
  37.     }
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             AccountEditOrderPageLoadedEvent::class => 'onAccountOrderEditLoaded',
  42.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
  43.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishLoaded',
  44.         ];
  45.     }
  46.     public function onAccountOrderEditLoaded(AccountEditOrderPageLoadedEvent $event): void
  47.     {
  48.         if (!$this->checkSettings($event->getSalesChannelContext())) {
  49.             return;
  50.         }
  51.         $this->logger->debug('Adding data');
  52.         $buttonData $this->puiFraudNetDataService->buildCheckoutData($event->getSalesChannelContext());
  53.         $event->getPage()->addExtension(self::PAYPAL_PUI_FRAUDNET_PAGE_EXTENSION_ID$buttonData);
  54.         $this->logger->debug('Added data');
  55.     }
  56.     public function onCheckoutConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
  57.     {
  58.         if (!$this->checkSettings($event->getSalesChannelContext())) {
  59.             return;
  60.         }
  61.         if ($event->getPage()->getCart()->getErrors()->blockOrder()) {
  62.             return;
  63.         }
  64.         $this->logger->debug('Adding data');
  65.         $puiFraudNetData $this->puiFraudNetDataService->buildCheckoutData($event->getSalesChannelContext());
  66.         $event->getPage()->addExtension(self::PAYPAL_PUI_FRAUDNET_PAGE_EXTENSION_ID$puiFraudNetData);
  67.     }
  68.     public function onCheckoutFinishLoaded(CheckoutFinishPageLoadedEvent $event): void
  69.     {
  70.         if (!$this->checkSettings($event->getSalesChannelContext(), false)) {
  71.             return;
  72.         }
  73.         $transactions $event->getPage()->getOrder()->getTransactions();
  74.         if (!$transactions || !($transaction $transactions->last())) {
  75.             return;
  76.         }
  77.         $this->logger->debug('Adding data');
  78.         $puiPaymentInstructionData $this->puiPaymentInstructionDataService->buildFinishData($transaction$event->getSalesChannelContext());
  79.         if (!$puiPaymentInstructionData) {
  80.             return;
  81.         }
  82.         $event->getPage()->addExtension(self::PAYPAL_PUI_PAYMENT_INSTRUCTIONS_PAGE_EXTENSION_ID$puiPaymentInstructionData);
  83.     }
  84.     private function checkSettings(SalesChannelContext $salesChannelContextbool $checkPaymentMethod true): bool
  85.     {
  86.         if ($checkPaymentMethod && $salesChannelContext->getPaymentMethod()->getHandlerIdentifier() !== PUIHandler::class) {
  87.             return false;
  88.         }
  89.         try {
  90.             $this->settingsValidationService->validate($salesChannelContext->getSalesChannelId());
  91.         } catch (PayPalSettingsInvalidException $e) {
  92.             return false;
  93.         }
  94.         return true;
  95.     }
  96. }