custom/plugins/RpayPayments/src/Components/Checkout/Subscriber/UserDataSubscriber.php line 46

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\Checkout\Subscriber;
  10. use DateTime;
  11. use Ratepay\RpayPayments\Components\PaymentHandler\Event\BeforePaymentEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class UserDataSubscriber implements EventSubscriberInterface
  17. {
  18.     private EntityRepositoryInterface $addressRepository;
  19.     private EntityRepositoryInterface $customerRepository;
  20.     private EntityRepositoryInterface $orderAddressRepository;
  21.     public function __construct(
  22.         EntityRepositoryInterface $customerRepository,
  23.         EntityRepositoryInterface $orderAddressRepository,
  24.         EntityRepositoryInterface $addressRepository
  25.     ) {
  26.         $this->customerRepository $customerRepository;
  27.         $this->orderAddressRepository $orderAddressRepository;
  28.         $this->addressRepository $addressRepository;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             BeforePaymentEvent::class => 'saveUserData',
  34.         ];
  35.     }
  36.     public function saveUserData(BeforePaymentEvent $event): void
  37.     {
  38.         $paymentRequestData $event->getPaymentRequestData();
  39.         $order $paymentRequestData->getOrder();
  40.         $customer $order->getOrderCustomer()->getCustomer();
  41.         $defaultBillingAddress $customer $this->addressRepository->search(new Criteria([$customer->getDefaultBillingAddressId()]), $paymentRequestData->getContext())->first() : null;
  42.         $orderBillingAddress $order->getAddresses()->get($order->getBillingAddressId());
  43.         $dataBag $paymentRequestData->getRequestDataBag();
  44.         if ($customer === null || $orderBillingAddress === null) {
  45.             // should never occurs.
  46.             throw new \RuntimeException('user data can not be saved. Unknown error.');
  47.         }
  48.         /** @var RequestDataBag $ratepayData */
  49.         $ratepayData $dataBag->get('ratepay');
  50.         if (!$ratepayData) {
  51.             return;
  52.         }
  53.         $customerUpdates $defaultBillingAddressUpdates $orderBillingAddressUpdates = [];
  54.         // collect updates
  55.         if ($ratepayData->has('birthday')) {
  56.             /** @var RequestDataBag $birthday */
  57.             $birthday $ratepayData->get('birthday');
  58.             $date = (new DateTime())->setDate(
  59.                 $birthday->getInt('year'),
  60.                 $birthday->getInt('month'),
  61.                 $birthday->getInt('day')
  62.             );
  63.             $customer->setBirthday($date);
  64.             $customerUpdates['birthday'] = $date;
  65.         }
  66.         if ($ratepayData->has('phone') && !empty($phone $ratepayData->get('phone'))) {
  67.             $defaultBillingAddressUpdates['phoneNumber'] = $phone;
  68.             $orderBillingAddress->setPhoneNumber($phone);
  69.             $orderBillingAddressUpdates['phoneNumber'] = $phone;
  70.         }
  71.         if ($ratepayData->has('vatId') && !empty($vatId $ratepayData->get('vatId'))) {
  72.             $orderBillingAddress->setVatId($vatId);
  73.             $orderBillingAddressUpdates['vatId'] = $vatId;
  74.             $customerUpdates['vatIds'] = [$vatId];
  75.         }
  76.         // update collected data
  77.         if (count($customerUpdates)) {
  78.             $this->customerRepository->upsert([array_merge(
  79.                 [
  80.                     'id' => $customer->getId(),
  81.                     'versionId' => $customer->getVersionId(),
  82.                 ],
  83.                 $customerUpdates
  84.             )], $event->getContext());
  85.         }
  86.         if ($defaultBillingAddress && count($defaultBillingAddressUpdates)) {
  87.             $this->addressRepository->upsert([array_merge(
  88.                 [
  89.                     'id' => $defaultBillingAddress->getId(),
  90.                 ],
  91.                 $defaultBillingAddressUpdates
  92.             )], $event->getContext());
  93.         }
  94.         if (count($orderBillingAddressUpdates)) {
  95.             $this->orderAddressRepository->upsert([array_merge(
  96.                 [
  97.                     'id' => $orderBillingAddress->getId(),
  98.                     'versionId' => $orderBillingAddress->getVersionId(),
  99.                 ],
  100.                 $orderBillingAddressUpdates
  101.             )], $event->getContext());
  102.         }
  103.     }
  104. }