/** * Call "Get Maifest" request * * @return boolean */ public function callApiGetManifest() { $result = false; $data = \XLite\Module\XC\CanadaPost\Core\API::getInstance()->callGetManifestRequest($this); if (isset($data->errors)) { // Save errors $this->apiCallErrors = $data->errors; } else { sleep(2); // to get Canada Post server time to generate PDF documents $this->setPoNumber($data->poNumber); foreach ($data->links as $link) { $manifestLink = new \XLite\Module\XC\CanadaPost\Model\Order\Parcel\Manifest\Link(); $manifestLink->setManifest($this); $this->addLink($manifestLink); foreach (array('rel', 'href', 'mediaType', 'idx') as $linkField) { if (isset($link->{$linkField})) { $manifestLink->{'set' . \XLite\Core\Converter::convertToCamelCase($linkField)}($link->{$linkField}); } } if (!$manifestLink->callApiGetArtifact() && $manifestLink->getApiCallErrors()) { // Error is occurred while downloading PDF documents if (!isset($this->apiCallErrors)) { $this->apiCallErrors = array(); } $this->apiCallErrors += $manifestLink->getApiCallErrors(); } } \XLite\Core\Database::getEM()->flush(); $result = true; } return $result; }
/** * doQuery * * lowlevel query * * @param mixed $data Array of prepared package data * @param boolean $ignoreCache Flag: if true then do not get rates from cache * * @return void */ protected function doQuery($data, $ignoreCache) { $rates = array(); $availableMethods = \XLite\Core\Database::getRepo('XLite\\Model\\Shipping\\Method')->findMethodsByProcessor($this->getProcessorId()); $config = $this->getConfig(); $XMLData = $this->getXMLData($data); try { if (!$ignoreCache) { $cachedRates = $this->getDataFromCache($XMLData); } if (isset($cachedRates)) { $result = $cachedRates; } elseif (\XLite\Model\Shipping::isIgnoreLongCalculations()) { // Ignore rates calculation return array(); } else { $postURL = \XLite\Module\XC\CanadaPost\Core\API::getInstance()->getGetRatesEndpoint(); $bouncer = new \XLite\Core\HTTP\Request($postURL); $bouncer->requestTimeout = 5; $bouncer->body = $XMLData; $bouncer->verb = 'POST'; $bouncer->setHeader('Authorization', 'Basic ' . base64_encode($config->user . ':' . $config->password)); $bouncer->setHeader('Accept', 'application/vnd.cpc.ship.rate-v2+xml'); $bouncer->setHeader('Content-Type', 'application/vnd.cpc.ship.rate-v2+xml'); $bouncer->setHeader('Accept-language', \XLite\Module\XC\CanadaPost\Core\API::ACCEPT_LANGUAGE_EN); if (\XLite\Module\XC\CanadaPost\Core\API::isOnBehalfOfAMerchant()) { $bouncer->setHeader('Platform-id', \XLite\Module\XC\CanadaPost\Core\API::getInstance()->getPlatformId()); } $response = $bouncer->sendRequest(); $result = $response->body; if (200 == $response->code) { $this->saveDataInCache($XMLData, $result); } else { $this->errorMsg = sprintf('Error while connecting to the Canada Post host (%s)', $postURL); } if (\XLite\Core\Config::getInstance()->XC->CanadaPost->debug_enabled) { // Save debug log \XLite\Module\XC\CanadaPost\Core\API::logApiCall($postURL, 'Get Rates', $XMLData, $result); } } // Save communication log for test request only (ignoreCache is set for test requests only) if ($ignoreCache === true) { $this->apiCommunicationLog[] = array('post URL' => $postURL, 'request' => htmlentities($XMLData), 'response' => htmlentities(\XLite\Core\XML::getInstance()->getFormattedXML($result))); } $response = $this->parseResponse($result); if (!isset($this->errorMsg) && !isset($response['err_msg']) && !empty($response['services'])) { $conversionRate = \XLite\Module\XC\CanadaPost\Core\API::getCurrencyConversionRate(); foreach ($response['services'] as $service) { $rate = new \XLite\Model\Shipping\Rate(); $method = $this->getShippingMethod($service['service_code'], $availableMethods); if (!isset($method)) { // Unknown method received: add this to the database with disabled status $this->addShippingMethod($service); } elseif ($method->getEnabled()) { // Method is registered and enabled $rate->setMethod($method); $rate->setBaseRate($service['rate'] * $conversionRate); $rates[$service['service_code']] = $rate; } } } elseif (!isset($this->errorMsg) || isset($response['err_msg'])) { $this->errorMsg = isset($response['err_msg']) ? $response['err_msg'] : ($this->errorMsg ?: 'Unknown error'); } } catch (\Exception $e) { $this->errorMsg = $e->getMessage(); } return $rates; }
/** * Call "Transmit Shipments" request (for Contracted shipments only) * To get error message you need to call "getApiCallErrors" method (if return is false) * * @return boolean */ protected function callApiTransmitShipment() { $result = false; $data = \XLite\Module\XC\CanadaPost\Core\API::getInstance()->callTransmitShipmentsRequest($this); if (isset($data->errors)) { // Save errors $this->apiCallErrors = $data->errors; } else { // Valid result sleep(2); // time to generate manifests $shipment = $this->getShipment(); foreach ($data->links as $link) { $manifest = new \XLite\Module\XC\CanadaPost\Model\Order\Parcel\Manifest(array('rel' => $link->rel, 'href' => $link->href, 'media_type' => $link->mediaType)); if (isset($link->idx)) { $manifest->setIdx($link->idx); } \XLite\Core\Database::getEM()->persist($manifest); $shipment->addManifest($manifest); if (!$manifest->callApiGetManifest() && $manifest->getApiCallErrors()) { // Error is occurred if (null === $this->apiCallErrors) { $this->apiCallErrors = array(); } $this->apiCallErrors += $manifest->getApiCallErrors(); } } \XLite\Core\Database::getEM()->flush(); $result = true; } return $result; }
/** * Get X-Cart platform ID * * @return string */ public function getPlatformId() { return \XLite\Module\XC\CanadaPost\Core\API::getInstance()->getPlatformId(); }
/** * Call "Get Artifact" request (i.e. download PDF file) * To get error message you need to call "getApiCallErrors" method (if return is false) * * @param boolean $flushChanges Flag - flush changes or not * * @return boolean */ public function callApiGetArtifact($flushChanges = false) { $result = false; if ($this->isGetArtifactCallAllowed() && $this->getStorageClass()) { $data = \XLite\Module\XC\CanadaPost\Core\API::getInstance()->callGetArtifactRequest($this); $storageClass = $this->getStorageClass(); if (isset($data->filePath) && !empty($data->filePath)) { // Save PDF document to storage $storage = $this->getStorage(); if (!isset($storage)) { $storage = new $storageClass(); $storage->setLink($this); $this->setStorage($storage); } $storage->loadFromLocalFile($data->filePath); $storage->setMime($this->getMediaType()); \Includes\Utils\FileManager::deleteFile($data->filePath); $result = true; if ($flushChanges) { \XLite\Core\Database::getEM()->flush(); } } if (isset($data->errors)) { $this->apiCallErrors = $data->errors; } } return $result; }