vendor/store.shopware.com/mltisafemultisafepay/src/Subscriber/DocumentCreatedEvent.php line 69

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * Copyright © MultiSafepay, Inc. All rights reserved.
  4.  * See DISCLAIMER.md for disclaimer details.
  5.  */
  6. namespace MultiSafepay\Shopware6\Subscriber;
  7. use Exception;
  8. use MultiSafepay\Api\Transactions\UpdateRequest;
  9. use MultiSafepay\Exception\ApiException;
  10. use MultiSafepay\Exception\InvalidApiKeyException;
  11. use MultiSafepay\Shopware6\Factory\SdkFactory;
  12. use MultiSafepay\Shopware6\Util\OrderUtil;
  13. use MultiSafepay\Shopware6\Util\PaymentUtil;
  14. use Shopware\Core\Checkout\Order\OrderEvents;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class DocumentCreatedEvent implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var SdkFactory
  22.      */
  23.     private $sdkFactory;
  24.     /**
  25.      * @var PaymentUtil
  26.      */
  27.     private $paymentUtil;
  28.     /**
  29.      * @var OrderUtil
  30.      */
  31.     private $orderUtil;
  32.     /**
  33.      * DocumentCreatedEvent constructor.
  34.      *
  35.      * @param EntityRepository $orderRepository
  36.      * @param SdkFactory $sdkFactory
  37.      */
  38.     public function __construct(
  39.         SdkFactory $sdkFactory,
  40.         PaymentUtil $paymentUtil,
  41.         OrderUtil $orderUtil
  42.     ) {
  43.         $this->sdkFactory $sdkFactory;
  44.         $this->paymentUtil $paymentUtil;
  45.         $this->orderUtil $orderUtil;
  46.     }
  47.     /**
  48.      * {@inheritDoc}
  49.      */
  50.     public static function getSubscribedEvents(): array
  51.     {
  52.         return [
  53.             OrderEvents::ORDER_WRITTEN_EVENT => 'sendInvoiceToMultiSafepay',
  54.         ];
  55.     }
  56.     /**
  57.      * Send invoice to MultiSafepay when an order contains an invoice
  58.      *
  59.      * @param EntityWrittenEvent $event
  60.      */
  61.     public function sendInvoiceToMultiSafepay(EntityWrittenEvent $event)
  62.     {
  63.         try {
  64.             $context $event->getContext();
  65.             foreach ($event->getWriteResults() as $writeResult) {
  66.                 $payload $writeResult->getPayload();
  67.                 if (empty($payload) || !$this->paymentUtil->isMultiSafepayPaymentMethod($payload['id'], $context)) {
  68.                     continue;
  69.                 }
  70.                 try {
  71.                     $order $this->orderUtil->getOrder($payload['id'], $context);
  72.                     foreach ($order->getDocuments() as $document) {
  73.                         if ($document->getConfig()['name'] !== 'invoice') {
  74.                             continue 2;
  75.                         }
  76.                         $this->sdkFactory->create($order->getSalesChannelId())
  77.                             ->getTransactionManager()
  78.                             ->update(
  79.                                 $order->getOrderNumber(),
  80.                                 (new UpdateRequest())->addData([
  81.                                     'invoice_id' => $order->getDocuments()
  82.                                                         ->first()
  83.                                                         ->getConfig()['custom']['invoiceNumber'],
  84.                                 ])
  85.                             );
  86.                         break 2;
  87.                     }
  88.                 } catch (InvalidApiKeyException $invalidApiKeyException) {
  89.                     return;
  90.                 } catch (ApiException $apiException) {
  91.                     return;
  92.                 }
  93.             }
  94.         } catch (Exception $exception) {
  95.             return;
  96.         }
  97.     }
  98. }