/** * Save API call to the log file * * @param string $url API endpoint * @param string $callName Call name * @param string $requestData Request XML * @param string $responseData Response XML * * @return void */ public static function logApiCall($url, $callName, $requestData, $responseData) { \XLite\Logger::logCustom('CAPOST', var_export(array('Request URL' => $url, 'Request XML (' . $callName . ')' => $requestData, 'Response XML' => \XLite\Core\XML::getInstance()->getFormattedXML($responseData)), true)); }
/** * parses response and returns an associative array * * @param string $stringData XML response of capost api * * @return array */ protected function parseResponse($stringData) { $result = array(); $xml = \XLite\Core\XML::getInstance(); $xmlParsed = $xml->parse($stringData, $err); if (isset($xmlParsed['messages'])) { $result['err_msg'] = $xml->getArrayByPath($xmlParsed, 'messages/message/description/0/#'); } if (!isset($result['err_msg'])) { $services = $xml->getArrayByPath($xmlParsed, 'price-quotes/price-quote'); if ($services) { foreach ($services as $k => $v) { $result['services'][] = array('service_code' => $xml->getArrayByPath($v, 'service-code/0/#'), 'service_name' => $xml->getArrayByPath($v, 'service-name/0/#'), 'rate' => $xml->getArrayByPath($v, 'price-details/0/#/due/0/#')); } } } return $result; }
/** * Parses IntlRateV2 response and returns an associative array * * @param string $stringData Response received from USPS * * @return array */ protected function parseResponseIntl($stringData) { $result = array(); $xml = \XLite\Core\XML::getInstance(); $xmlParsed = $xml->parse($stringData, $err); if (isset($xmlParsed['Error'])) { $result['err_msg'] = $xml->getArrayByPath($xmlParsed, 'Error/Description/0/#'); } else { $error = $xml->getArrayByPath($xmlParsed, $this->getApiName() . 'Response/Package/Error'); if ($error) { $result['err_msg'] = $xml->getArrayByPath($error, 'Description/0/#'); } } if (!isset($result['err_msg'])) { $postage = $xml->getArrayByPath($xmlParsed, $this->getApiName() . 'Response/Package/Service'); if ($postage) { foreach ($postage as $k => $v) { $serviceName = $this->sanitizeServiceName($xml->getArrayByPath($v, '#/SvcDescription/0/#')); $result['postage'][] = array('CLASSID' => 'I-' . $xml->getArrayByPath($v, '@/ID') . '-' . $this->getUniqueMethodID($serviceName), 'MailService' => $this->getUSPSNamePrefix() . $serviceName, 'Rate' => $xml->getArrayByPath($v, '#/Postage/0/#')); } } } return $result; }
/** * Parcel response of the "Create Authorized Return" call * * @param string $responseXml Response XML data * * @return \XLite\Core\CommonCell */ protected function parseResponseCreateAuthorizedReturn($responseXml) { $result = new \XLite\Core\CommonCell(); // Parse XML document $xml = \XLite\Core\XML::getInstance(); $xmlParsed = $xml->parse($responseXml, $err); if (isset($xmlParsed['messages'])) { // Collect API error messages (using common method) $result->errors = $this->parseResponseErrors($xmlParsed); } else { if (isset($xmlParsed['authorized-return-info'])) { // Collect returned data from "Create Authorized Return" call $data = new \XLite\Core\CommonCell(); $data->trackingPin = $xml->getArrayByPath($xmlParsed, 'authorized-return-info/tracking-pin/0/#'); $data->links = $this->parseResponseLinks($xml->getArrayByPath($xmlParsed, 'authorized-return-info/links/link')); $result->authorizedReturnInfo = $data; } } return $result; }
public static function func_xml_format($xml) { return \XLite\Core\XML::getFormattedXML($xml); }
/** * Get shipping rate * * @param array $entry * * @return array */ protected function getRateAmount($entry) { $xml = \XLite\Core\XML::getInstance(); $config = $this->getConfiguration(); $currencyCode = $config->currency_code; $rateCurrency = $xml->getArrayByPath($entry, 'RatedShipmentDetails/ShipmentRateDetail/TotalNetCharge/Currency/0/#'); if ($rateCurrency != $currencyCode) { // Currency conversion is needed $ratedShipmentDetails = $xml->getArrayByPath($entry, 'RatedShipmentDetails'); // Try to find extact rate value $preciseRateFound = false; foreach ($ratedShipmentDetails as $key => $shipmentRateDetail) { $currencyExchangeRate = $xml->getArrayByPath($shipmentRateDetail, 'ShipmentRateDetail/CurrencyExchangeRate/RATE/0/#'); $fromCurrency = $xml->getArrayByPath($shipmentRateDetail, 'ShipmentRateDetail/CurrencyExchangeRate/FromCurrency/0/#'); $rateCurrency = $xml->getArrayByPath($shipmentRateDetail, 'ShipmentRateDetail/TotalNetCharge/Currency/0/#'); $estimatedRate = $xml->getArrayByPath($shipmentRateDetail, 'ShipmentRateDetail/TotalNetCharge/Amount/0/#'); if ($currencyExchangeRate == '1.0' && $fromCurrency == $currencyCode && $rateCurrency == $currencyCode) { // This rate type can be used without conversion $preciseRateFound = true; break; } } if (!$preciseRateFound) { // Rate type without conversion is not found/ Use conversion foreach ($ratedShipmentDetails as $key => $shipmentRateDetail) { $currencyExchangeRate = $xml->getArrayByPath($shipmentRateDetail, 'ShipmentRateDetail/CurrencyExchangeRate/RATE/0/#'); if (0 == $currencyExchangeRate) { continue; } $fromCurrency = $xml->getArrayByPath($shipmentRateDetail, 'ShipmentRateDetail/CurrencyExchangeRate/FromCurrency/0/#'); $intoCurrency = $xml->getArrayByPath($shipmentRateDetail, 'ShipmentRateDetail/CurrencyExchangeRate/IntoCurrency/0/#'); $rateCurrency = $xml->getArrayByPath($shipmentRateDetail, 'ShipmentRateDetail/TotalNetCharge/Currency/0/#'); $estimatedRate = $xml->getArrayByPath($shipmentRateDetail, 'ShipmentRateDetail/TotalNetCharge/Amount/0/#'); if ($fromCurrency == $rateCurrency) { $estimatedRate *= $currencyExchangeRate; break; } elseif ($intoCurrency == $rateCurrency) { $estimatedRate /= $currencyExchangeRate; break; } } } } // if ($rateCurrency != $currencyCode) { if (empty($estimatedRate)) { $estimatedRate = $xml->getArrayByPath($entry, 'RatedShipmentDetails/ShipmentRateDetail/TotalNetCharge/Amount/0/#'); } return $estimatedRate; }
/** * Parses response and returns an associative array * * @param string $response XML response of UPS API * * @return void */ protected function parseResponse($response) { $result = array(); $xml = \XLite\Core\XML::getInstance(); $xmlParsed = $xml->parse($response, $err); if (!empty($xmlParsed['RatingServiceSelectionResponse']['#']['Response'][0]['#']['Error'])) { // Error detected $this->errorMsg = sprintf('Error: %s - %s - %s', $xmlParsed['RatingServiceSelectionResponse']['#']['Response'][0]['#']['Error'][0]['#']['ErrorCode'][0]['#'], $xmlParsed['RatingServiceSelectionResponse']['#']['Response'][0]['#']['Error'][0]['#']['ErrorSeverity'][0]['#'], $xmlParsed['RatingServiceSelectionResponse']['#']['Response'][0]['#']['Error'][0]['#']['ErrorDescription'][0]['#']); } if (empty($this->errorMsg)) { // Parse rates $resultXML = $xml->getArrayByPath($xmlParsed, 'RatingServiceSelectionResponse/#/RatedShipment'); if ($resultXML && is_array($resultXML)) { $conversionRate = $this->getCurrencyConversionRate(); $currency = null; foreach ($resultXML as $service) { $rate = array(); $rate['serviceCode'] = $service['#']['Service'][0]['#']['Code'][0]['#']; $rate['deliveryTime'] = $service['#']['GuaranteedDaysToDelivery'][0]['#']; $currency = $service['#']['TotalCharges'][0]['#']['CurrencyCode'][0]['#']; if (isset($service['#']['NegotiatedRates'])) { $rate['totalCharges'] = $service['#']['NegotiatedRates'][0]['#']['NetSummaryCharges'][0]['#']['GrandTotal'][0]['#']['MonetaryValue'][0]['#']; } else { $rate['totalCharges'] = $service['#']['TotalCharges'][0]['#']['MonetaryValue'][0]['#']; } $rate['totalCharges'] *= $conversionRate; $result[] = $rate; } if ($currency && \XLite\Core\Config::getInstance()->XC->UPS->currency_code != $currency) { \XLite\Core\Database::getRepo('XLite\\Model\\Config')->createOption(array('category' => 'XC\\UPS', 'name' => 'currency_code', 'value' => $currency)); } } } return $result; }
/** * Add api communication message * * @param string $message API communication log message * * @return void */ protected function addApiCommunicationMessage($message) { if (!empty($message['request_data'])) { $message['request_data'] = htmlentities($this->filterRequestData($message['request_data'])); } if (!empty($message['response'])) { $message['response'] = htmlentities(\XLite\Core\XML::getInstance()->getFormattedXML($message['response'])); } parent::addApiCommunicationMessage($message); }