/** * @return $this */ protected function _getExpressRate() { $rate = $this->_rateMethodFactory->create(); $rate->setCarrier($this->_code); $rate->setCarrierTitle($this->getConfigData('title')); $rate->setMethodTitle('Express delivery'); $rate->setPrice(2.5); $rate->setCost(0); $this->_result->append($rate); return $this; }
/** * Parse xml tracking response * * @param string $trackingvalue * @param string $response * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function _parseXmlTrackingResponse($trackingvalue, $response) { $errorTitle = __('For some reason we can\'t retrieve tracking info right now.'); $resultArr = []; if (strlen(trim($response)) > 0) { if (strpos(trim($response), '<?xml') === 0) { $xml = $this->parseXml($response); if (is_object($xml)) { if (isset($xml->Number) && isset($xml->Description) && (string) $xml->Description != '') { $errorTitle = (string) $xml->Description; } elseif (isset($xml->TrackInfo) && isset($xml->TrackInfo->Error) && isset($xml->TrackInfo->Error->Description) && (string) $xml->TrackInfo->Error->Description != '') { $errorTitle = (string) $xml->TrackInfo->Error->Description; } else { $errorTitle = __('Sorry, something went wrong. Please try again or contact us and we\'ll try to help.'); } if (isset($xml->TrackInfo) && isset($xml->TrackInfo->TrackSummary)) { $resultArr['tracksummary'] = (string) $xml->TrackInfo->TrackSummary; } } } } if (!$this->_result) { $this->_result = $this->_trackFactory->create(); } $defaults = $this->getDefaults(); if ($resultArr) { $tracking = $this->_trackStatusFactory->create(); $tracking->setCarrier('usps'); $tracking->setCarrierTitle($this->getConfigData('title')); $tracking->setTracking($trackingvalue); $tracking->setTrackSummary($resultArr['tracksummary']); $this->_result->append($tracking); } else { $error = $this->_trackErrorFactory->create(); $error->setCarrier('usps'); $error->setCarrierTitle($this->getConfigData('title')); $error->setTracking($trackingvalue); $error->setErrorMessage($errorTitle); $this->_result->append($error); } }
/** * Parse tracking response * * @param string[] $trackingValue * @param \stdClass $response * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function _parseTrackingResponse($trackingValue, $response) { if (is_object($response)) { if ($response->HighestSeverity == 'FAILURE' || $response->HighestSeverity == 'ERROR') { $errorTitle = (string) $response->Notifications->Message; } elseif (isset($response->TrackDetails)) { $trackInfo = $response->TrackDetails; $resultArray['status'] = (string) $trackInfo->StatusDescription; $resultArray['service'] = (string) $trackInfo->ServiceInfo; $timestamp = isset($trackInfo->EstimatedDeliveryTimestamp) ? $trackInfo->EstimatedDeliveryTimestamp : $trackInfo->ActualDeliveryTimestamp; $timestamp = strtotime((string) $timestamp); if ($timestamp) { $resultArray['deliverydate'] = date('Y-m-d', $timestamp); $resultArray['deliverytime'] = date('H:i:s', $timestamp); } $deliveryLocation = isset($trackInfo->EstimatedDeliveryAddress) ? $trackInfo->EstimatedDeliveryAddress : $trackInfo->ActualDeliveryAddress; $deliveryLocationArray = []; if (isset($deliveryLocation->City)) { $deliveryLocationArray[] = (string) $deliveryLocation->City; } if (isset($deliveryLocation->StateOrProvinceCode)) { $deliveryLocationArray[] = (string) $deliveryLocation->StateOrProvinceCode; } if (isset($deliveryLocation->CountryCode)) { $deliveryLocationArray[] = (string) $deliveryLocation->CountryCode; } if ($deliveryLocationArray) { $resultArray['deliverylocation'] = implode(', ', $deliveryLocationArray); } $resultArray['signedby'] = (string) $trackInfo->DeliverySignatureName; $resultArray['shippeddate'] = date('Y-m-d', (int) $trackInfo->ShipTimestamp); if (isset($trackInfo->PackageWeight) && isset($trackInfo->Units)) { $weight = (string) $trackInfo->PackageWeight; $unit = (string) $trackInfo->Units; $resultArray['weight'] = "{$weight} {$unit}"; } $packageProgress = []; if (isset($trackInfo->Events)) { $events = $trackInfo->Events; if (isset($events->Address)) { $events = [$events]; } foreach ($events as $event) { $tempArray = []; $tempArray['activity'] = (string) $event->EventDescription; $timestamp = strtotime((string) $event->Timestamp); if ($timestamp) { $tempArray['deliverydate'] = date('Y-m-d', $timestamp); $tempArray['deliverytime'] = date('H:i:s', $timestamp); } if (isset($event->Address)) { $addressArray = []; $address = $event->Address; if (isset($address->City)) { $addressArray[] = (string) $address->City; } if (isset($address->StateOrProvinceCode)) { $addressArray[] = (string) $address->StateOrProvinceCode; } if (isset($address->CountryCode)) { $addressArray[] = (string) $address->CountryCode; } if ($addressArray) { $tempArray['deliverylocation'] = implode(', ', $addressArray); } } $packageProgress[] = $tempArray; } } $resultArray['progressdetail'] = $packageProgress; } } if (!$this->_result) { $this->_result = $this->_trackFactory->create(); } if (isset($resultArray)) { $tracking = $this->_trackStatusFactory->create(); $tracking->setCarrier('fedex'); $tracking->setCarrierTitle($this->getConfigData('title')); $tracking->setTracking($trackingValue); $tracking->addData($resultArray); $this->_result->append($tracking); } else { $error = $this->_trackErrorFactory->create(); $error->setCarrier('fedex'); $error->setCarrierTitle($this->getConfigData('title')); $error->setTracking($trackingValue); $error->setErrorMessage($errorTitle ? $errorTitle : __('For some reason we can\'t retrieve tracking info right now.')); $this->_result->append($error); } }
/** * Parse xml tracking response * * @param string $trackingValue * @param string $xmlResponse * @return null * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function _parseXmlTrackingResponse($trackingValue, $xmlResponse) { $errorTitle = 'For some reason we can\'t retrieve tracking info right now.'; $resultArr = []; $packageProgress = []; if ($xmlResponse) { $xml = new \Magento\Framework\Simplexml\Config(); $xml->loadString($xmlResponse); $arr = $xml->getXpath("//TrackResponse/Response/ResponseStatusCode/text()"); $success = (int) $arr[0][0]; if ($success === 1) { $arr = $xml->getXpath("//TrackResponse/Shipment/Service/Description/text()"); $resultArr['service'] = (string) $arr[0]; $arr = $xml->getXpath("//TrackResponse/Shipment/PickupDate/text()"); $resultArr['shippeddate'] = (string) $arr[0]; $arr = $xml->getXpath("//TrackResponse/Shipment/Package/PackageWeight/Weight/text()"); $weight = (string) $arr[0]; $arr = $xml->getXpath("//TrackResponse/Shipment/Package/PackageWeight/UnitOfMeasurement/Code/text()"); $unit = (string) $arr[0]; $resultArr['weight'] = "{$weight} {$unit}"; $activityTags = $xml->getXpath("//TrackResponse/Shipment/Package/Activity"); if ($activityTags) { $index = 1; foreach ($activityTags as $activityTag) { $addArr = []; if (isset($activityTag->ActivityLocation->Address->City)) { $addArr[] = (string) $activityTag->ActivityLocation->Address->City; } if (isset($activityTag->ActivityLocation->Address->StateProvinceCode)) { $addArr[] = (string) $activityTag->ActivityLocation->Address->StateProvinceCode; } if (isset($activityTag->ActivityLocation->Address->CountryCode)) { $addArr[] = (string) $activityTag->ActivityLocation->Address->CountryCode; } $dateArr = []; $date = (string) $activityTag->Date; //YYYYMMDD $dateArr[] = substr($date, 0, 4); $dateArr[] = substr($date, 4, 2); $dateArr[] = substr($date, -2, 2); $timeArr = []; $time = (string) $activityTag->Time; //HHMMSS $timeArr[] = substr($time, 0, 2); $timeArr[] = substr($time, 2, 2); $timeArr[] = substr($time, -2, 2); if ($index === 1) { $resultArr['status'] = (string) $activityTag->Status->StatusType->Description; $resultArr['deliverydate'] = implode('-', $dateArr); //YYYY-MM-DD $resultArr['deliverytime'] = implode(':', $timeArr); //HH:MM:SS $resultArr['deliverylocation'] = (string) $activityTag->ActivityLocation->Description; $resultArr['signedby'] = (string) $activityTag->ActivityLocation->SignedForByName; if ($addArr) { $resultArr['deliveryto'] = implode(', ', $addArr); } } else { $tempArr = []; $tempArr['activity'] = (string) $activityTag->Status->StatusType->Description; $tempArr['deliverydate'] = implode('-', $dateArr); //YYYY-MM-DD $tempArr['deliverytime'] = implode(':', $timeArr); //HH:MM:SS if ($addArr) { $tempArr['deliverylocation'] = implode(', ', $addArr); } $packageProgress[] = $tempArr; } $index++; } $resultArr['progressdetail'] = $packageProgress; } } else { $arr = $xml->getXpath("//TrackResponse/Response/Error/ErrorDescription/text()"); $errorTitle = (string) $arr[0][0]; } } if (!$this->_result) { $this->_result = $this->_trackFactory->create(); } if ($resultArr) { $tracking = $this->_trackStatusFactory->create(); $tracking->setCarrier('ups'); $tracking->setCarrierTitle($this->getConfigData('title')); $tracking->setTracking($trackingValue); $tracking->addData($resultArr); $this->_result->append($tracking); } else { $error = $this->_trackErrorFactory->create(); $error->setCarrier('ups'); $error->setCarrierTitle($this->getConfigData('title')); $error->setTracking($trackingValue); $error->setErrorMessage($errorTitle); $this->_result->append($error); } return $this->_result; }
/** * @param \Magento\Shipping\Model\Rate\Result $result * @param string $methodId * @param \stdClass $method * @param string $msg * @return \Owebia\ShippingFree\Model\Carrier */ protected function appendError(\Magento\Shipping\Model\Rate\Result $result, $methodId, \stdClass $method, $msg) { if ($this->getConfigData('showmethod') != 0) { $error = $this->_rateErrorFactory->create(); $error->setCarrier($this->_code); $error->setCarrierTitle($this->getConfigData('title')); $methodTitle = isset($method->title) ? $method->title : "Method `{$methodId}`"; $error->setErrorMessage("{$methodTitle} - {$msg}"); $result->append($error); } return $this; }