function getUPSrates($pkg) { global $messageStack, $currencies, $UPSRateCodes, $shipping_defaults; // Retrieve the user choices for services to rate shop // TBD - defaults per user db choice for now $user_choices = explode(',', str_replace(' ', '', MODULE_SHIPPING_UPS_TYPES)); $UPSQuote = array(); // Initialize the Response Array $arrRates = array(); // Initialize the Rate Output array $this->package = $pkg->split_shipment($pkg); if (!$this->package) { $messageStack->add(SHIPPING_UPS_PACKAGE_ERROR . $pkg->pkg_weight, 'error'); return false; } if ($shipping_defaults['TnTEnable'] && gen_get_country_iso_2($pkg->ship_to_country_code) == 'US') { // Use UPS time in transit to get shipment time $strXML = $this->FormatTnTRequest(); $url = MODULE_SHIPPING_UPS_TEST_MODE == 'Test' ? MODULE_SHIPPING_UPS_TNT_URL_TEST : MODULE_SHIPPING_UPS_TNT_URL; $SubmitXML = GetXMLString($strXML, $url, "POST"); // Check for XML request errors if ($SubmitXML['result'] == 'error') { return $SubmitXML; } $ResponseXML = $SubmitXML['xmlString']; // Check for errors $XMLPath = 'TimeInTransitResponse:Response:ResponseStatusCode'; $XMLSuccess = GetNodeData($ResponseXML, $XMLPath); if (!$XMLSuccess) { // fetch the error code // $XMLPath = 'TimeInTransitResponse:Response:Error:ErrorSeverity'; // $XMLErrorSev = GetNodeData($ResponseXML, $XMLPath); $XMLPath = 'TimeInTransitResponse:Response:Error:ErrorCode'; $XMLErrorType = GetNodeData($ResponseXML, $XMLPath); $XMLPath = 'TimeInTransitResponse:Response:Error:ErrorDescription'; $XMLErrorDesc = GetNodeData($ResponseXML, $XMLPath); $messageStack->add(SHIPPING_UPS_TNT_ERROR . $XMLErrorType . ' - ' . $XMLErrorDesc, 'error'); return false; } // See if service list returned or candidate city list is returned. $XMLPath = 'TimeInTransitResponse:TransitResponse:ServiceSummary:Service:Code'; // name of the index in array $XMLService = GetNodeData($ResponseXML, $XMLPath); if (!$XMLService) { // fetch the candidate list city and matching postal codes in case bad zip provided $XMLStart = 'TimeInTransitResponse:TransitResponse:TransitToList:Candidate'; $XMLIndexName = ''; // needs to be null to create single dimension array of cities $TagsToFind = array('index' => 'AddressArtifactFormat:PoliticalDivision2'); // use 'index' to create non-associate array $CityCodes['City'] = GetNodeArray($ResponseXML, $XMLStart, $XMLIndexName, $TagsToFind); $TagsToFind = array('index' => 'AddressArtifactFormat:PostcodePrimaryLow'); // use 'index' to create non-associate array $CityCodes['PostalCode'] = GetNodeArray($ResponseXML, $XMLStart, $XMLIndexName, $TagsToFind); $UPSQuote['validcities'] = $CityCodes; $UPSQuote['result'] = 'CityMatch'; return $UPSQuote; } else { // fetch the service list $XMLStart = 'TimeInTransitResponse:TransitResponse:ServiceSummary'; // base location in the XML string (repeated) $XMLIndexName = 'Service:Code'; // name of the index in array $TagsToFind = array(); $TagsToFind['DeliveryDOW'] = 'EstimatedArrival:DayOfWeek'; //index name and path from XMLStart to get data $TagsToFind['DeliveryTime'] = 'EstimatedArrival:Time'; $TagsToFind['TransitDays'] = 'EstimatedArrival:BusinessTransitDays'; $Services = GetNodeArray($ResponseXML, $XMLStart, $XMLIndexName, $TagsToFind); // Fetch the Ship to state to insert if left blank $XMLPath = 'TimeInTransitResponse:TransitResponse:TransitTo:AddressArtifactFormat:PoliticalDivision1'; // Get Ship To State $defaults['ShipToStateProv'] = GetNodeData($ResponseXML, $XMLPath); // Fetch the Ship to City $XMLPath = 'TimeInTransitResponse:TransitResponse:TransitTo:AddressArtifactFormat:PoliticalDivision2'; // Get Ship To State $CityCodes['City'][0] = GetNodeData($ResponseXML, $XMLPath); $defaults['City'] = $CityCodes['City'][0]; $CityCodes['PostalCode'][0] = ''; } foreach ($this->UPSTnTCodes as $key => $value) { if (isset($Services[$key]) && in_array($value, $user_choices)) { $arrRates[$this->code][$value]['notes'] = $Services[$key]['TransitDays'] . SHIPPING_UPS_RATE_TRANSIT . $Services[$key]['DeliveryDOW']; } } } // ******************************************************************************************* // Fetch the book rates from UPS $strXML = $this->FormatRateRequest(); //echo 'Ship Request xmlString = ' . htmlspecialchars($strXML) . '<br />'; $url = MODULE_SHIPPING_UPS_TEST_MODE == 'Test' ? MODULE_SHIPPING_UPS_RATE_URL_TEST : MODULE_SHIPPING_UPS_RATE_URL; $SubmitXML = GetXMLString($strXML, $url, "POST"); //echo 'Ship Request response string = ' . htmlspecialchars($SubmitXML['xmlString']) . '<br />'; // Check for XML request errors if ($SubmitXML['result'] == 'error') { $messageStack->add(SHIPPING_UPS_CURL_ERROR . $SubmitXML['message'], 'error'); return false; } $ResponseXML = $SubmitXML['xmlString']; // Check for errors returned from UPS $XMLPath = 'RatingServiceSelectionResponse:Response:ResponseStatusCode'; $XMLSuccess = GetNodeData($ResponseXML, $XMLPath); if (!$XMLSuccess) { // fetch the error code // $XMLPath = 'RatingServiceSelectionResponse:Response:Error:ErrorSeverity'; // $XMLErrorSev = GetNodeData($ResponseXML, $XMLPath); $XMLPath = 'RatingServiceSelectionResponse:Response:Error:ErrorCode'; $XMLErrorType = GetNodeData($ResponseXML, $XMLPath); $XMLPath = 'RatingServiceSelectionResponse:Response:Error:ErrorDescription'; $XMLErrorDesc = GetNodeData($ResponseXML, $XMLPath); $messageStack->add(SHIPPING_UPS_RATE_ERROR . $XMLErrorType . ' - ' . $XMLErrorDesc, 'error'); return false; } // Fetch the UPS Rates $XMLStart = 'RatingServiceSelectionResponse:RatedShipment'; $XMLIndexName = 'Service:Code'; // name of the index in array $TagsToFind = array(); $TagsToFind = array('TransitDays' => 'GuaranteedDaysToDelivery', 'TransitTime' => 'ScheduledDeliveryTime', 'ShipmentCost' => 'NegotiatedRates:NetSummaryCharges:GrandTotal:MonetaryValue', 'BookCharges' => 'TotalCharges:MonetaryValue'); $UPSRates = GetNodeArray($ResponseXML, $XMLStart, $XMLIndexName, $TagsToFind); foreach ($this->UPSRateCodes as $key => $value) { if (isset($UPSRates[$key]) && in_array($value, $user_choices)) { if ($UPSRates[$key]['BookCharges'] != "") { $arrRates[$this->code][$value]['book'] = $currencies->clean_value($UPSRates[$key]['BookCharges']); } if ($UPSRates[$key]['ShipmentCost'] != "") { $arrRates[$this->code][$value]['cost'] = $currencies->clean_value($UPSRates[$key]['ShipmentCost']); } $arrRates[$this->code][$value]['note'] = ''; if ($UPSRates[$key]['TransitDays'] != "") { $arrRates[$this->code][$value]['note'] .= $UPSRates[$key]['TransitDays'] . ' Day(s) Transit. '; } $arrRates[$this->code][$value]['note'] .= $UPSRates[$key]['TransitTime'] != "" ? 'by ' . $UPSRates[$key]['TransitTime'] : 'by End of Day'; if (function_exists('ups_shipping_rate_calc')) { $arrRates[$this->code][$value]['quote'] = ups_shipping_rate_calc($arrRates[$this->code][$value]['book'], $arrRates[$this->code][$value]['cost'], $value); } else { if ($UPSRates[$key]['BookCharges'] != "") { $arrRates[$this->code][$value]['quote'] = $UPSRates[$key]['BookCharges']; } } } } // All calculations finished, return $UPSQuote['result'] = 'success'; $UPSQuote['rates'] = $arrRates; return $UPSQuote; }
function queryFedEx($pkg, $user_choices, $num_packages) { // Fetch the book rates from FEDEX global $messageStack, $currencies; $arrRates = array(); $strXML = $this->FormatFedExRateRequest($pkg, $num_packages); //echo 'FedEx Express XML Submit String:<br />' . htmlspecialchars($strXML) . '<br />'; $SubmitXML = GetXMLString($strXML, $this->rate_url, "POST"); // Check for XML request errors if ($SubmitXML['result'] == 'error') { $messageStack->add(SHIPPING_FEDEX_CURL_ERROR . $SubmitXML['message'], 'error'); return false; } $ResponseXML = $SubmitXML['xmlString']; //echo '<br />XML Return String:<br />' . htmlspecialchars($ResponseXML) . '<br />'; // Check for errors $XMLFail = GetNodeData($ResponseXML, 'Error:Code'); if ($XMLFail) { // fetch the error code $XMLErrorDesc = GetNodeData($ResponseXML, 'Error:Message'); $FEDEXQuote['result'] = 'error'; $messageStack->add(SHIPPING_FEDEX_RATE_ERROR . $XMLFail . ' - ' . $XMLErrorDesc, 'error'); return false; } // Fetch the FedEx Rates $XMLStart = 'Entry'; $XMLIndexName = 'Service'; // name of the index in array $TagsToFind = array('DeliveryDOW' => 'DeliveryDay', 'TransitDays' => 'TimeInTransit', 'ShipmentCost' => 'EstimatedCharges:DiscountedCharges:NetCharge', 'BookCharges' => 'EstimatedCharges:ListCharges:NetCharge'); $fdxRates = GetNodeArray($ResponseXML, $XMLStart, $XMLIndexName, $TagsToFind); //echo 'FedEx rate array = '; print_r($fdxRates); echo '<br />'; foreach ($this->FedExRateCodes as $key => $value) { if (isset($fdxRates[$key]) && in_array($value, $user_choices)) { if ($fdxRates[$key]['BookCharges'] != "") { $arrRates[$this->code][$value]['book'] = $currencies->clean_value($fdxRates[$key]['BookCharges']); } if ($fdxRates[$key]['ShipmentCost'] != "") { $arrRates[$this->code][$value]['cost'] = $currencies->clean_value($fdxRates[$key]['ShipmentCost']); } $arrRates[$this->code][$value]['note'] = ''; if ($fdxRates[$key]['TransitDays'] != "") { $arrRates[$this->code][$value]['note'] .= $fdxRates[$key]['TransitDays'] . ' Day(s) Transit. '; } if ($fdxRates[$key]['DeliveryDOW'] != "") { $arrRates[$this->code][$value]['note'] .= 'Arrives ' . $fdxRates[$key]['DeliveryDOW']; } if (function_exists('fedex_shipping_rate_calc')) { $arrRates[$this->code][$value]['quote'] = fedex_shipping_rate_calc($arrRates[$this->code][$value]['book'], $arrRates[$this->code][$value]['cost'], $value); } else { if ($fdxRates[$key]['BookCharges'] != "") { $arrRates[$this->code][$value]['quote'] = $fdxRates[$key]['BookCharges']; } } } } return $arrRates; }
function getUPSrates($pkg) { global $messageStack, $currencies, $UPSRateCodes, $shipping_defaults; $user_choices = explode(',', str_replace(' ', '', MODULE_SHIPPING_UPS_TYPES)); $UPSQuote = array(); $arrRates = array(); $this->package = $pkg->split_shipment($pkg); if (!$this->package) { $messageStack->add(SHIPPING_UPS_PACKAGE_ERROR . $pkg->pkg_weight, 'error'); return false; } if ($shipping_defaults['TnTEnable'] && gen_get_country_iso_2_from_3($pkg->ship_to_country_code) == 'US') { // Use UPS time in transit to get shipment time $strXML = $this->FormatTnTRequest(); $url = MODULE_SHIPPING_UPS_TEST_MODE == 'Test' ? MODULE_SHIPPING_UPS_TNT_URL_TEST : MODULE_SHIPPING_UPS_TNT_URL; $SubmitXML = GetXMLString($strXML, $url, "POST"); // Check for XML request errors if ($SubmitXML['result'] == 'error') { return $SubmitXML; } $ResponseXML = xml_to_object($SubmitXML['xmlString']); // Check for errors if (!$ResponseXML->TimeInTransitResponse->Response->ResponseStatusCode) { $XMLErrorType = $ResponseXML->TimeInTransitResponse->Response->Error->ErrorCode; $XMLErrorDesc = $ResponseXML->TimeInTransitResponse->Response->Error->ErrorDescription; $messageStack->add(SHIPPING_UPS_TNT_ERROR . $XMLErrorType . ' - ' . $XMLErrorDesc, 'error'); return false; } // See if service list returned or candidate city list is returned. $XMLService = $ResponseXML->TimeInTransitResponse->TransitResponse->ServiceSummary->Service->Code; if (!$XMLService) { // fetch the candidate list city and matching postal codes in case bad zip provided $XMLStart = 'TimeInTransitResponse:TransitResponse:TransitToList:Candidate'; $XMLIndexName = ''; // needs to be null to create single dimension array of cities $TagsToFind = array('index' => 'AddressArtifactFormat:PoliticalDivision2'); // use 'index' to create non-associate array $CityCodes['City'] = GetNodeArray($ResponseXML, $XMLStart, $XMLIndexName, $TagsToFind); $TagsToFind = array('index' => 'AddressArtifactFormat:PostcodePrimaryLow'); // use 'index' to create non-associate array $CityCodes['PostalCode'] = GetNodeArray($ResponseXML, $XMLStart, $XMLIndexName, $TagsToFind); $UPSQuote['validcities'] = $CityCodes; $UPSQuote['result'] = 'CityMatch'; return $UPSQuote; } else { // fetch the service list $XMLStart = 'TimeInTransitResponse:TransitResponse:ServiceSummary'; // base location in the XML string (repeated) $XMLIndexName = 'Service:Code'; // name of the index in array $TagsToFind = array(); $TagsToFind['DeliveryDOW'] = 'EstimatedArrival:DayOfWeek'; //index name and path from XMLStart to get data $TagsToFind['DeliveryTime'] = 'EstimatedArrival:Time'; $TagsToFind['TransitDays'] = 'EstimatedArrival:BusinessTransitDays'; $Services = GetNodeArray($ResponseXML, $XMLStart, $XMLIndexName, $TagsToFind); // Fetch the Ship to state to insert if left blank $defaults['ShipToStateProv'] = $ResponseXML->TimeInTransitResponse->TransitResponse->TransitTo->AddressArtifactFormat->PoliticalDivision1; // Fetch the Ship to City $XMLPath = ''; // Get Ship To State $CityCodes['City'][0] = $ResponseXML->TimeInTransitResponse->TransitResponse->TransitTo->AddressArtifactFormat->PoliticalDivision2; $defaults['City'] = $CityCodes['City'][0]; $CityCodes['PostalCode'][0] = ''; } foreach ($this->UPSTnTCodes as $key => $value) { if (isset($Services[$key]) && in_array($value, $user_choices)) { $arrRates[$this->code][$value]['notes'] = $Services[$key]['TransitDays'] . SHIPPING_UPS_RATE_TRANSIT . $Services[$key]['DeliveryDOW']; } } } // ******************************************************************************************* // Fetch the book rates from UPS $strXML = $this->FormatRateRequest(); //echo 'Ship Request xmlString = ' . htmlspecialchars($strXML) . '<br />'; $url = MODULE_SHIPPING_UPS_TEST_MODE == 'Test' ? MODULE_SHIPPING_UPS_RATE_URL_TEST : MODULE_SHIPPING_UPS_RATE_URL; $SubmitXML = GetXMLString($strXML, $url, "POST"); //echo 'Ship Request response string = ' . htmlspecialchars($SubmitXML['xmlString']) . '<br />'; // Check for XML request errors if ($SubmitXML['result'] == 'error') { $messageStack->add(SHIPPING_UPS_CURL_ERROR . $SubmitXML['message'], 'error'); return false; } $ResponseXML = xml_to_object($SubmitXML['xmlString']); //echo 'Ship Request response object = '; print_r($ResponseXML); echo '<br />'; exit(); // Check for errors returned from UPS if (!$ResponseXML->RatingServiceSelectionResponse->Response->ResponseStatusCode) { // fetch the error code $XMLErrorType = $ResponseXML->RatingServiceSelectionResponse->Response->Error->ErrorCode; $XMLErrorDesc = $ResponseXML->RatingServiceSelectionResponse->Response->Error->ErrorDescription; $messageStack->add(SHIPPING_UPS_RATE_ERROR . $XMLErrorType . ' - ' . $XMLErrorDesc, 'error'); return false; } // Fetch the UPS Rates foreach ($ResponseXML->RatingServiceSelectionResponse->RatedShipment as $rate) { $code = $rate->Service->Code; if (!$code) { continue; } $value = $this->UPSRateCodes[$code]; $del_time = $rate->GuaranteedDaysToDelivery . ' days by ' . $rate->ScheduledDeliveryTime; $book = $rate->TotalCharges->MonetaryValue; $cost = $rate->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue; if (in_array($value, $user_choices)) { if ($book != "") { $arrRates[$this->code][$value]['book'] = $currencies->clean_value($book); } if ($cost != "") { $arrRates[$this->code][$value]['cost'] = $currencies->clean_value($cost); } $arrRates[$this->code][$value]['note'] = ''; if ($rate->GuaranteedDaysToDelivery != "") { $arrRates[$this->code][$value]['note'] .= $rate->GuaranteedDaysToDelivery; } $arrRates[$this->code][$value]['note'] .= $rate->ScheduledDeliveryTime != "" ? ' by ' . $rate->ScheduledDeliveryTime : ' by End of Day'; if (function_exists('ups_shipping_rate_calc')) { $arrRates[$this->code][$value]['quote'] = ups_shipping_rate_calc($arrRates[$this->code][$value]['book'], $arrRates[$this->code][$value]['cost'], $value); } else { if ($book != "") { $arrRates[$this->code][$value]['quote'] = $book; } } } } /* The LTL needs debugging, UPS doesn't support PHP and doesn't provide examples for their web services, gotta love them // now check Ground LTL if ($pkg->pkg_weight >= 150) { $client = new SoapClient(PATH_TO_LTL_RATE_WSDL, array('trace' => 1)); $strXML = $this->FormatLTLRateRequest($pkg); try { echo 'UPS LTL Freight XML Submit String:<pre>' . ($strXML) . '</pre><br />'; $response = $client->ProcessFreightRate($strXML); echo 'Request <pre>' . htmlspecialchars($client->__getLastRequest()) . '</pre>'; echo 'Response <pre>' . htmlspecialchars($client->__getLastResponse()) . '</pre>'; echo 'Rate response array = '; print_r($response); echo '<br />'; if ($response->HighestSeverity != 'FAILURE' && $response->HighestSeverity != 'ERROR') { } else { // error returned } } catch (SoapFault $exception) { echo 'Fault Request <pre>' . htmlspecialchars($client->__getLastRequest()) . '</pre>'; echo 'Fault Response <pre>' . htmlspecialchars($client->__getLastResponse()) . '</pre>'; $message = " [soap fault] ({$exception->faultcode}) {$exception->faultstring}"; $messageStack->add(SHIPPING_UPS_CURL_ERROR . $message, 'error'); return false; } //echo 'arrRates array = '; print_r($arrRates); echo '<br /><br />'; } */ $UPSQuote['result'] = 'success'; $UPSQuote['rates'] = $arrRates; return $UPSQuote; }