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

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.     private string $kernelDirectory;
  19.     public function __construct(string $kernelDirectory)
  20.     {
  21.         $this->kernelDirectory $kernelDirectory;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             CheckoutConfirmPageLoadedEvent::class => 'hideApplePayForNonSafariUsers',
  27.             AccountPaymentMethodPageLoadedEvent::class => 'hideApplePayForNonSafariUsers',
  28.             AccountEditOrderPageLoadedEvent::class => 'hideApplePayForNonSafariUsers',
  29.         ];
  30.     }
  31.     /**
  32.      * @param AccountPaymentMethodPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  33.      */
  34.     public function hideApplePayForNonSafariUsers($event): void
  35.     {
  36.         $paymentMethods $event->getPage()->getPaymentMethods();
  37.         $request $event->getRequest();
  38.         $userAgent $request->server->get('HTTP_USER_AGENT');
  39.         $browser = (new Browser($userAgent))->getName();
  40.         if ($browser === Browser::SAFARI && $this->isSetup() === true) {
  41.             return;
  42.         }
  43.         $paymentMethods $this->removePaymentMethod($paymentMethodsPayoneApplePay::UUID);
  44.         $event->getPage()->setPaymentMethods($paymentMethods);
  45.     }
  46.     private function isSetup(): bool
  47.     {
  48.         if (!file_exists($this->kernelDirectory ApplePayRoute::CERT_FOLDER 'merchant_id.key')) {
  49.             return false;
  50.         }
  51.         if (!file_exists($this->kernelDirectory ApplePayRoute::CERT_FOLDER 'merchant_id.pem')) {
  52.             return false;
  53.         }
  54.         return true;
  55.     }
  56. }