custom/plugins/MolliePayments/src/Subscriber/ApplePayDirectSubscriber.php line 82

Open in your IDE?
  1. <?php
  2. namespace Kiener\MolliePayments\Subscriber;
  3. use Kiener\MolliePayments\Components\ApplePayDirect\ApplePayDirect;
  4. use Kiener\MolliePayments\Handler\Method\ApplePayPayment;
  5. use Kiener\MolliePayments\Service\SettingsService;
  6. use Mollie\Shopware\Entity\Order\MollieShopwareOrder;
  7. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  8. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  9. use Shopware\Storefront\Event\StorefrontRenderEvent;
  10. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ApplePayDirectSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var SettingsService
  16.      */
  17.     private $settingsService;
  18.     /**
  19.      * @var ApplePayDirect
  20.      */
  21.     private $applePay;
  22.     /**
  23.      * @param SettingsService $settingsService
  24.      * @param ApplePayDirect $applePay
  25.      */
  26.     public function __construct(SettingsService $settingsServiceApplePayDirect $applePay)
  27.     {
  28.         $this->settingsService $settingsService;
  29.         $this->applePay $applePay;
  30.     }
  31.     /**
  32.      * @inheritDoc
  33.      */
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             StorefrontRenderEvent::class => 'onStorefrontRender',
  38.             CheckoutFinishPageLoadedEvent::class => 'onRestoreBackup',
  39.         ];
  40.     }
  41.     /**
  42.      * @param StorefrontRenderEvent $event
  43.      * @throws \Exception
  44.      * @return void
  45.      */
  46.     public function onStorefrontRender(StorefrontRenderEvent $event): void
  47.     {
  48.         $settings $this->settingsService->getSettings($event->getSalesChannelContext()->getSalesChannel()->getId());
  49.         $applePayDirectEnabled $this->applePay->isApplePayDirectEnabled($event->getSalesChannelContext());
  50.         $shoPhoneNumberField $settings->isPhoneNumberFieldRequired() || $settings->isPhoneNumberFieldShown();
  51.         $applePayPaymentMethodId "";
  52.         try {
  53.             $applePayPaymentMethodId $this->applePay->getActiveApplePayID($event->getSalesChannelContext());
  54.         } catch (\Exception $exception) {
  55.         }
  56.         $event->setParameter('mollie_applepaydirect_phonenumber_required', (int)$shoPhoneNumberField);
  57.         $event->setParameter('mollie_applepaydirect_enabled'$applePayDirectEnabled);
  58.         $event->setParameter('mollie_applepaydirect_restrictions'$settings->getRestrictApplePayDirect());
  59.         $event->setParameter('mollie_express_required_data_protection'$settings->isRequireDataProtectionCheckbox() && $event->getSalesChannelContext()->getCustomer() == null);
  60.         $event->setParameter('apple_pay_payment_method_id'$applePayPaymentMethodId);
  61.     }
  62.     /**
  63.      * If our apple pay direct payment is done, we want to restore the original cart
  64.      * just in case if the customer had some items in there.
  65.      * @param CheckoutFinishPageLoadedEvent $event
  66.      * @return void
  67.      */
  68.     public function onRestoreBackup(CheckoutFinishPageLoadedEvent $event): void
  69.     {
  70.         $context $event->getSalesChannelContext();
  71.         $mollieShopwareOrder = new MollieShopwareOrder($event->getPage()->getOrder());
  72.         $latestTransaction $mollieShopwareOrder->getLatestTransaction();
  73.         if (!$latestTransaction instanceof OrderTransactionEntity) {
  74.             return;
  75.         }
  76.         $paymentMethod $latestTransaction->getPaymentMethod();
  77.         if (!$paymentMethod instanceof PaymentMethodEntity) {
  78.             return;
  79.         }
  80.         $paymentIdentifier $paymentMethod->getHandlerIdentifier();
  81.         # Apple Pay direct will automatically restore a previous cart once the checkout is done
  82.         # the user does not really work in the shopware checkout, and therefore it's good
  83.         # if the cart remains the same as before the Apple Pay direct checkout
  84.         if ($paymentIdentifier === ApplePayPayment::class) {
  85.             $this->applePay->restoreCart($context);
  86.         }
  87.     }
  88. }