custom/plugins/RpayPayments/src/Components/DeviceFingerprint/Subscriber/DeviceFingerprintSubscriber.php line 112

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (c) Ratepay GmbH
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. namespace Ratepay\RpayPayments\Components\DeviceFingerprint\Subscriber;
  10. use RatePAY\Model\Request\SubModel\Head;
  11. use RatePAY\Model\Request\SubModel\Head\CustomerDevice;
  12. use Ratepay\RpayPayments\Components\Checkout\Event\OrderExtensionDataBuilt;
  13. use Ratepay\RpayPayments\Components\Checkout\Event\PaymentDataExtensionBuilt;
  14. use Ratepay\RpayPayments\Components\Checkout\Model\Extension\OrderExtension;
  15. use Ratepay\RpayPayments\Components\Checkout\Model\RatepayOrderDataEntity;
  16. use Ratepay\RpayPayments\Components\DeviceFingerprint\Constraint\DfpConstraint;
  17. use Ratepay\RpayPayments\Components\DeviceFingerprint\DfpServiceInterface;
  18. use Ratepay\RpayPayments\Components\PaymentHandler\Event\ValidationDefinitionCollectEvent;
  19. use Ratepay\RpayPayments\Components\PluginConfig\Service\ConfigService;
  20. use Ratepay\RpayPayments\Components\RatepayApi\Dto\PaymentRequestData;
  21. use Ratepay\RpayPayments\Components\RatepayApi\Event\BuildEvent;
  22. use Ratepay\RpayPayments\Components\RatepayApi\Service\Request\PaymentRequestService;
  23. use Shopware\Storefront\Event\RouteRequest\OrderRouteRequestEvent;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. use Symfony\Component\HttpFoundation\RequestStack;
  26. use Symfony\Component\Validator\Constraints\NotBlank;
  27. class DeviceFingerprintSubscriber implements EventSubscriberInterface
  28. {
  29.     protected DfpServiceInterface $dfpService;
  30.     private RequestStack $requestStack;
  31.     private ConfigService $configService;
  32.     public function __construct(
  33.         DfpServiceInterface $dfpService,
  34.         ConfigService $configService,
  35.         RequestStack $requestStack
  36.     ) {
  37.         $this->dfpService $dfpService;
  38.         $this->requestStack $requestStack;
  39.         $this->configService $configService;
  40.     }
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             PaymentDataExtensionBuilt::class => 'addRatepayTemplateData',
  45.             PaymentRequestService::EVENT_BUILD_HEAD => 'onPaymentRequest',
  46.             ValidationDefinitionCollectEvent::class => 'addValidationDefinition',
  47.             OrderExtensionDataBuilt::class => 'addDfpTokenToOrder',
  48.             OrderRouteRequestEvent::class => 'addRatepayDataToAccountOrderCriteria',
  49.         ];
  50.     }
  51.     public function onPaymentRequest(BuildEvent $buildEvent): void
  52.     {
  53.         /** @var PaymentRequestData $requestData */
  54.         $requestData $buildEvent->getRequestData();
  55.         /** @var Head $head */
  56.         $head $buildEvent->getBuildData();
  57.         $ratepayData $requestData->getRequestDataBag()->get('ratepay');
  58.         if ($token $ratepayData->get('deviceIdentToken') ?? null) {
  59.             $head->setCustomerDevice((new CustomerDevice())->setDeviceToken($token));
  60.         }
  61.     }
  62.     public function addRatepayTemplateData(PaymentDataExtensionBuilt $event): void
  63.     {
  64.         $baseData $event->getOrderEntity() ?? $event->getSalesChannelContext();
  65.         $snippet $this->dfpService->getDfpSnippet($this->requestStack->getCurrentRequest(), $baseData);
  66.         if ($snippet) {
  67.             $event->getExtension()->set('dfp', [
  68.                 'snippetId' => $this->configService->getDeviceFingerprintSnippetId(),
  69.                 'html' => $snippet,
  70.                 'deviceIdentToken' => $this->dfpService->generatedDfpId($this->requestStack->getCurrentRequest(), $baseData),
  71.             ]);
  72.         }
  73.     }
  74.     public function addValidationDefinition(ValidationDefinitionCollectEvent $event): void
  75.     {
  76.         if (!$this->dfpService->isDfpRequired($event->getBaseData())) {
  77.             return;
  78.         }
  79.         $event->addDefinition('deviceIdentToken', [
  80.             new NotBlank(),
  81.             new DfpConstraint($this->dfpService$event->getBaseData()),
  82.         ]);
  83.     }
  84.     public function addDfpTokenToOrder(OrderExtensionDataBuilt $event): void
  85.     {
  86.         $requestDataBag $event->getPaymentRequestData()->getRequestDataBag();
  87.         $ratepayData $requestDataBag->get('ratepay');
  88.         $data $event->getData();
  89.         $data[RatepayOrderDataEntity::FIELD_ADDITIONAL_DATA]['deviceIdentToken'] = $ratepayData->get('deviceIdentToken');
  90.         $event->setData($data);
  91.     }
  92.     public function addRatepayDataToAccountOrderCriteria(OrderRouteRequestEvent $event): void
  93.     {
  94.         $event->getCriteria()->addAssociation(OrderExtension::EXTENSION_NAME);
  95.     }
  96. }