Beispiel #1
0
 /**
  * Check - is selected shipping method support delivery to post office
  *
  * @return boolean
  */
 public function isSelectedMethodSupportDeliveryToPO()
 {
     $rate = $this->getSelectedShippingRate();
     return $rate && 'capost' == $rate->getMethod()->getProcessor() && in_array($rate->getMethod()->getCode(), \XLite\Module\XC\CanadaPost\Core\API::getAllowedForDelivetyToPOMethodCodes());
 }
Beispiel #2
0
 /**
  * Check - is shipping method is support "Delivery to Post Office" feature
  *
  * @param \XLite\Model\Shipping\Method $method Shipping method object
  *
  * @return boolean
  */
 protected function isMethodSupportDeliveryToPO(\XLite\Model\Shipping\Method $method)
 {
     return 'capost' == $method->getProcessor() && in_array($method->getCode(), \XLite\Module\XC\CanadaPost\Core\API::getAllowedForDelivetyToPOMethodCodes());
 }
Beispiel #3
0
 /**
  * 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;
 }
Beispiel #4
0
 /**
  * Get CanadaPost options object
  *
  * @return \XLite\Core\CommonCell
  */
 protected function getConfig()
 {
     return \XLite\Module\XC\CanadaPost\Core\API::getCanadaPostConfig();
 }
Beispiel #5
0
 /**
  * Get X-Cart platform ID
  *
  * @return string
  */
 public function getPlatformId()
 {
     return \XLite\Module\XC\CanadaPost\Core\API::getInstance()->getPlatformId();
 }
Beispiel #6
0
 /**
  * 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;
 }
Beispiel #7
0
 /**
  * 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;
 }
Beispiel #8
0
    /**
     * Generate XML request
     *
     * @param array $data Array of package data
     *
     * @return string
     */
    protected function getXMLData($data)
    {
        $config = $this->getConfiguration();
        $xmlHeader = '<?xml version="1.0" encoding="utf-8"?' . '>';
        //  Option applies to this shipment.
        $opts = array();
        if ($config->coverage > 0 && $data['subtotal'] > 0) {
            // Add coverage (insuarance) option
            if ($config->coverage != 100) {
                $data['subtotal'] = $data['subtotal'] / 100 * $config->coverage;
            }
            $coverage = \XLite\Module\XC\CanadaPost\Core\API::adjustFloatValue($data['subtotal'], 2, 0.01, 99999.99000000001);
            $opts[] = <<<OUT
    <option>
        <option-code>COV</option-code>
        <option-amount>{$coverage}</option-amount>
    </option>
OUT;
        }
        $optionsXML = '';
        if ($opts) {
            $options = implode(PHP_EOL, $opts);
            $optionsXML = <<<OUT
<options>
{$options}
</options>
OUT;
        }
        $contractId = '';
        $customerNumber = '';
        if (\XLite\Module\XC\CanadaPost\Core\API::QUOTE_TYPE_CONTRACTED === $config->quote_type) {
            $customerNumber = <<<OUT
<customer-number>{$config->customer_number}</customer-number>
OUT;
            if ($config->contract_id) {
                $contractId = <<<OUT
<contract-id>{$config->contract_id}</contract-id>
OUT;
            }
        }
        $parcelCharacteristics = '';
        $data['weight'] = \XLite\Module\XC\CanadaPost\Core\API::adjustFloatValue($data['weight'], 3, 0.001, 99.999);
        $weight = <<<OUT
<weight>{$data['weight']}</weight>
OUT;
        $dimensions = '';
        if (!empty($data['box'])) {
            $length = $data['box']['length'];
            $width = $data['box']['width'];
            $height = $data['box']['height'];
        } elseif ($config->length && $config->width && $config->height) {
            $length = $config->length;
            $width = $config->width;
            $height = $config->height;
        }
        if (!empty($length) && !empty($width) && !empty($height)) {
            $length = \XLite\Module\XC\CanadaPost\Core\API::adjustFloatValue($length, 1, 0.1, 999.9);
            $width = \XLite\Module\XC\CanadaPost\Core\API::adjustFloatValue($width, 1, 0.1, 999.9);
            $height = \XLite\Module\XC\CanadaPost\Core\API::adjustFloatValue($height, 1, 0.1, 999.9);
            $dimensions = <<<OUT
<dimensions>
    <length>{$length}</length>
    <width>{$width}</width>  
    <height>{$height}</height>
</dimensions>
OUT;
        }
        $parcelCharacteristics .= <<<OUT
<parcel-characteristics>
    {$weight}
    {$dimensions}
</parcel-characteristics>
OUT;
        $dstPostalCode = \XLite\Module\XC\CanadaPost\Core\API::strToUpper(preg_replace('/\\s+/', '', $data['dstAddress']['zipcode']));
        $srcPostalCode = \XLite\Module\XC\CanadaPost\Core\API::strToUpper(preg_replace('/\\s+/', '', $data['srcAddress']['zipcode']));
        if ('CA' === $data['dstAddress']['country']) {
            $destination = <<<OUT
<domestic>
    <postal-code>{$dstPostalCode}</postal-code>
</domestic>
OUT;
        } elseif ('US' === $data['dstAddress']['country']) {
            $destination = <<<OUT
<united-states>
    <zip-code>{$dstPostalCode}</zip-code>
</united-states>
OUT;
        } else {
            $destination = <<<OUT
<international>
    <country-code>{$data['dstAddress']['country']}</country-code>
</international>
OUT;
        }
        $quoteType = \XLite\Module\XC\CanadaPost\Core\API::QUOTE_TYPE_CONTRACTED === $config->quote_type ? 'commercial' : 'counter';
        $request = <<<OUT
{$xmlHeader}
<mailing-scenario xmlns="http://www.canadapost.ca/ws/ship/rate-v2">
    {$customerNumber}
    <quote-type>{$quoteType}</quote-type>
    {$optionsXML}
    {$contractId}
    {$parcelCharacteristics}
    <origin-postal-code>{$srcPostalCode}</origin-postal-code>
    <destination>{$destination}</destination>
</mailing-scenario>
OUT;
        return $request;
    }