/** * Get publisher id * * @param string $email Email * * @return string */ public function getPublisherId($email) { $publisherId = null; $sellerName = \XLite\Core\Config::getInstance()->Company->company_name; $data = array('sellerName' => $sellerName, 'emailAddress' => $email, 'bnCode' => static::BN_CODE); $request = new \XLite\Core\HTTP\Request(static::END_POINT); if (function_exists('curl_version')) { $request->setAdditionalOption(\CURLOPT_SSLVERSION, 1); $curlVersion = curl_version(); if ($curlVersion && $curlVersion['ssl_version'] && 0 !== strpos($curlVersion['ssl_version'], 'NSS')) { $request->setAdditionalOption(\CURLOPT_SSL_CIPHER_LIST, 'TLSv1'); } } $request->body = json_encode($data); $request->verb = 'POST'; $timeStamp = LC_START_TIME . '000'; $authorization = 'FPA ' . static::CLIENT_KEY; $authorization .= ':' . sha1(static::SHARED_SECRET . $timeStamp); $authorization .= ':' . $timeStamp; $request->setHeader('Authorization', $authorization); $request->setHeader('Accept', 'application/json'); $request->setHeader('Content-Type', 'application/json'); $response = $request->sendRequest(); \XLite\Module\CDev\Paypal\Main::addLog('getPublisherId', $response->body); if (201 == $response->code) { $responseData = json_decode($response->body, true); if ($responseData && isset($responseData['publisherId'])) { $publisherId = $responseData['publisherId']; } } return $publisherId; }
/** * Call "Get Manifest" request * * Reason to Call: * To retrieve the set of information links for a particular manifest that was previously created as part of Transmit Shipments. * * More info at: * https://www.canadapost.ca/cpo/mc/business/productsservices/developers/services/shippingmanifest/manifest.jsf * * @param \XLite\Module\XC\CanadaPost\Model\Order\Parcel\Manifest $manifest Manifest object * * @return \XLite\Core\CommonCell */ public function callGetManifestRequest(\XLite\Module\XC\CanadaPost\Model\Order\Parcel\Manifest $manifest) { $apiHost = $manifest->getHref(); $result = new \XLite\Core\CommonCell(); try { $request = new \XLite\Core\HTTP\Request($apiHost); $request->requestTimeout = $this->requestTimeout; $request->verb = 'GET'; $request->setHeader('Authorization', 'Basic ' . base64_encode(static::getCanadaPostConfig()->user . ':' . static::getCanadaPostConfig()->password)); $request->setHeader('Accept', 'application/vnd.cpc.manifest-v5+xml'); $request->setHeader('Accept-language', static::ACCEPT_LANGUAGE_EN); if (static::isOnBehalfOfAMerchant()) { $request->setHeader('Platform-id', $this->getPlatformId()); } $response = $request->sendRequest(); if (isset($response->body) && !empty($response->body)) { // Parse response to object $result = $this->parseResponse($response->body); } else { $result->errors = array('INTERNAL' => sprintf('Error while connecting to the Canada Post host (%s) during Get Manifest request', $apiHost)); } if (static::getCanadaPostConfig()->debug_enabled) { // Save debug log static::logApiCall($apiHost, 'Get Manifest', '', $response->body); } } catch (\Exception $e) { if (!isset($result->errors)) { $result->errors = array(); } $result->errors += array($e->getCode(), $e->getMessage()); } return $result; }
/** * Call "Get Service" request * * Reason to Call: * To find out details for a given postal service such as the dimension and weight limits and the available options. * * @param string $endpoint Service endpoint (URL) * @param string $acceptHeader Accept header value * * @return \XLite\Core\CommonCell */ protected function callGetService($endpoint, $acceptHeader) { $result = new \XLite\Core\CommonCell(); try { $request = new \XLite\Core\HTTP\Request($endpoint); $request->requestTimeout = $this->requestTimeout; $request->verb = 'GET'; $request->setHeader('Authorization', 'Basic ' . base64_encode(static::getCanadaPostConfig()->user . ':' . static::getCanadaPostConfig()->password)); $request->setHeader('Accept', $acceptHeader); $request->setHeader('Accept-language', $this->getAcceptLanguage()); if (static::isOnBehalfOfAMerchant()) { $request->setHeader('Platform-id', $this->getPlatformId()); } $response = $request->sendRequest(); if (isset($response->body) && !empty($response->body)) { // Parse XML response to object $result = $this->parseResponseGetService($response->body); } else { // Register request error $result->errors = array($this->createErrorMessage('INTERNAL', sprintf('Error while connecting to the Canada Post host (%s) during "Get Service" request', $endpoint))); } if (static::getCanadaPostConfig()->debug_enabled) { // Save debug log static::logApiCall($endpoint, 'Get Service', '', $response->body); } } catch (\Exception $e) { // Register exception error $errorMessage = $this->createErrorMessage($this->getCode(), $this->getMessage()); $result->errors = array_merge((array) $result->errors, array($errorMessage)); } 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; }