Your IP : 216.73.216.43


Current Path : /var/www/www.indacotrentino.com/www/app/code/Torresani/SiWeGO/Observer/
Upload File :
Current File : //var/www/www.indacotrentino.com/www/app/code/Torresani/SiWeGO/Observer/OrderPlaceBefore.php

<?php

namespace Torresani\SiWeGO\Observer;

use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartItemInterfaceFactory;
use Magento\Sales\Model\Order\Item as OrderItem;

use Magento\Quote\Api\CartItemRepositoryInterface;
use Magento\Quote\Model\Quote\Item\ToOrderItem;

class OrderPlaceBefore implements ObserverInterface
{
    protected $orderRepository;
    protected $logger;
    protected $orderAggregated;
    protected $quoteRepository;

    protected $cartItemRepository;
    protected $cartRepository;
    protected $toOrderItem;

    protected $_productRepository;
    protected $formKey;

    public function __construct(
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Sales\Api\Data\OrderInterface $orderAggregated,
        \Magento\Quote\Api\Data\CartItemInterfaceFactory $cartItemFactory,
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
        LoggerInterface $logger,

        CartItemRepositoryInterface $cartItemRepository,
        CartRepositoryInterface $cartRepository,
        \Magento\Quote\Model\Quote\Item\ToOrderItem $toOrderItem,

        \Magento\Catalog\Model\ProductRepository $productRepository,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Framework\Data\Form\FormKey $formKey
    ) {
        $this->orderRepository = $orderRepository;
        $this->orderAggregated = $orderAggregated;
        $this->cartItemFactory = $cartItemFactory;
        $this->quoteRepository = $quoteRepository;
        $this->logger = $logger;

        $this->cartItemRepository = $cartItemRepository;
        $this->cartRepository = $cartRepository;
        $this->toOrderItem = $toOrderItem;

        $this->_productRepository = $productRepository;
        $this->formKey = $formKey;
    }

    /**
     * Execute observer
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        $order = $observer->getEvent()->getOrder();
        if ($order->getShippingMethod() === 'siwego_siwego_nonso') {
            if (preg_match('/\((.*?)\)/', $order->getShippingDescription(), $matches)) {
                $sessionId = $matches[1];

                $username='indacostaging';
                $password='Fa_R^7.2@jfqD&zDbS4';
                $URL='https://stagingapi.siwego.app/partners/' . $sessionId . '/confirm';

                $data = array(
                    'callback' => 'https://magento.test.indacotrentino.com/rest/V1/siwego/setState',
                    'warehouse' => array(
                        'address' => 'Via Vienna, 7',
                        'company' => 'Delivery srl',
                        'email' => 'info@asdasdasda.it',
                        'mobilePhone' => '3333333333',
                        'telephone' => '0461123456',
                        'openingHours' => '2023-08-24T10:49:50.499Z',
                        'closingHours' => '2023-08-24T10:49:50.499Z',
                        'qrCode' => 'qrCode_warehouse'
                    )
                );

                $data_string = json_encode($data);

                $ch = curl_init();
                $headers = array(
                    'Content-Type:application/json',
                    'Authorization: Basic '. base64_encode("$username:$password")
                );
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                curl_setopt($ch, CURLOPT_URL,$URL);
                curl_setopt($ch, CURLOPT_TIMEOUT, 15);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

                $result = curl_exec($ch);
                if ($result === false) {
                    $this->logger->info(curl_error($ch));
                    echo 'Errore cURL: ' . curl_error($ch);
                }
                curl_close($ch);

                $r = json_decode($result);
                $urlSiWeGO = $r->deepLinkUrl;

                $message = "Per continuare la contrattazione sul trasporto del tuo pacco accedi a SiWeGO tramite questo <a href='" . $urlSiWeGO . "'>link personalizzato</a>.";
                $order->addStatusHistoryComment($message)
                    ->setIsVisibleOnFront(true)
                    ->setIsCustomerNotified(true);

                $order->setSiwegoUrl($urlSiWeGO);
                $order->setSiwegoSessionid($sessionId);
                $order->save();

            }
        }
    }
}