/** * Adds the error message in the result * * @param Mage_Shipping_Model_Rate_Result $result * @param string $message * @return Storm_Correios_Model_Carrier_Shipping */ protected function _appendError(Mage_Shipping_Model_Rate_Result &$result, $message) { $this->_getHelper()->log($message); $error = Mage::getModel('shipping/rate_result_error'); $error->setCarrier($this->_code)->setCarrierTitle($this->getConfigData('title'))->setErrorMessage($message); $result->append($error); return $this; }
/** * Add a rate to the result * * @param Mage_Shipping_Model_Rate_Result_Abstract|Mage_Shipping_Model_Rate_Result $result */ public function append($result) { if ($result instanceof Mage_Shipping_Model_Rate_Result_Abstract) { $this->_rates[] = $result; } elseif ($result instanceof Mage_Shipping_Model_Rate_Result) { $rates = $result->getAllRates(); foreach ($rates as $rate) { $this->append($rate); } } return $this; }
/** * Some special errors must be sent to users. * If not applicable, the default error will be sent. * * @param array $errorList Error List * * @return boolean */ protected function _appendShippingErrors($errorList) { $output = false; $successCode = '0'; $hasValidQuote = array_key_exists($successCode, $errorList); if (!$hasValidQuote) { $displayErrorList = explode(',', $this->getConfigData('hard_errors')); if ($this->getConfigFlag('show_soft_errors')) { $softErrorList = explode(',', $this->getConfigData('soft_errors')); $displayErrorList = array_merge($displayErrorList, $softErrorList); } foreach ($errorList as $errorCode => $errorMsg) { $isDisplayError = in_array($errorCode, $displayErrorList); if ($isDisplayError) { $error = Mage::getModel('shipping/rate_result_error'); $error->setCarrier($this->_code); $error->setErrorMessage($errorMsg); $this->_result->append($error); $output = true; } } if (!$output) { $logMsg = implode(',', $errorList); Mage::log("{$this->_code}: Warning! There is no valid quotes, and no one error was throwed: {$logMsg}"); } } return $output; }
public function sortRatesByPrice() { if (Mage::getStoreConfigFlag('udropship/customer/allow_shipping_extra_charge')) { $this->sortRatesByPriority(); } else { parent::sortRatesByPrice(); } return $this; }
/** * Add a warning message at the top of the shipping method list. * * @param SimpleXMLElement $servico Post Method * * @return boolean */ protected function _appendShippingWarning(SimpleXMLElement $servico) { $id = (string) $servico->Erro; $ids = explode(',', $this->getConfigData('soft_errors')); if (in_array($id, $ids)) { $error = Mage::getModel('shipping/rate_result_error'); $error->setCarrier($this->_code); $error->setErrorMessage($servico->MsgErro); $this->_result->append($error); return true; } return false; }
/** * Get tracking response * * @return string */ public function getResponse() { $statuses = ''; if ($this->_result instanceof Mage_Shipping_Model_Tracking_Result) { if ($trackings = $this->_result->getAllTrackings()) { foreach ($trackings as $tracking) { if ($data = $tracking->getAllData()) { if (!empty($data['status'])) { $statuses .= Mage::helper('usa')->__($data['status']) . "\n<br/>"; } else { $statuses .= Mage::helper('usa')->__('Empty response') . "\n<br/>"; } } } } } if (empty($statuses)) { $statuses = Mage::helper('usa')->__('Empty response'); } return $statuses; }
/** * Get tracking response * * @return string */ public function getResponse() { $statuses = ''; if ($this->_result instanceof Mage_Shipping_Model_Tracking_Result) { if ($trackings = $this->_result->getAllTrackings()) { foreach ($trackings as $tracking) { if ($data = $tracking->getAllData()) { if (isset($data['status'])) { $statuses .= Mage::helper('Mage_Usa_Helper_Data')->__($data['status']); } else { $statuses .= Mage::helper('Mage_Usa_Helper_Data')->__($data['error_message']); } } } } } if (empty($statuses)) { $statuses = Mage::helper('Mage_Usa_Helper_Data')->__('Empty response'); } return $statuses; }
/** * Protected Get Tracking, opens the request to Correios * * @param string $code Code * * @return bool */ protected function _getTracking($code) { $error = Mage::getModel('shipping/tracking_result_error'); $error->setTracking($code); $error->setCarrier($this->_code); $error->setCarrierTitle($this->getConfigData('title')); $error->setErrorMessage($this->getConfigData('urlerror')); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "https://fastshipping.ciawn.com.br/v1/tracking/" . $code . "?token=" . $this->_token); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); $response = curl_exec($curl); curl_close($curl); $response = json_decode($response)->result; $progress = array(); foreach ($response as $key => $item) { $datetime = explode(' ', $item->data); $locale = new Zend_Locale('pt_BR'); $date = ''; $date = new Zend_Date($datetime[0], 'dd/MM/YYYY', $locale); $track = array('deliverydate' => $date->toString('YYYY-MM-dd'), 'deliverytime' => $datetime[1] . ':00', 'deliverylocation' => $item->local, 'status' => $item->acao, 'activity' => $item->detalhes); $progress[] = $track; } if (!empty($progress)) { $track = $progress[0]; $track['progressdetail'] = $progress; $tracking = Mage::getModel('shipping/tracking_result_status'); $tracking->setTracking($code); $tracking->setCarrier($this->_code); $tracking->setCarrierTitle($this->getConfigData('title')); $tracking->addData($track); $this->_result->append($tracking); return true; } else { $this->_result->append($error); return false; } }
protected function _getQuotes($extraCover, $config) { $destCountry = $config['country_code']; try { if ($destCountry == Fontis_Australia_Helper_Data::AUSTRALIA_COUNTRY_CODE) { $services = $this->_client->listDomesticParcelServices($config); } else { $services = $this->_client->listInternationalParcelServices($config); } } catch (Exception $e) { Mage::logException($e); return; } // TODO: Clean up this logic $allowedMethods = $this->getAllowedMethods(); $extraCoverParent = $this->getCode('extra_cover'); foreach ($services['services']['service'] as $service) { $serviceCode = $service['code']; // e.g. AUS_PARCEL_REGULAR if (in_array($serviceCode, $allowedMethods)) { $serviceName = $service['name']; // e.g. Parcel Post $servicePrice = $service['price']; // Just add the shipping method if the call to Australia Post // returns no options for that method if (!isset($service['options']['option']) && $this->_isAvailableShippingMethod($serviceName, $destCountry)) { $method = $this->createMethod($serviceCode, $serviceName, $servicePrice); $this->_result->append($method); // If a shipping method has a bunch of options, we will have to // create a specific method for each of the variants } else { $serviceOption = $service['options']['option']; // Unlike domestic shipping methods where the "default" // method is listed as simply another service option (this // allows us to simply loop through each one), we have to // extrapolate the default international shipping method // from what we know about the service itself if ($destCountry != Fontis_Australia_Helper_Data::AUSTRALIA_COUNTRY_CODE && $this->_isAvailableShippingMethod($serviceName, $destCountry)) { $method = $this->createMethod($serviceCode, $serviceName, $servicePrice); $this->_result->append($method); } // Checks to see if the API has returned either a single // service option or an array of them. If it is a single // option then turn it into an array. if (isset($serviceOption['name'])) { $serviceOption = array($serviceOption); } foreach ($serviceOption as $option) { $serviceOptionName = $option['name']; $serviceOptionCode = $option['code']; $config = array_merge($config, array('service_code' => $serviceCode, 'option_code' => $serviceOptionCode, 'extra_cover' => $extraCover)); try { if ($destCountry == Fontis_Australia_Helper_Data::AUSTRALIA_COUNTRY_CODE) { $postage = $this->_client->calculateDomesticParcelPostage($config); } else { $postage = $this->_client->calculateInternationalParcelPostage($config); } } catch (Exception $e) { continue; } $servicePrice = $postage['postage_result']['total_cost']; /** @var Fontis_Australia_Helper_Clickandsend $clickandsendHelper */ $clickandsendHelper = Mage::helper('australia/clickandsend'); // Create a shipping method with only the top-level options $_finalCode = $serviceCode . '_' . $serviceOptionCode; $_finalName = $serviceName . ' (' . $serviceOptionName . ')'; if ($this->_isAvailableShippingMethod($_finalName, $destCountry) && !(in_array($serviceOptionCode, $clickandsendHelper->getDisallowedServiceOptions()) && in_array($serviceCode, $clickandsendHelper->getDisallowedServiceCodes()) && $clickandsendHelper->isClickAndSendEnabled() && $clickandsendHelper->isFilterShippingMethods())) { $method = $this->createMethod($_finalCode, $_finalName, $servicePrice); $this->_result->append($method); } // Add the extra cover options (these are suboptions of // the top-level options) if (array_key_exists($serviceOptionCode, $extraCoverParent) && !($clickandsendHelper->isClickAndSendEnabled() && $clickandsendHelper->isFilterShippingMethods())) { try { if ($destCountry == Fontis_Australia_Helper_Data::AUSTRALIA_COUNTRY_CODE) { $config = array_merge($config, array('suboption_code' => ServiceOption::AUS_SERVICE_OPTION_EXTRA_COVER)); $postageWithExtraCover = $this->_client->calculateDomesticParcelPostage($config); } else { $postageWithExtraCover = $this->_client->calculateInternationalParcelPostage($config); } unset($config['suboption_code']); } catch (Exception $e) { continue; } if ($serviceOptionName == 'Signature on Delivery') { $serviceOptionName = $serviceOptionName . ' + Extra Cover'; } else { $serviceOptionName = 'Extra Cover'; } if ($serviceOptionCode == ServiceOption::AUS_SERVICE_OPTION_SIGNATURE_ON_DELIVERY) { $serviceOptionCode = 'FULL_PACKAGE'; } else { $serviceOptionCode = 'EXTRA_COVER'; } $servicePrice = $postageWithExtraCover['postage_result']['total_cost']; $_finalCode = $serviceCode . '_' . $serviceOptionCode; $_finalName = $serviceName . ' (' . $serviceOptionName . ')'; if ($this->_isAvailableShippingMethod($_finalName, $destCountry)) { $method = $this->createMethod($_finalCode, $_finalName, $servicePrice); $this->_result->append($method); } } } } } } }
/** * Protected Get Tracking, opens the request to Correios * * @param string $code * * @return bool */ protected function _getTracking($code) { $error = Mage::getModel('shipping/tracking_result_error'); $error->setTracking($code); $error->setCarrier($this->_code); $error->setCarrierTitle($this->getConfigData('title')); $error->setErrorMessage($this->getConfigData('urlerror')); $url = 'http://websro.correios.com.br/sro_bin/txect01$.QueryList'; $url .= '?P_LINGUA=001&P_TIPO=001&P_COD_UNI=' . $code; try { $client = new Zend_Http_Client(); $client->setUri($url); $content = $client->request(); $body = $content->getBody(); } catch (Exception $e) { $this->_result->append($error); return false; } if (!preg_match('#<table ([^>]+)>(.*?)</table>#is', $body, $matches)) { $this->_result->append($error); return false; } $table = $matches[2]; if (!preg_match_all('/<tr>(.*)<\\/tr>/i', $table, $columns, PREG_SET_ORDER)) { $this->_result->append($error); return false; } $progress = array(); for ($i = 0; $i < count($columns); $i++) { $column = $columns[$i][1]; $description = ''; $found = false; if (preg_match('/<td rowspan="?2"?/i', $column) && preg_match('/<td rowspan="?2"?>(.*)<\\/td><td>(.*)<\\/td><td><font color="[A-Z0-9]{6}">(.*)<\\/font><\\/td>/i', $column, $matches)) { if (preg_match('/<td colspan="?2"?>(.*)<\\/td>/i', $columns[$i + 1][1], $matchesDescription)) { $description = str_replace(' ', '', $matchesDescription[1]); } $found = true; } elseif (preg_match('/<td rowspan="?1"?>(.*)<\\/td><td>(.*)<\\/td><td><font color="[A-Z0-9]{6}">(.*)<\\/font><\\/td>/i', $column, $matches)) { $found = true; } if ($found) { $datetime = explode(' ', $matches[1]); $locale = new Zend_Locale('pt_BR'); $date = ''; $date = new Zend_Date($datetime[0], 'dd/MM/YYYY', $locale); $track = array('deliverydate' => $date->toString('YYYY-MM-dd'), 'deliverytime' => $datetime[1] . ':00', 'deliverylocation' => htmlentities($matches[2]), 'status' => htmlentities($matches[3]), 'activity' => htmlentities($matches[3])); if ($description !== '') { $track['activity'] = $matches[3] . ' - ' . htmlentities($description); } $progress[] = $track; } } if (!empty($progress)) { $track = $progress[0]; $track['progressdetail'] = $progress; $tracking = Mage::getModel('shipping/tracking_result_status'); $tracking->setTracking($code); $tracking->setCarrier('correios'); $tracking->setCarrierTitle($this->getConfigData('title')); $tracking->addData($track); $this->_result->append($tracking); return true; } else { $this->_result->append($error); return false; } }