custom/plugins/LtcKomfortkasse6/src/Subscriber/OrderSubscriber.php line 42

Open in your IDE?
  1. <?php
  2. declare(strict_types 1)
  3.     ;
  4. namespace Ltc\Komfortkasse\Subscriber;
  5. use Shopware\Core\Checkout\Order\OrderEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class OrderSubscriber implements EventSubscriberInterface
  12. {
  13.     /** @var SystemConfigService */
  14.     private $systemConfigService;
  15.     /** @var EntityRepositoryInterface */
  16.     private $orderRepo;
  17.     public function __construct(SystemConfigService $systemConfigServiceEntityRepositoryInterface $orderRepo)
  18.     {
  19.         $this->systemConfigService $systemConfigService;
  20.         $this->orderRepo $orderRepo;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [ OrderEvents::ORDER_WRITTEN_EVENT => 'onInsertOrder'
  25.         ];
  26.     }
  27.     public function onInsertOrder(EntityWrittenEvent $event)
  28.     {
  29.         $active $this->systemConfigService->get('LtcKomfortkasse6.config.active');
  30.         if (!$active)
  31.             return;
  32.         foreach ($event->getWriteResults() as $result) {
  33.             $orderCriteria = new Criteria([ $result->getPrimaryKey()
  34.             ]);
  35.             $orderCriteria->addAssociation('salesChannel.domains');
  36.             $orderCriteria->addAssociation('transactions.paymentMethod');
  37.             $orders $this->orderRepo->search($orderCriteria$event->getContext())->getEntities();
  38.             foreach ($orders as $order) {
  39.                 $urls = array ();
  40.                 $channel $order->getSalesChannel();
  41.                 if (!$this->systemConfigService->get('LtcKomfortkasse6.config.active'$channel->getId()))
  42.                     continue;
  43.                 foreach ($channel->getDomains() as $domain) {
  44.                     $urls [] = $domain->getUrl();
  45.                 }
  46.                 $paymentMethods = array();
  47.                 foreach ($order->getTransactions() as $t) {
  48.                     if (!in_array($t->getPaymentMethodId(), $paymentMethods))
  49.                         $paymentMethods [] = $t->getPaymentMethodId();
  50.                     if ($t->getPaymentMethod() && !in_array($t->getPaymentMethod()->getFormattedHandlerIdentifier(), $paymentMethods))
  51.                         $paymentMethods [] = $t->getPaymentMethod()->getFormattedHandlerIdentifier();
  52.                 }
  53.                 
  54.                 $query http_build_query(array ('id' => $order->getId(),'url' => $urls'payment_method' => $paymentMethods));
  55.                 $ch curl_init();
  56.                 curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  57.                 curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
  58.                 curl_setopt($chCURLOPT_URL'https://ssl.komfortkasse.eu/api/shop/neworder.jsf');
  59.                 curl_setopt($chCURLOPT_POSTtrue);
  60.                 curl_setopt($chCURLOPT_NOSIGNAL1);
  61.                 curl_setopt($chCURLOPT_CONNECTTIMEOUT0);
  62.                 curl_setopt($chCURLOPT_TIMEOUT_MS1001);
  63.                 curl_setopt($chCURLOPT_POSTFIELDS$query);
  64.                 @curl_exec($ch);
  65.                 @curl_close ($ch);
  66.             }
  67.         }
  68.     }
  69. }