Ejemplo n.º 1
0
 /**
  * @param $shipperResponse
  * @return Mage_Shipping_Model_Rate_Result
  */
 protected function parseShipperResponse($shipperResponse)
 {
     $debugRequest = $this->shipperRequest;
     $debugData = ['request' => json_encode($debugRequest, JSON_PRETTY_PRINT), 'response' => $shipperResponse];
     if (!is_object($shipperResponse)) {
         $this->shipperLogger->postInfo('Shipperhq_Shipper', 'Shipper HQ did not return a response', $debugData);
         return $this->returnGeneralError('Shipper HQ did not return a response - could not contact ShipperHQ. Please review your settings');
     }
     $transactionId = $this->shipperRateHelper->extractTransactionId($shipperResponse);
     $this->registry->unregister('shipperhq_transaction');
     $this->registry->register('shipperhq_transaction', $transactionId);
     //first check and save globals for display purposes
     $globals = array();
     if (is_object($shipperResponse) && isset($shipperResponse->globalSettings)) {
         $globals = $this->shipperRateHelper->extractGlobalSettings($shipperResponse);
         $globals['transaction'] = $transactionId;
         $this->shipperDataHelper->setGlobalSettings($globals);
     }
     $result = $this->rateFactory->create();
     // If no rates are found return error message
     if (!empty($shipperResponse->errors)) {
         $this->shipperLogger->postInfo('Shipperhq_Shipper', 'Shipper HQ returned an error', $debugData);
         if (isset($shipperResponse->errors)) {
             foreach ($shipperResponse->errors as $error) {
                 $this->appendError($result, $error, $this->_code, $this->getConfigData('title'));
             }
         }
         return $result;
     } elseif (!isset($shipperResponse->carrierGroups)) {
         // DO NOTHING
     }
     if (isset($shipperResponse->carrierGroups)) {
         $carrierRates = $this->processRatesResponse($shipperResponse, $transactionId, $globals);
     } else {
         $carrierRates = [];
     }
     $this->persistAddressValidation($shipperResponse);
     if (count($carrierRates) == 0) {
         $this->shipperLogger->postInfo('Shipperhq_Shipper', 'Shipper HQ did not return any carrier rates', $debugData);
         return $result;
     }
     foreach ($carrierRates as $carrierRate) {
         if (isset($carrierRate['error'])) {
             $carriergroupId = null;
             $carrierGroupDetail = null;
             if (array_key_exists('carriergroup_detail', $carrierRate) && !is_null($carrierRate['carriergroup_detail'])) {
                 if (array_key_exists('carrierGroupId', $carrierRate['carriergroup_detail'])) {
                     $carriergroupId = $carrierRate['carriergroup_detail']['carrierGroupId'];
                 }
                 $carrierGroupDetail = $carrierRate['carriergroup_detail'];
             }
             $this->appendError($result, $carrierRate['error'], $carrierRate['code'], $carrierRate['title'], $carriergroupId, $carrierGroupDetail);
             continue;
         }
         if (!array_key_exists('rates', $carrierRate)) {
             $this->shipperLogger->postInfo('Shipperhq_Shipper', 'Shipper HQ did not return any rates for ' . $carrierRate['code'] . ' ' . $carrierRate['title'], $debugData);
         } else {
             $baseRate = 1;
             $baseCurrencyCode = $this->shipperDataHelper->getBaseCurrencyCode();
             foreach ($carrierRate['rates'] as $rateDetails) {
                 if (isset($rateDetails['currency'])) {
                     if ($rateDetails['currency'] != $baseCurrencyCode || $baseRate != 1) {
                         $baseRate = $this->shipperDataHelper->getBaseCurrencyRate($rateDetails['currency']);
                         if (!$baseRate) {
                             $error = __('Can\'t convert rate from "%1".', $rateDetails['currency']);
                             $this->appendError($result, $error, $carrierRate['code'], $carrierRate['title'], $rateDetails['carriergroup_detail']['carrierGroupId'], $rateDetails['carriergroup_detail']);
                             $this->shipperLogger->postCritical('Shipperhq_Shipper', 'Currency Rate Missing', 'Currency code in shipping rate is ' . $rateDetails['currency'] . ' but there is no currency conversion rate configured so we cannot display this shipping rate');
                             continue;
                         }
                     }
                 }
                 $rate = $this->rateMethodFactory->create();
                 $rate->setCarrier($carrierRate['code']);
                 $lengthCarrierCode = strlen($carrierRate['code']);
                 $rate->setCarrierTitle(__($carrierRate['title']));
                 $methodCombineCode = preg_replace('/&|;| /', "", $rateDetails['methodcode']);
                 //SHQ16-1520 - enforce limit on length of shipping carrier code and method code of less than 35 - M2 hard limit of 40
                 $lengthMethodCode = strlen($methodCombineCode);
                 if ($lengthCarrierCode + $lengthMethodCode > 40) {
                     $total = $lengthCarrierCode + $lengthMethodCode;
                     $trim = $total - 35;
                     $methodCombineCode = substr($methodCombineCode, $trim, $lengthMethodCode);
                 }
                 $rate->setMethod($methodCombineCode);
                 $rate->setMethodTitle(__($rateDetails['method_title']));
                 $rate->setTooltip($rateDetails['tooltip']);
                 if (array_key_exists('method_description', $rateDetails)) {
                     $rate->setMethodDescription(__($rateDetails['method_description']));
                 }
                 $rate->setCost($rateDetails['cost'] * $baseRate);
                 $rate->setPrice($rateDetails['price'] * $baseRate);
                 if (array_key_exists('carrier_type', $rateDetails)) {
                     $rate->setCarrierType($rateDetails['carrier_type']);
                 }
                 if (array_key_exists('carrier_id', $rateDetails)) {
                     $rate->setCarrierId($rateDetails['carrier_id']);
                 }
                 if (array_key_exists('dispatch_date', $rateDetails)) {
                     $rate->setDispatchDate($rateDetails['dispatch_date']);
                 }
                 if (array_key_exists('delivery_date', $rateDetails)) {
                     $rate->setDeliveryDate($rateDetails['delivery_date']);
                 }
                 if (array_key_exists('carriergroup_detail', $rateDetails) && !is_null($rateDetails['carriergroup_detail'])) {
                     $carrierGroupDetail = $baseRate != 1 ? $this->updateWithCurrrencyConversion($rateDetails['carriergroup_detail'], $baseRate) : $rateDetails['carriergroup_detail'];
                     $rate->setCarriergroupShippingDetails($this->shipperDataHelper->encode($carrierGroupDetail));
                     if (array_key_exists('carrierGroupId', $carrierGroupDetail)) {
                         $rate->setCarriergroupId($carrierGroupDetail['carrierGroupId']);
                     }
                     if (array_key_exists('checkoutDescription', $carrierGroupDetail)) {
                         $rate->setCarriergroup($carrierGroupDetail['checkoutDescription']);
                     }
                 }
                 $result->append($rate);
             }
             if (isset($carrierRate['shipments'])) {
                 $this->persistShipments($carrierRate['shipments']);
             }
         }
     }
     return $result;
 }