custom/plugins/PayonePayment/src/EventListener/CheckoutConfirmApplePayEventListener.php line 44

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\Core\Utils\PayoneClassLoader;
  5. use PayonePayment\PaymentMethod\PayoneApplePay;
  6. use PayonePayment\StoreApi\Route\ApplePayRoute;
  7. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  8. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  9. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  10. use Sinergi\BrowserDetector\Browser;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. if (file_exists(__DIR__ '/../../vendor/autoload.php')) {
  13.     (new PayoneClassLoader())->register();
  14. }
  15. class CheckoutConfirmApplePayEventListener implements EventSubscriberInterface
  16. {
  17.     use RemovesPaymentMethod;
  18.     /** @var string */
  19.     private $kernelDirectory;
  20.     public function __construct(string $kernelDirectory)
  21.     {
  22.         $this->kernelDirectory $kernelDirectory;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             CheckoutConfirmPageLoadedEvent::class      => 'hideApplePayForNonSafariUsers',
  28.             AccountPaymentMethodPageLoadedEvent::class => 'hideApplePayForNonSafariUsers',
  29.             AccountEditOrderPageLoadedEvent::class     => 'hideApplePayForNonSafariUsers',
  30.         ];
  31.     }
  32.     /**
  33.      * @param AccountPaymentMethodPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  34.      */
  35.     public function hideApplePayForNonSafariUsers($event): void
  36.     {
  37.         $paymentMethods $event->getPage()->getPaymentMethods();
  38.         $request   $event->getRequest();
  39.         $userAgent $request->server->get('HTTP_USER_AGENT');
  40.         $browser = (new Browser($userAgent))->getName();
  41.         if ($browser === Browser::SAFARI && $this->isSetup() === true) {
  42.             return;
  43.         }
  44.         $paymentMethods $this->removePaymentMethod($paymentMethodsPayoneApplePay::UUID);
  45.         $event->getPage()->setPaymentMethods($paymentMethods);
  46.     }
  47.     private function isSetup(): bool
  48.     {
  49.         if (!file_exists($this->kernelDirectory ApplePayRoute::CERT_FOLDER 'merchant_id.key')) {
  50.             return false;
  51.         }
  52.         if (!file_exists($this->kernelDirectory ApplePayRoute::CERT_FOLDER 'merchant_id.pem')) {
  53.             return false;
  54.         }
  55.         return true;
  56.     }
  57. }