Example #1
0
 /**
  * Prepare shipping rate result based on response
  *
  * @param mixed $xmlResponse
  * @return Result
  */
 protected function _parseXmlResponse($xmlResponse)
 {
     $costArr = array();
     $priceArr = array();
     if (strlen(trim($xmlResponse)) > 0) {
         $xml = new \Magento\Framework\Simplexml\Config();
         $xml->loadString($xmlResponse);
         $arr = $xml->getXpath("//RatingServiceSelectionResponse/Response/ResponseStatusCode/text()");
         $success = (int) $arr[0];
         if ($success === 1) {
             $arr = $xml->getXpath("//RatingServiceSelectionResponse/RatedShipment");
             $allowedMethods = explode(",", $this->getConfigData('allowed_methods'));
             // Negotiated rates
             $negotiatedArr = $xml->getXpath("//RatingServiceSelectionResponse/RatedShipment/NegotiatedRates");
             $negotiatedActive = $this->getConfigFlag('negotiated_active') && $this->getConfigData('shipper_number') && !empty($negotiatedArr);
             $allowedCurrencies = $this->_currencyFactory->create()->getConfigAllowCurrencies();
             foreach ($arr as $shipElement) {
                 $code = (string) $shipElement->Service->Code;
                 if (in_array($code, $allowedMethods)) {
                     if ($negotiatedActive) {
                         $cost = $shipElement->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue;
                     } else {
                         $cost = $shipElement->TotalCharges->MonetaryValue;
                     }
                     //convert price with Origin country currency code to base currency code
                     $successConversion = true;
                     $responseCurrencyCode = (string) $shipElement->TotalCharges->CurrencyCode;
                     if ($responseCurrencyCode) {
                         if (in_array($responseCurrencyCode, $allowedCurrencies)) {
                             $cost = (double) $cost * $this->_getBaseCurrencyRate($responseCurrencyCode);
                         } else {
                             $errorTitle = __('We can\'t convert a rate from "%1-%2".', $responseCurrencyCode, $this->_request->getPackageCurrency()->getCode());
                             $error = $this->_rateErrorFactory->create();
                             $error->setCarrier('ups');
                             $error->setCarrierTitle($this->getConfigData('title'));
                             $error->setErrorMessage($errorTitle);
                             $successConversion = false;
                         }
                     }
                     if ($successConversion) {
                         $costArr[$code] = $cost;
                         $priceArr[$code] = $this->getMethodPrice(floatval($cost), $code);
                     }
                 }
             }
         } else {
             $arr = $xml->getXpath("//RatingServiceSelectionResponse/Response/Error/ErrorDescription/text()");
             $errorTitle = (string) $arr[0][0];
             $error = $this->_rateErrorFactory->create();
             $error->setCarrier('ups');
             $error->setCarrierTitle($this->getConfigData('title'));
             $error->setErrorMessage($this->getConfigData('specificerrmsg'));
         }
     }
     $result = $this->_rateFactory->create();
     if (empty($priceArr)) {
         $error = $this->_rateErrorFactory->create();
         $error->setCarrier('ups');
         $error->setCarrierTitle($this->getConfigData('title'));
         if (!isset($errorTitle)) {
             $errorTitle = __('Cannot retrieve shipping rates');
         }
         $error->setErrorMessage($this->getConfigData('specificerrmsg'));
         $result->append($error);
     } else {
         foreach ($priceArr as $method => $price) {
             $rate = $this->_rateMethodFactory->create();
             $rate->setCarrier('ups');
             $rate->setCarrierTitle($this->getConfigData('title'));
             $rate->setMethod($method);
             $methodArr = $this->getShipmentByCode($method);
             $rate->setMethodTitle($methodArr);
             $rate->setCost($costArr[$method]);
             $rate->setPrice($price);
             $result->append($rate);
         }
     }
     return $result;
 }