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

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