/** * @param \Magento\Framework\Event\Observer $observer * * @throws \Exception * @throws bool */ public function execute(\Magento\Framework\Event\Observer $observer) { $merchantOrder = $observer->getMerchantOrder(); if (!count($merchantOrder['shipments']) > 0) { return; } $data = $observer->getPayment(); $order = $this->_coreModel->_getOrder($data["external_reference"]); //if order has shipments, status is updated. If it doesn't the shipment is created. if ($merchantOrder['shipments'][0]['status'] == 'ready_to_ship') { if ($order->hasShipments()) { $shipment = $this->_shipment->load($order->getId(), 'order_id'); } else { $shipment = $this->_shipmentFactory->create($order); $order->setIsInProcess(true); } $shipment->setShippingLabel($merchantOrder['shipments'][0]['id']); $shipmentInfo = $this->_shipmentHelper->getShipmentInfo($merchantOrder['shipments'][0]['id']); $this->_coreHelper->log("Shipment Info", 'mercadopago-notification.log', $shipmentInfo); $serviceInfo = $this->_shipmentHelper->getServiceInfo($merchantOrder['shipments'][0]['service_id'], $merchantOrder['site_id']); $this->_coreHelper->log("Service Info by service id", 'mercadopago-notification.log', $serviceInfo); if ($shipmentInfo && isset($shipmentInfo->tracking_number)) { $tracking['number'] = $shipmentInfo->tracking_number; $tracking['description'] = str_replace('#{trackingNumber}', $shipmentInfo->tracking_number, $serviceInfo->tracking_url); $tracking['title'] = self::CODE; $existingTracking = $this->_trackFactory->create()->load($shipment->getOrderId(), 'order_id'); if ($existingTracking->getId()) { $track = $shipment->getTrackById($existingTracking->getId()); $track->setNumber($tracking['number'])->setDescription($tracking['description'])->setTitle($tracking['title'])->save(); } else { $track = $this->_trackFactory->create()->addData($tracking); $track->setCarrierCode(\MercadoPago\MercadoEnvios\Model\Carrier\MercadoEnvios::CODE); $shipment->addTrack($track); $shipment->save(); } $this->_coreHelper->log("Track added", 'mercadopago-notification.log', $track); } $this->_transaction->addObject($order)->save(); } }