/** * Parse xml response and return result * * @param string $response * @return Mage_Shipping_Model_Rate_Result|Varien_Object */ protected function _parseXmlResponse($response) { $r = $this->_rawRequest; $costArr = array(); $priceArr = array(); $errorTitle = 'Unable to retrieve quotes'; $tr = get_html_translation_table(HTML_ENTITIES); unset($tr['<'], $tr['>'], $tr['"']); $response = str_replace(array_keys($tr), array_values($tr), $response); if (strlen(trim($response)) > 0) { if (strpos(trim($response), '<?xml') === 0) { $xml = simplexml_load_string($response); if (is_object($xml)) { if (is_object($xml->Faults) && is_object($xml->Faults->Fault) && is_object($xml->Faults->Fault->Code) && is_object($xml->Faults->Fault->Description) && is_object($xml->Faults->Fault->Context)) { $code = (string) $xml->Faults->Fault->Code; $description = $xml->Faults->Fault->Description; $context = $xml->Faults->Fault->Context; $this->_errors[$code] = Mage::helper('usa')->__('Error #%s : %s (%s)', $code, $description, $context); } else { if ($r->getDestCountryId() == self::USA_COUNTRY_ID) { if ($xml->Shipment) { foreach ($xml->Shipment as $shipXml) { $this->_parseXmlObject($shipXml); } } else { $this->_errors[] = Mage::helper('usa')->__('Shipment is not available.'); } } else { $shipXml = $xml->IntlShipment; $this->_parseXmlObject($shipXml); } $shipXml = $r->getDestCountryId() == self::USA_COUNTRY_ID ? $xml->Shipment : $xml->IntlShipment; } } } else { $this->_errors[] = Mage::helper('usa')->__('The response is in wrong format.'); } } if ($this->_rawRequest->getAction() == 'GenerateLabel') { $result = new Varien_Object(); if (!empty($this->_errors)) { $result->setErrors(implode($this->_errors, '; ')); } else { if ($xml !== false) { if ($r->getDestCountryId() == self::USA_COUNTRY_ID) { $shippingLabelContent = base64_decode((string) $xml->Shipment->Label->Image); $trackingNumber = (string) $xml->Shipment->ShipmentDetail->AirbillNbr; } else { $shippingLabelContent = base64_decode((string) $xml->IntlShipment->Label->Image); $trackingNumber = (string) $xml->IntlShipment->ShipmentDetail->AirbillNbr; } } $result->setShippingLabelContent($shippingLabelContent); $result->setTrackingNumber($trackingNumber); } return $result; } else { $result = Mage::getModel('shipping/rate_result'); if ($this->_dhlRates) { foreach ($this->_dhlRates as $rate) { $method = $rate['service']; $data = $rate['data']; $rate = Mage::getModel('shipping/rate_result_method'); $rate->setCarrier('dhl'); $rate->setCarrierTitle($this->getConfigData('title')); $rate->setMethod($method); $rate->setMethodTitle($data['term']); $rate->setCost($data['price_total']); $rate->setPrice($data['price_total']); $result->append($rate); } } else { if (!empty($this->_errors)) { $error = Mage::getModel('shipping/rate_result_error'); $error->setCarrier('dhl'); $error->setCarrierTitle($this->getConfigData('title')); $error->setErrorMessage($this->getConfigData('specificerrmsg')); $result->append($error); } } return $result; } }
/** * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response * * @param Varien_Object $request * @return Varien_Object */ protected function _doShipmentRequest(Varien_Object $request) { $this->_prepareShipmentRequest($request); $result = new Varien_Object(); $client = $this->_createShipSoapClient(); $requestClient = $this->_formShipmentRequest($request); $response = $client->processShipment($requestClient); if ($response->HighestSeverity != 'FAILURE' && $response->HighestSeverity != 'ERROR') { $shippingLabelContent = $response->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image; $trackingNumber = $response->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds->TrackingNumber; $result->setShippingLabelContent($shippingLabelContent); $result->setTrackingNumber($trackingNumber); $debugData = array('request' => $client->__getLastRequest(), 'result' => $client->__getLastResponse()); $this->_debug($debugData); } else { $debugData = array('request' => $client->__getLastRequest(), 'result' => array('error' => '', 'code' => '', 'xml' => $client->__getLastResponse())); if (is_array($response->Notifications)) { foreach ($response->Notifications as $notification) { $debugData['result']['code'] .= $notification->Code . '; '; $debugData['result']['error'] .= $notification->Message . '; '; } } else { $debugData['result']['code'] = $response->Notifications->Code . ' '; $debugData['result']['error'] = $response->Notifications->Message . ' '; } $this->_debug($debugData); $result->setErrors($debugData['result']['error']); } $result->setGatewayResponse($client->__getLastResponse()); return $result; }
/** * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response * * @param Varien_Object $request * @return Varien_Object */ protected function _doShipmentRequest(Varien_Object $request) { $this->_prepareShipmentRequest($request); $result = new Varien_Object(); $service = $this->getCode('service_to_code', $request->getShippingMethod()); $recipientUSCountry = $this->_isUSCountry($request->getRecipientAddressCountryCode()); if ($recipientUSCountry && $service == 'Priority Express') { $requestXml = $this->_formUsExpressShipmentRequest($request); $api = 'ExpressMailLabel'; } else { if ($recipientUSCountry) { $requestXml = $this->_formUsSignatureConfirmationShipmentRequest($request, $service); if ($this->getConfigData('mode')) { $api = 'SignatureConfirmationV3'; } else { $api = 'SignatureConfirmationCertifyV3'; } } else { if ($service == 'First Class') { $requestXml = $this->_formIntlShipmentRequest($request); $api = 'FirstClassMailIntl'; } else { if ($service == 'Priority') { $requestXml = $this->_formIntlShipmentRequest($request); $api = 'PriorityMailIntl'; } else { $requestXml = $this->_formIntlShipmentRequest($request); $api = 'ExpressMailIntl'; } } } } $debugData = array('request' => $requestXml); $url = $this->getConfigData('gateway_secure_url'); if (!$url) { $url = $this->_defaultGatewayUrl; } $client = new Zend_Http_Client(); $client->setUri($url); $client->setConfig(array('maxredirects' => 0, 'timeout' => 30)); $client->setParameterGet('API', $api); $client->setParameterGet('XML', $requestXml); $response = $client->request()->getBody(); $response = simplexml_load_string($response); if ($response === false || $response->getName() == 'Error') { $debugData['result'] = array('error' => $response->Description, 'code' => $response->Number, 'xml' => $response->asXML()); $this->_debug($debugData); $result->setErrors($debugData['result']['error']); } else { if ($recipientUSCountry && $service == 'Priority Express') { $labelContent = base64_decode((string) $response->EMLabel); $trackingNumber = (string) $response->EMConfirmationNumber; } else { if ($recipientUSCountry) { $labelContent = base64_decode((string) $response->SignatureConfirmationLabel); $trackingNumber = (string) $response->SignatureConfirmationNumber; } else { $labelContent = base64_decode((string) $response->LabelImage); $trackingNumber = (string) $response->BarcodeNumber; } } $result->setShippingLabelContent($labelContent); $result->setTrackingNumber($trackingNumber); } $result->setGatewayResponse($response); $debugData['result'] = $response; $this->_debug($debugData); return $result; }
/** * Send and process shipment accept request * * @param SimpleXMLElement * @return Varien_Object */ protected function _sendShipmentAcceptRequest(SimpleXMLElement $shipmentConfirmResponse) { $xmlRequest = new SimpleXMLElement('<?xml version = "1.0" ?><ShipmentAcceptRequest/>'); $request = $xmlRequest->addChild('Request'); $request->addChild('RequestAction', 'ShipAccept'); $xmlRequest->addChild('ShipmentDigest', $shipmentConfirmResponse->ShipmentDigest); $debugData = array('request' => $xmlRequest->asXML()); try { $url = $this->_defaultUrls['ShipAccept']; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_xmlAccessRequest . $xmlRequest->asXML()); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (bool) $this->getConfigFlag('mode_xml')); $xmlResponse = curl_exec($ch); $debugData['result'] = $xmlResponse; $this->_setCachedQuotes($xmlRequest, $xmlResponse); } catch (Exception $e) { $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode()); $xmlResponse = ''; } try { $response = new SimpleXMLElement($xmlResponse); } catch (Exception $e) { $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode()); } $result = new Varien_Object(); if (isset($response->Error)) { $result->setErrors((string) $response->Error->ErrorDescription); } else { $shippingLabelContent = (string) $response->ShipmentResults->PackageResults->LabelImage->GraphicImage; $trackingNumber = (string) $response->ShipmentResults->PackageResults->TrackingNumber; $result->setShippingLabelContent(base64_decode($shippingLabelContent)); $result->setTrackingNumber($trackingNumber); } $this->_debug($debugData); return $result; }
/** * Parse response from DHL web service * * @param string $response * @return Mage_Shipping_Model_Rate_Result */ protected function _parseResponse($response) { $responseError = Mage::helper('usa')->__('The response is in wrong format.'); if (strlen(trim($response)) > 0) { if (strpos(trim($response), '<?xml') === 0) { $xml = simplexml_load_string($response); if (is_object($xml)) { if (in_array($xml->getName(), array('ErrorResponse', 'ShipmentValidateErrorResponse')) || isset($xml->GetQuoteResponse->Note->Condition)) { $code = null; $data = null; if (isset($xml->Response->Status->Condition)) { $nodeCondition = $xml->Response->Status->Condition; } else { $nodeCondition = $xml->GetQuoteResponse->Note->Condition; } if ($this->_isShippingLabelFlag) { foreach ($nodeCondition as $condition) { $code = isset($condition->ConditionCode) ? (string) $condition->ConditionCode : 0; $data = isset($condition->ConditionData) ? (string) $condition->ConditionData : ''; if (!empty($code) && !empty($data)) { break; } } Mage::throwException(Mage::helper('usa')->__('Error #%s : %s', trim($code), trim($data))); } $code = isset($nodeCondition->ConditionCode) ? (string) $nodeCondition->ConditionCode : 0; $data = isset($nodeCondition->ConditionData) ? (string) $nodeCondition->ConditionData : ''; $this->_errors[$code] = Mage::helper('usa')->__('Error #%s : %s', trim($code), trim($data)); } else { if (isset($xml->GetQuoteResponse->BkgDetails->QtdShp)) { foreach ($xml->GetQuoteResponse->BkgDetails->QtdShp as $quotedShipment) { $this->_addRate($quotedShipment); } } elseif (isset($xml->AirwayBillNumber)) { $result = new Varien_Object(); $result->setTrackingNumber((string) $xml->AirwayBillNumber); try { $labelContent = (string) $xml->LabelImage->OutputImage; $result->setShippingLabelContent(base64_decode($labelContent)); } catch (Exception $e) { Mage::throwException(Mage::helper('usa')->__($e->getMessage())); } return $result; } else { $this->_errors[] = $responseError; } } } } else { $this->_errors[] = $responseError; } } else { $this->_errors[] = $responseError; } /* @var $result Mage_Shipping_Model_Rate_Result */ $result = Mage::getModel('shipping/rate_result'); if ($this->_rates) { foreach ($this->_rates as $rate) { $method = $rate['service']; $data = $rate['data']; /* @var $rate Mage_Shipping_Model_Rate_Result_Method */ $rate = Mage::getModel('shipping/rate_result_method'); $rate->setCarrier(self::CODE); $rate->setCarrierTitle($this->getConfigData('title')); $rate->setMethod($method); $rate->setMethodTitle($data['term']); $rate->setCost($data['price_total']); $rate->setPrice($data['price_total']); $result->append($rate); } } else { if (!empty($this->_errors)) { if ($this->_isShippingLabelFlag) { Mage::throwException($responseError); } return $this->_showError(); } } return $result; }
/** * Parse response from DHL web service * * @param string $response * @return Mage_Shipping_Model_Rate_Result */ protected function _parseResponse($response) { $htmlTranslationTable = get_html_translation_table(HTML_ENTITIES); unset($htmlTranslationTable['<'], $htmlTranslationTable['>'], $htmlTranslationTable['"']); $response = str_replace(array_keys($htmlTranslationTable), array_values($htmlTranslationTable), $response); $responseError = Mage::helper('usa')->__('The response is in wrong format.'); if (strlen(trim($response)) > 0) { if (strpos(trim($response), '<?xml') === 0) { $xml = simplexml_load_string($response); if (is_object($xml)) { if (in_array($xml->getName(), array('ErrorResponse', 'ShipmentValidateErrorResponse')) || isset($xml->GetQuoteResponse->Note->Condition)) { $code = null; $data = null; if (isset($xml->Response->Status->Condition)) { $nodeCondition = $xml->Response->Status->Condition; } else { $nodeCondition = $xml->GetQuoteResponse->Note->Condition; } if ($this->_isShippingLabelFlag) { foreach ($nodeCondition as $condition) { $code = isset($condition->ConditionCode) ? (string) $condition->ConditionCode : 0; $data = isset($condition->ConditionData) ? (string) $condition->ConditionData : ''; if (!empty($code) && !empty($data)) { break; } } Mage::throwException(Mage::helper('usa')->__('Error #%s : %s', trim($code), trim($data))); } $code = isset($nodeCondition->ConditionCode) ? (string) $nodeCondition->ConditionCode : 0; $data = isset($nodeCondition->ConditionData) ? (string) $nodeCondition->ConditionData : ''; $this->_errors[$code] = Mage::helper('usa')->__('Error #%s : %s', trim($code), trim($data)); } else { if (isset($xml->GetQuoteResponse->BkgDetails->QtdShp)) { foreach ($xml->GetQuoteResponse->BkgDetails->QtdShp as $quotedShipment) { $this->_addRate($quotedShipment); } } elseif (isset($xml->AirwayBillNumber)) { $result = new Varien_Object(); $result->setTrackingNumber((string) $xml->AirwayBillNumber); try { /* @var $pdf Mage_Usa_Model_Shipping_Carrier_Dhl_Label_Pdf */ $pdf = Mage::getModel('usa/shipping_carrier_dhl_label_pdf', array('info' => $xml, 'request' => $this->_request)); $result->setShippingLabelContent($pdf->render()); } catch (Exception $e) { Mage::throwException(Mage::helper('usa')->__($e->getMessage())); } return $result; } else { $this->_errors[] = $responseError; } } } } else { $this->_errors[] = $responseError; } } else { $this->_errors[] = $responseError; } /* @var $result Mage_Shipping_Model_Rate_Result */ $result = Mage::getModel('shipping/rate_result'); if ($this->_rates) { foreach ($this->_rates as $rate) { $method = $rate['service']; $data = $rate['data']; /* @var $rate Mage_Shipping_Model_Rate_Result_Method */ $rate = Mage::getModel('shipping/rate_result_method'); $rate->setCarrier(self::CODE); $rate->setCarrierTitle($this->getConfigData('title')); $rate->setMethod($method); $rate->setMethodTitle($data['term']); $rate->setCost($data['price_total']); $rate->setPrice($data['price_total']); $result->append($rate); } } else { if (!empty($this->_errors)) { if ($this->_isShippingLabelFlag) { Mage::throwException($responseError); } return $this->_showError(); } } return $result; }
/** * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response * * @param Varien_Object $request * @return Varien_Object */ protected function _doShipmentRequest(Varien_Object $request) { $this->_prepareShipmentRequest($request); $result = new Varien_Object(); try { $client = $this->_createLabelSoapClient(); $requestShip = $this->_formShipmentRequest($request); $response = $client->GetPostageLabel($requestShip); $debugData['request_sent'] = $requestShip; //response includes string of PDF label - too big to log $debugString = "Status: " . $response->LabelRequestResponse->Status; if (isset($response->LabelRequestResponse->TrackingNumber)) { $debugString .= ' Tracking ID: ' . $response->LabelRequestResponse->TrackingNumber . ' Final Postage: ' . $response->LabelRequestResponse->FinalPostage; } $debugData['response'] = $debugString; } catch (Exception $e) { $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode()); Mage::logException($e); $result->setErrors((string) 'an issue: ' . $e->getMessage()); } if (is_object($response)) { if ($response->LabelRequestResponse->Status != 0) { $result->setErrors((string) $response->LabelRequestResponse->ErrorMessage); $debugData['response'] = $response->LabelRequestResponse->ErrorMessage; } else { $result->setShippingLabelContent(base64_decode($response->LabelRequestResponse->Base64LabelImage)); $result->setTrackingNumber($response->LabelRequestResponse->TrackingNumber); $this->_recordCostToOrder($request->getOrderShipment()->getOrder(), $response->LabelRequestResponse->FinalPostage); } } $result->setGatewayResponse($client->__getLastResponse()); if ($this->_debug) { Mage::helper('wsalogger/log')->postInfo('endicia', 'Request XML', $client->__getLastRequest()); Mage::helper('wsalogger/log')->postInfo('endicia', 'Response', $debugData); } return $result; }