'Unknown error', self::ERROR_RESPONSE_WRONG_FORMAT => 'Response is in the wrong format', ]; protected $_shipToCountryCode; public function prepareServiceResponse($response) { if (strlen(trim($response)) > 0) { if (strpos(trim($response), 'Package)) { $rateTotal = 0; $errors = []; if ($this->_shipToCountryCode == App_Service_Shipping_Usps::COUNTRY_CODE_US) { foreach ($xml->Package as $package) { if (isset($package->Error) && is_object($package->Error)) { $errors[] = (string) $package->Error->Description; } else { $rateTotal = Zend_Locale_Math::Add($rateTotal, $package->Postage->Rate, 4); } } if (!empty($errors)) { return $this->setIsSuccess(false)->setData([ 'errorCode' => self::ERROR_SERVICE, 'errorText' => implode('
', $errors), ]); } else { return $this->setIsSuccess()->setData([ 'serviceDetails' => $this->getServiceDetails(), 'rate' => [ 'total' => Zend_Locale_Math::round($rateTotal, 2), ], ]); } } else { /** International Rates */ if (isset($xml->Package->Error)) { return $this->setIsSuccess(false)->setData([ 'errorCode' => (string) $xml->Package->Error->Number, 'errorText' => (string) $xml->Package->Error->Description, ]); } elseif (isset($xml->Package->Service)) { return $this->setIsSuccess()->setData([ 'serviceDetails' => $this->getServiceDetails(), 'rate' => [ 'total' => (string) $xml->Package->Service->Postage, ], ]); } } } } } else { $xmlParser = xml_parser_create(); /** @var array $values */ xml_parse_into_struct($xmlParser, $response, $values, $index); xml_parser_free($xmlParser); $params = []; $level = []; foreach ($values as $xmlElement) { if ($xmlElement['type'] == 'open') { if (array_key_exists('attributes', $xmlElement)) { list($level[$xmlElement['level']]) = array_values($xmlElement['attributes']); } else { $level[$xmlElement['level']] = $xmlElement['tag']; } } if ($xmlElement['type'] == 'complete') { $startLevel = 1; $phpStmt = '$params'; while ($startLevel < $xmlElement['level']) { $phpStmt .= '[$level[' . $startLevel . ']]'; $startLevel++; } $phpStmt .= '[$xmlElement[\'tag\']] = $xmlElement[\'value\'];'; eval($phpStmt); } } if (isset($params['ERROR'])) { return $this->setIsSuccess(false)->setData([ 'errorCode' => $params['ERROR']['NUMBER'], 'errorText' => $params['ERROR']['DESCRIPTION'], ]); } } } return $this->setIsSuccess(false)->setData([ 'errorCode' => self::ERROR_RESPONSE_WRONG_FORMAT, 'errorText' => $this->_errorMap[self::ERROR_UNKNOWN], ]); } public function setShipToCountryCode($shipToCountryCode) { $this->_shipToCountryCode = $shipToCountryCode; } }