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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\DataAbstractionLayer\Aggregate\PayonePaymentOrderTransactionDataEntity;
  5. use PayonePayment\DataAbstractionLayer\Entity\Mandate\PayonePaymentMandateEntity;
  6. use PayonePayment\DataAbstractionLayer\Extension\PayonePaymentOrderTransactionExtension;
  7. use PayonePayment\Installer\PaymentMethodInstaller;
  8. use PayonePayment\Storefront\Struct\CheckoutFinishPaymentData;
  9. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  10. use Shopware\Core\Checkout\Order\OrderEntity;
  11. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  12. use Shopware\Core\Framework\Context;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class CheckoutFinishEventListener implements EventSubscriberInterface
  19. {
  20.     private EntityRepositoryInterface $mandateRepository;
  21.     private EntityRepositoryInterface $orderTransactionRepository;
  22.     public function __construct(
  23.         EntityRepositoryInterface $mandateRepository,
  24.         EntityRepositoryInterface $orderTransactionRepository
  25.     ) {
  26.         $this->mandateRepository $mandateRepository;
  27.         $this->orderTransactionRepository $orderTransactionRepository;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinish',
  33.         ];
  34.     }
  35.     public function onCheckoutFinish(CheckoutFinishPageLoadedEvent $event): void
  36.     {
  37.         $salesChannelContext $event->getSalesChannelContext();
  38.         $context $salesChannelContext->getContext();
  39.         if (!$this->isPayonePayment($salesChannelContext->getPaymentMethod())) {
  40.             return;
  41.         }
  42.         $mandateIdentification $this->getMandateIdentification(
  43.             $event->getPage()->getOrder(),
  44.             $context
  45.         );
  46.         if ($mandateIdentification === null) {
  47.             return;
  48.         }
  49.         if (!$this->hasDirectDebitPayment($mandateIdentification)) {
  50.             return;
  51.         }
  52.         $payoneData = new CheckoutFinishPaymentData();
  53.         $payoneData->assign([
  54.             'mandate' => $this->getMandate($mandateIdentification$context),
  55.         ]);
  56.         $event->getPage()->addExtension(CheckoutFinishPaymentData::EXTENSION_NAME$payoneData);
  57.     }
  58.     protected function hasDirectDebitPayment(?string $mandateIdentification): bool
  59.     {
  60.         return $mandateIdentification !== null;
  61.     }
  62.     private function isPayonePayment(PaymentMethodEntity $paymentMethod): bool
  63.     {
  64.         return \in_array($paymentMethod->getId(), PaymentMethodInstaller::PAYMENT_METHOD_IDStrue);
  65.     }
  66.     private function getMandate(string $mandateIdentificationContext $context): ?PayonePaymentMandateEntity
  67.     {
  68.         $criteria = new Criteria();
  69.         $criteria->addFilter(new EqualsFilter('identification'$mandateIdentification));
  70.         return $this->mandateRepository->search($criteria$context)->first();
  71.     }
  72.     private function getMandateIdentification(OrderEntity $orderContext $context): ?string
  73.     {
  74.         $criteria = new Criteria();
  75.         $criteria->addFilter(new EqualsFilter('orderId'$order->getId()));
  76.         /** @var OrderTransactionEntity[] $transactions */
  77.         $transactions $this->orderTransactionRepository->search($criteria$context);
  78.         foreach ($transactions as $transaction) {
  79.             /** @var PayonePaymentOrderTransactionDataEntity|null $payoneTransactionData */
  80.             $payoneTransactionData $transaction->getExtension(PayonePaymentOrderTransactionExtension::NAME);
  81.             if ($payoneTransactionData !== null && !empty($payoneTransactionData->getMandateIdentification())) {
  82.                 return $payoneTransactionData->getMandateIdentification();
  83.             }
  84.         }
  85.         return null;
  86.     }
  87. }