custom/plugins/PolPaymentPayolutionSW6/src/EventListener/CheckoutFinishEventListener.php line 98

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PolPaymentPayolutionSW6\EventListener;
  4. use PolPaymentPayolutionSW6\Component\CustomFields\CustomFieldsInterface;
  5. use PolPaymentPayolutionSW6\Component\DataHandler\OrderTransactionDataHandler\OrderTransactionDataHandlerInterface;
  6. use PolPaymentPayolutionSW6\Component\DataHandler\OrderTransactionStatusHandler\OrderTransactionStatusHandlerInterface;
  7. use PolPaymentPayolutionSW6\Component\PaymentMethodConfiguration\PaymentMethodConfigurationServiceInterface;
  8. use PolPaymentPayolutionSW6\Component\Validator\PaymentRequestValidator\PaymentRequestValidatorInterface;
  9. use PolPaymentPayolutionSW6\Event\PreAuthFinished;
  10. use PolPaymentPayolutionSW6\PayolutionApi\Client\Exception\InvalidXmlException;
  11. use PolPaymentPayolutionSW6\PayolutionApi\Process\Exception\ProcessFailedException;
  12. use PolPaymentPayolutionSW6\PayolutionApi\Request\RequestFactory\PaymentRequestFactoryInterface;
  13. use PolPaymentPayolutionSW6\PayolutionApi\Request\RequestFactory\Transaction\TransactionRequestFactoryInterface;
  14. use PolPaymentPayolutionSW6\PayolutionApi\Request\RequestProcessor\RequestProcessorInterface;
  15. use Psr\Log\LoggerInterface;
  16. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  17. use Shopware\Core\Framework\Context;
  18. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  21. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  22. use Shopware\Storefront\Event\RouteRequest\HandlePaymentMethodRouteRequestEvent;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. class CheckoutFinishEventListener implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     private $orderTransactionRepository;
  30.     /**
  31.      * @var OrderTransactionDataHandlerInterface
  32.      */
  33.     private $orderTransactionDataHandler;
  34.     /**
  35.      * @var OrderTransactionStatusHandlerInterface
  36.      */
  37.     private $orderTransactionStatusHandler;
  38.     /**
  39.      * @var TransactionRequestFactoryInterface
  40.      */
  41.     private $transactionRequestFactory;
  42.     /**
  43.      * @var PaymentRequestFactoryInterface
  44.      */
  45.     private $paymentRequestFactory;
  46.     /**
  47.      * @var RequestProcessorInterface
  48.      */
  49.     private $captureRequestProcessor;
  50.     /**
  51.      * @var PaymentMethodConfigurationServiceInterface
  52.      */
  53.     private $paymentMethodConfigurationService;
  54.     /**
  55.      * @var LoggerInterface
  56.      */
  57.     private $logger;
  58.     public function __construct(
  59.         EntityRepositoryInterface $orderTransactionRepository,
  60.         OrderTransactionDataHandlerInterface $orderTransactionDataHandler,
  61.         OrderTransactionStatusHandlerInterface $orderTransactionStatusHandler,
  62.         TransactionRequestFactoryInterface $transactionRequestFactory,
  63.         PaymentRequestFactoryInterface $paymentRequestFactory,
  64.         RequestProcessorInterface $captureRequestProcessor,
  65.         PaymentMethodConfigurationServiceInterface $paymentMethodConfigurationService,
  66.         LoggerInterface $logger
  67.     ) {
  68.         $this->orderTransactionRepository $orderTransactionRepository;
  69.         $this->orderTransactionDataHandler $orderTransactionDataHandler;
  70.         $this->orderTransactionStatusHandler $orderTransactionStatusHandler;
  71.         $this->transactionRequestFactory $transactionRequestFactory;
  72.         $this->paymentRequestFactory $paymentRequestFactory;
  73.         $this->captureRequestProcessor $captureRequestProcessor;
  74.         $this->paymentMethodConfigurationService $paymentMethodConfigurationService;
  75.         $this->logger $logger;
  76.     }
  77.     public static function getSubscribedEvents(): array
  78.     {
  79.         return [
  80.             PreAuthFinished::class => 'onPreAuthFinish',
  81.             HandlePaymentMethodRouteRequestEvent::class => 'addRequiredRequestParameter',
  82.         ];
  83.     }
  84.     public function onPreAuthFinish(PreAuthFinished $event): void
  85.     {
  86.         $criteria = new Criteria();
  87.         $criteria->addFilter(new EqualsFilter('orderId'$event->getOrder()->getId()));
  88.         $criteria->addAssociations(['order.customer''order.currency''order.lineItems''order.deliveries''order.salesChannel''paymentMethod']);
  89.         $context $event->getContext();
  90.         /** @var OrderTransactionEntity $orderTransaction */
  91.         $orderTransaction $this->orderTransactionRepository->search($criteria$context)->filter(function (OrderTransactionEntity $orderTransaction) use ($event): bool {
  92.             return $orderTransaction->getPaymentMethodId() === $event->getSalesChannelContext()->getPaymentMethod()->getId();
  93.         })->last();
  94.         $this->updateOrderTransactionLineItems($orderTransaction$context);
  95.         if (!$this->paymentMethodConfigurationService->paymentMethodSupportsAutoCapture($event->getSalesChannelContext()->getPaymentMethod()) || $orderTransaction->getCustomFields()[CustomFieldsInterface::IS_PAYMENT_CAPTURED]) {
  96.             return;
  97.         }
  98.         try {
  99.             $transaction $this->transactionRequestFactory->createTransaction(
  100.                 $orderTransaction,
  101.                 new RequestDataBag(),
  102.                 $orderTransaction->getAmount()->getTotalPrice(),
  103.                 null,
  104.                 [PaymentRequestValidatorInterface::IS_CAPTURE_REQUEST => true]
  105.             );
  106.             $captureRequest $this->paymentRequestFactory->createRequest($orderTransaction$transaction$orderTransaction->getOrder()->getSalesChannel()->getId());
  107.             $captureRequest->setPreAuthorizationUniqueId($orderTransaction->getCustomFields()[CustomFieldsInterface::UNIQUE_ID]);
  108.             $captureResponse $this->captureRequestProcessor->processRequest($captureRequest);
  109.             $this->orderTransactionDataHandler->updateOrderTransactionData($orderTransaction$context, [
  110.                 CustomFieldsInterface::IS_PAYMENT_CAPTURED => true,
  111.                 CustomFieldsInterface::REMAINING_CAPTURE_AMOUNT => 0,
  112.                 CustomFieldsInterface::REMAINING_REFUND_AMOUNT => $orderTransaction->getAmount()->getTotalPrice(),
  113.             ]);
  114.             foreach ($orderTransaction->getOrder()->getLineItems() as $orderLineItem) {
  115.                 $this->orderTransactionDataHandler->updateOrderLineItemData($orderLineItem$context, [CustomFieldsInterface::IS_PAYMENT_CAPTURED => true]);
  116.             }
  117.             $this->orderTransactionStatusHandler->setPaid($orderTransaction$context);
  118.             $this->logger->info('payment auto captured due enabled setting'$captureResponse->getResponseAsArray());
  119.         } catch (InvalidXmlException $xmlException) {
  120.             $this->logger->error('payment auto capture invalid xml', ['message' => $xmlException->getMessage()]);
  121.         } catch (ProcessFailedException $processFailedException) {
  122.             $this->logger->error('payment auto capture process failed'$processFailedException->getResponse()->getResponseAsArray());
  123.         } catch (\Throwable $e) {
  124.             $this->logger->error('payment auto capture error occurred', ['message' => $e->getMessage()]);
  125.         }
  126.     }
  127.     public function addRequiredRequestParameter(HandlePaymentMethodRouteRequestEvent $event): void
  128.     {
  129.         $this->logger->debug('Adding request parameter');
  130.         $storefrontRequest $event->getStorefrontRequest();
  131.         $storeApiRequest $event->getStoreApiRequest();
  132.         $originalRoute $storefrontRequest->attributes->get('_route');
  133.         if ($originalRoute !== 'frontend.account.edit-order.update-order') {
  134.             return;
  135.         }
  136.         $requiredRequestParameter $this->getRequiredRequestParameter();
  137.         foreach ($requiredRequestParameter as $requestParameter) {
  138.             $storeApiRequest->request->set(
  139.                 $requestParameter,
  140.                 $storefrontRequest->request->get($requestParameter)
  141.             );
  142.             $storeApiRequest->request->set(
  143.                 $requestParameter,
  144.                 $storefrontRequest->request->get($requestParameter)
  145.             );
  146.         }
  147.         $this->logger->debug('Added request parameter');
  148.     }
  149.     private function getRequiredRequestParameter(): array
  150.     {
  151.         return [
  152.             PaymentRequestValidatorInterface::PRE_CHECK_ID,
  153.             PaymentRequestValidatorInterface::TRANSACTION_ID,
  154.             PaymentRequestValidatorInterface::CALCULATION_ID,
  155.             PaymentRequestValidatorInterface::B2B_COMPANY_TYPE,
  156.             PaymentRequestValidatorInterface::B2B_COMPANY_NAME,
  157.             PaymentRequestValidatorInterface::B2B_COMPANY_VAT_ID,
  158.             PaymentRequestValidatorInterface::B2B_COMPANY_OWNER_FIRST_NAME,
  159.             PaymentRequestValidatorInterface::B2B_COMPANY_OWNER_LAST_NAME,
  160.             PaymentRequestValidatorInterface::B2B_COMPANY_ACCEPT_CREDIT_CHECK,
  161.             PaymentRequestValidatorInterface::BIRTHDAY_DAY,
  162.             PaymentRequestValidatorInterface::BIRTHDAY_MONTH,
  163.             PaymentRequestValidatorInterface::BIRTHDAY_YEAR,
  164.             PaymentRequestValidatorInterface::INVOICE_ACCEPT_CREDIT_CHECK,
  165.             PaymentRequestValidatorInterface::INSTALLMENTS_ACCEPT_CREDIT_CHECK,
  166.             PaymentRequestValidatorInterface::INSTALLMENTS_DURATION,
  167.             PaymentRequestValidatorInterface::INSTALLMENTS_ACCOUNT_HOLDER,
  168.             PaymentRequestValidatorInterface::INSTALLMENTS_ACCOUNT_IBAN,
  169.             PaymentRequestValidatorInterface::DIRECT_DEBIT_MANDATE_OWNER,
  170.             PaymentRequestValidatorInterface::DIRECT_DEBIT_MANDATE_IBAN,
  171.             PaymentRequestValidatorInterface::DIRECT_DEBIT_ACCEPT_CREDIT_CHECK,
  172.             PaymentRequestValidatorInterface::DIRECT_DEBIT_ACCEPT_MANDATE,
  173.         ];
  174.     }
  175.     private function updateOrderTransactionLineItems(OrderTransactionEntity $orderTransactionContext $context): void
  176.     {
  177.         foreach ($orderTransaction->getOrder()->getLineItems() as $orderLineItem) {
  178.             $this->orderTransactionDataHandler->updateOrderLineItemData(
  179.                 $orderLineItem,
  180.                 $context,
  181.                 [
  182.                     CustomFieldsInterface::IS_PAYOLUTION_PAYMENT => true,
  183.                     CustomFieldsInterface::IS_PAYMENT_CAPTURED => false,
  184.                     CustomFieldsInterface::IS_PAYMENT_REFUNDED => false,
  185.                 ]
  186.             );
  187.         }
  188.     }
  189. }