Index: app/code/core/Mage/Usa/etc/config.xml =================================================================== --- app/code/core/Mage/Usa/etc/config.xml (revision 90433) +++ app/code/core/Mage/Usa/etc/config.xml (working copy) @@ -172,14 +172,14 @@ 0 0 - Bound Printed Matter,Express Mail,Express Mail Flat Rate Envelope,Express Mail Flat Rate Envelope Hold For Pickup,Express Mail Flat-Rate Envelope Sunday/Holiday Guarantee,Express Mail Hold For Pickup,Express Mail International,Express Mail International Flat Rate Envelope,Express Mail PO to PO,Express Mail Sunday/Holiday Guarantee,First-Class Mail International Large Envelope,First-Class Mail International Letters,First-Class Mail International Package,First-Class,First-Class Mail,First-Class Mail Flat,First-Class Mail International,First-Class Mail Letter,First-Class Mail Parcel,Global Express Guaranteed (GXG),Global Express Guaranteed Non-Document Non-Rectangular,Global Express Guaranteed Non-Document Rectangular,Library Mail,Media Mail,Parcel Post,Priority Mail,Priority Mail Small Flat Rate Box,Priority Mail Medium Flat Rate Box,Priority Mail Large Flat Rate Box,Priority Mail Flat Rate Box,Priority Mail Flat Rate Envelope,Priority Mail International,Priority Mail International Flat Rate Box,Priority Mail International Flat Rate Envelope,Priority Mail International Small Flat Rate Box,Priority Mail International Medium Flat Rate Box,Priority Mail International Large Flat Rate Box,USPS GXG Envelopes + Bound Printed Matter,Express Mail,Express Mail Flat Rate Envelope,Express Mail Flat Rate Envelope Hold For Pickup,Express Mail Flat-Rate Envelope Sunday/Holiday Guarantee,Express Mail Hold For Pickup,Express Mail International,Express Mail International Flat Rate Envelope,Express Mail PO to PO,Express Mail Sunday/Holiday Guarantee,First-Class Mail International Large Envelope,First-Class Mail International Letters,First-Class Mail International Package,First-Class,First-Class Mail,First-Class Mail Flat,First-Class Mail Large Envelope,First-Class Mail International,First-Class Mail Letter,First-Class Mail Parcel,First-Class Mail Package,Global Express Guaranteed (GXG),Global Express Guaranteed Non-Document Non-Rectangular,Global Express Guaranteed Non-Document Rectangular,Library Mail,Media Mail,Parcel Post,Priority Mail,Priority Mail Small Flat Rate Box,Priority Mail Medium Flat Rate Box,Priority Mail Large Flat Rate Box,Priority Mail Flat Rate Box,Priority Mail Flat Rate Envelope,Priority Mail International,Priority Mail International Flat Rate Box,Priority Mail International Flat Rate Envelope,Priority Mail International Small Flat Rate Box,Priority Mail International Medium Flat Rate Box,Priority Mail International Large Flat Rate Box,USPS GXG Envelopes VARIABLE http://production.shippingapis.com/ShippingAPI.dll true - Bound Printed Matter,Express Mail,Express Mail Flat Rate Envelope,Express Mail Flat Rate Envelope Hold For Pickup,Express Mail Flat-Rate Envelope Sunday/Holiday Guarantee,Express Mail Hold For Pickup,Express Mail International,Express Mail International Flat Rate Envelope,Express Mail PO to PO,Express Mail Sunday/Holiday Guarantee,First-Class Mail International Large Envelope,First-Class Mail International Letters,First-Class Mail International Package,First-Class,First-Class Mail,First-Class Mail Flat,First-Class Mail International,First-Class Mail Letter,First-Class Mail Parcel,Global Express Guaranteed (GXG),Global Express Guaranteed Non-Document Non-Rectangular,Global Express Guaranteed Non-Document Rectangular,Library Mail,Media Mail,Parcel Post,Priority Mail,Priority Mail Small Flat Rate Box,Priority Mail Medium Flat Rate Box,Priority Mail Large Flat Rate Box,Priority Mail Flat Rate Box,Priority Mail Flat Rate Envelope,Priority Mail International,Priority Mail International Flat Rate Box,Priority Mail International Flat Rate Envelope,Priority Mail International Small Flat Rate Box,Priority Mail International Medium Flat Rate Box,Priority Mail International Large Flat Rate Box,USPS GXG Envelopes + Bound Printed Matter,Express Mail,Express Mail Flat Rate Envelope,Express Mail Flat Rate Envelope Hold For Pickup,Express Mail Flat-Rate Envelope Sunday/Holiday Guarantee,Express Mail Hold For Pickup,Express Mail International,Express Mail International Flat Rate Envelope,Express Mail PO to PO,Express Mail Sunday/Holiday Guarantee,First-Class Mail International Large Envelope,First-Class Mail International Letters,First-Class Mail International Package,First-Class,First-Class Mail,First-Class Mail Flat,First-Class Mail Large Envelope,First-Class Mail International,First-Class Mail Letter,First-Class Mail Parcel,First-Class Mail Package,Global Express Guaranteed (GXG),Global Express Guaranteed Non-Document Non-Rectangular,Global Express Guaranteed Non-Document Rectangular,Library Mail,Media Mail,Parcel Post,Priority Mail,Priority Mail Small Flat Rate Box,Priority Mail Medium Flat Rate Box,Priority Mail Large Flat Rate Box,Priority Mail Flat Rate Box,Priority Mail Flat Rate Envelope,Priority Mail International,Priority Mail International Flat Rate Box,Priority Mail International Flat Rate Envelope,Priority Mail International Small Flat Rate Box,Priority Mail International Medium Flat Rate Box,Priority Mail International Large Flat Rate Box,USPS GXG Envelopes usa/shipping_carrier_usps REGULAR United States Postal Service Index: app/code/core/Mage/Usa/Model/Shipping/Carrier/Usps.php =================================================================== --- app/code/core/Mage/Usa/Model/Shipping/Carrier/Usps.php (revision 90433) +++ app/code/core/Mage/Usa/Model/Shipping/Carrier/Usps.php (working copy) @@ -28,7 +28,7 @@ /** * USPS shipping rates estimation * - * @link http://www.usps.com/webtools/htm/Development-Guide.htm + * @link http://www.usps.com/webtools/htm/Development-Guide-v3-0b.htm * @category Mage * @package Mage_Usa * @author Magento Core Team @@ -159,7 +159,7 @@ $weight = $this->getTotalNumOfBoxes($request->getPackageWeight()); $r->setWeightPounds(floor($weight)); - $r->setWeightOunces(round(($weight-floor($weight))*16, 1)); + $r->setWeightOunces(round(($weight-floor($weight)) * 16, 1)); if ($request->getFreeMethodWeight()!=$request->getPackageWeight()) { $r->setFreeMethodWeight($request->getFreeMethodWeight()); } @@ -188,10 +188,16 @@ $weight = $this->getTotalNumOfBoxes($r->getFreeMethodWeight()); $r->setWeightPounds(floor($weight)); - $r->setWeightOunces(round(($weight-floor($weight))*16, 1)); + $r->setWeightOunces(round(($weight-floor($weight)) * 16, 1)); $r->setService($freeMethod); } + /** + * Build RateV3 request, send it to USPS gateway and retrieve quotes in XML format + * + * @link http://www.usps.com/webtools/htm/Rate-Calculators-v2-3.htm + * @return Mage_Shipping_Model_Rate_Result + */ protected function _getXmlQuotes() { $r = $this->_rawRequest; @@ -199,7 +205,6 @@ $xml = new SimpleXMLElement(''); $xml->addAttribute('USERID', $r->getUserId()); - $package = $xml->addChild('Package'); $package->addAttribute('ID', 0); $service = $this->getCode('service_to_code', $r->getService()); @@ -273,86 +278,71 @@ } $this->_debug($debugData); } - return $this->_parseXmlResponse($responseBody);; + return $this->_parseXmlResponse($responseBody); } + /** + * Parse calculated rates + * + * @link http://www.usps.com/webtools/htm/Rate-Calculators-v2-3.htm + * @param string $response + * @return Mage_Shipping_Model_Rate_Result + */ protected function _parseXmlResponse($response) { $costArr = array(); $priceArr = array(); - $errorTitle = 'Unable to retrieve quotes'; - if (strlen(trim($response))>0) { - if (strpos(trim($response), ' 0) { + if (strpos(trim($response), '#', $response)) { $response = str_replace('', '', $response); } $xml = simplexml_load_string($response); - if (is_object($xml)) { - if (is_object($xml->Number) && is_object($xml->Description) && (string)$xml->Description!='') { - $errorTitle = (string)$xml->Description; - } elseif (is_object($xml->Package) && is_object($xml->Package->Error) && is_object($xml->Package->Error->Description) && (string)$xml->Package->Error->Description!='') { - $errorTitle = (string)$xml->Package->Error->Description; - } else { - $errorTitle = 'Unknown error'; - } - $r = $this->_rawRequest; - $allowedMethods = explode(",", $this->getConfigData('allowed_methods')); - $allMethods = $this->getCode('method'); - $newMethod = false; - if ($this->_isUSCountry($r->getDestCountryId())) { - if (is_object($xml->Package) && is_object($xml->Package->Postage)) { - foreach ($xml->Package->Postage as $postage) { -// if (in_array($this->getCode('service_to_code', (string)$postage->MailService), $allowedMethods) && $this->getCode('service', $this->getCode('service_to_code', (string)$postage->MailService))) { - if (in_array((string)$postage->MailService, $allowedMethods)) { - $costArr[(string)$postage->MailService] = (string)$postage->Rate; -// $priceArr[(string)$postage->MailService] = $this->getMethodPrice((string)$postage->Rate, $this->getCode('service_to_code', (string)$postage->MailService)); - $priceArr[(string)$postage->MailService] = $this->getMethodPrice((string)$postage->Rate, (string)$postage->MailService); - } elseif (!in_array((string)$postage->MailService, $allMethods)) { - $allMethods[] = (string)$postage->MailService; - $newMethod = true; - } + if (is_object($xml)) { + $r = $this->_rawRequest; + $allowedMethods = explode(",", $this->getConfigData('allowed_methods')); + + /* + * US Domestic Rates + */ + if ($this->_isUSCountry($r->getDestCountryId())) { + if (is_object($xml->Package) && is_object($xml->Package->Postage)) { + foreach ($xml->Package->Postage as $postage) { + $serviceName = $this->_filterServiceName((string)$postage->MailService); + $postage->MailService = $serviceName; + if (in_array($serviceName, $allowedMethods)) { + $costArr[$serviceName] = (string)$postage->Rate; + $priceArr[$serviceName] = $this->getMethodPrice((string)$postage->Rate, $serviceName); } - asort($priceArr); } - } else { - if (is_object($xml->Package) && is_object($xml->Package->Service)) { - foreach ($xml->Package->Service as $service) { -// if (in_array($this->getCode('service_to_code', (string)$service->SvcDescription), $allowedMethods) && $this->getCode('service', $this->getCode('service_to_code', (string)$service->SvcDescription))) { - if (in_array((string)$service->SvcDescription, $allowedMethods)) { - $costArr[(string)$service->SvcDescription] = (string)$service->Postage; -// $priceArr[(string)$service->SvcDescription] = $this->getMethodPrice((string)$service->Postage, $this->getCode('service_to_code', (string)$service->SvcDescription)); - $priceArr[(string)$service->SvcDescription] = $this->getMethodPrice((string)$service->Postage, (string)$service->SvcDescription); - } elseif (!in_array((string)$service->SvcDescription, $allMethods)) { - $allMethods[] = (string)$service->SvcDescription; - $newMethod = true; - } + asort($priceArr); + } + } else { + /* + * International Rates + */ + if (is_object($xml->Package) && is_object($xml->Package->Service)) { + foreach ($xml->Package->Service as $service) { + $serviceName = $this->_filterServiceName((string)$service->SvcDescription); + $service->SvcDescription = $serviceName; + if (in_array($serviceName, $allowedMethods)) { + $costArr[$serviceName] = (string)$service->Postage; + $priceArr[$serviceName] = $this->getMethodPrice((string)$service->Postage, $serviceName); } - asort($priceArr); } + asort($priceArr); } - /* - * following if statement is obsolete - * we don't have adminhtml/config resoure model - */ - if (false && $newMethod) { - sort($allMethods); - $insert['usps']['fields']['methods']['value'] = $allMethods; - Mage::getResourceModel('adminhtml/config')->saveSectionPost('carriers','','',$insert); - } } - } else { - $errorTitle = 'Response is in the wrong format'; + } } } $result = Mage::getModel('shipping/rate_result'); - $defaults = $this->getDefaults(); if (empty($priceArr)) { $error = Mage::getModel('shipping/rate_result_error'); $error->setCarrier('usps'); $error->setCarrierTitle($this->getConfigData('title')); - //$error->setErrorMessage($errorTitle); $error->setErrorMessage($this->getConfigData('specificerrmsg')); $result->append($error); } else { @@ -367,6 +357,7 @@ $result->append($rate); } } + return $result; } @@ -411,13 +402,15 @@ 'service_to_code'=>array( 'First-Class' => 'FIRST CLASS', 'First-Class Mail International Large Envelope' => 'FIRST CLASS', - 'First-Class Mail International Letters' => 'FIRST CLASS', + 'First-Class Mail International Letter' => 'FIRST CLASS', 'First-Class Mail International Package' => 'FIRST CLASS', 'First-Class Mail' => 'FIRST CLASS', 'First-Class Mail Flat' => 'FIRST CLASS', + 'First-Class Mail Large Envelope' => 'FIRST CLASS', 'First-Class Mail International' => 'FIRST CLASS', 'First-Class Mail Letter' => 'FIRST CLASS', 'First-Class Mail Parcel' => 'FIRST CLASS', + 'First-Class Mail Package' => 'FIRST CLASS', 'Parcel Post' => 'PARCEL', 'Bound Printed Matter' => 'BPM', 'Media Mail' => 'MEDIA', @@ -480,14 +473,12 @@ } if (!isset($codes[$type])) { -// throw Mage::exception('Mage_Shipping', Mage::helper('usa')->__('Invalid USPS XML code type: %s', $type)); return false; } elseif (''===$code) { return $codes[$type]; } if (!isset($codes[$type][$code])) { -// throw Mage::exception('Mage_Shipping', Mage::helper('usa')->__('Invalid USPS XML code for type %s: %s', $type, $code)); return false; } else { return $codes[$type][$code]; @@ -522,7 +513,7 @@ { $r = $this->_rawTrackRequest; - foreach ($trackings as $tracking){ + foreach ($trackings as $tracking) { $xml = new SimpleXMLElement(''); $xml->addAttribute('USERID', $r->getUserId()); @@ -560,9 +551,9 @@ protected function _parseXmlTrackingResponse($trackingvalue, $response) { - $errorTitle = 'Unable to retrieve tracking'; + $errorTitle = Mage::helper('usa')->__('Unable to retrieve tracking'); $resultArr=array(); - if (strlen(trim($response))>0) { + if (strlen(trim($response)) > 0) { if (strpos(trim($response), 'TrackInfo) && isset($xml->TrackInfo->Error) && isset($xml->TrackInfo->Error->Description) && (string)$xml->TrackInfo->Error->Description!='') { $errorTitle = (string)$xml->TrackInfo->Error->Description; } else { - $errorTitle = 'Unknown error'; + $errorTitle = Mage::helper('usa')->__('Unknown error'); } if(isset($xml->TrackInfo) && isset($xml->TrackInfo->TrackSummary)){ @@ -582,7 +573,7 @@ } } - if(!$this->_result){ + if (!$this->_result) { $this->_result = Mage::getModel('shipping/tracking_result'); } $defaults = $this->getDefaults(); @@ -607,10 +598,10 @@ public function getResponse() { $statuses = ''; - if ($this->_result instanceof Mage_Shipping_Model_Tracking_Result){ + if ($this->_result instanceof Mage_Shipping_Model_Tracking_Result) { if ($trackings = $this->_result->getAllTrackings()) { - foreach ($trackings as $tracking){ - if($data = $tracking->getAllData()){ + foreach ($trackings as $tracking) { + if($data = $tracking->getAllData()) { if (!empty($data['track_summary'])) { $statuses .= Mage::helper('usa')->__($data['track_summary']); } else { @@ -902,4 +893,18 @@ return false; } + + /** + * Clean service name from unsupported strings and characters + * + * @param string $name + * @return string + */ + protected function _filterServiceName($name) + { + $name = (string)preg_replace(array('~<[^/!][^>]+>.*]+>~sU', '~\