Ejemplo n.º 1
0
Archivo: Rate.php Proyecto: kingsj/core
 /**
  * testCreate
  *
  * @return void
  * @access public
  * @see    ____func_see____
  * @since  1.0.0
  */
 public function testCreate()
 {
     // Prepare data for rate
     $method = $this->getMethodByName('Courier');
     $methodName = $method->getName();
     $markups = \XLite\Core\Database::getRepo('XLite\\Model\\Shipping\\Markup')->findAll();
     $this->assertTrue(0 < count($markups), 'markups list length checking');
     $markup = array_shift($markups);
     $markupId = $markup->getMarkupId();
     unset($markups);
     $extraData = new \XLite\Core\CommonCell();
     $extraData->testparam1 = 'test value 1';
     $extraData->testparam2 = 'test value 2';
     $newRate = new \XLite\Model\Shipping\Rate();
     $newRate->setMethod($method);
     $newRate->setBaseRate(100);
     $newRate->setMarkup($markup);
     $newRate->setMarkupRate(200);
     $newRate->setExtraData($extraData);
     // Check all parameters
     $this->assertEquals($markupId, $newRate->getMarkup()->getMarkupId(), 'Markup wrong');
     $this->assertEquals(100, $newRate->getBaseRate(), 'Base rate wrong');
     $this->assertEquals(200, $newRate->getMarkupRate(), 'Markup rate wrong');
     $this->assertEquals(300, $newRate->getTotalRate(), 'Total rate wrong');
     $this->assertEquals('test value 1', $newRate->getExtraData()->testparam1, 'Extra data #1 wrong');
     $this->assertEquals('test value 2', $newRate->getExtraData()->testparam2, 'Extra data #2 wrong');
     $this->assertEquals($methodName, $newRate->getMethodName(), 'getMethodName() returned wrong value');
 }
Ejemplo n.º 2
0
 /**
  * Performs request to USPS server and returns array of rates
  *
  * @param array   $data        Array of request parameters
  * @param boolean $ignoreCache Flag: if true then do not get rates from cache
  *
  * @return array
  */
 protected function doQuery($data, $ignoreCache)
 {
     $result = null;
     $rates = array();
     $availableMethods = \XLite\Core\Database::getRepo('XLite\\Model\\Shipping\\Method')->findMethodsByProcessor($this->getProcessorId());
     if ($availableMethods) {
         $xmlData = $this->getXMLData($data);
         $currencyRate = doubleval(\XLite\Core\Config::getInstance()->CDev->USPS->currency_rate);
         $currencyRate = 0 < $currencyRate ? $currencyRate : 1;
         $postURL = $this->getApiURL() . '?API=' . $this->getApiName() . '&XML=' . urlencode(preg_replace('/>(\\s+)</', '><', $xmlData));
         try {
             if (!$ignoreCache) {
                 $cachedRate = $this->getDataFromCache($postURL);
             }
             if (isset($cachedRate)) {
                 // Get rates from cache
                 $result = $cachedRate;
             } elseif (\XLite\Model\Shipping::isIgnoreLongCalculations()) {
                 // Ignore rates calculation
                 return array();
             } else {
                 // Calculate rate
                 $bouncer = new \XLite\Core\HTTP\Request($postURL);
                 $bouncer->requestTimeout = 5;
                 $response = $bouncer->sendRequest();
                 if ($response && 200 == $response->code) {
                     $result = $response->body;
                     $this->saveDataInCache($postURL, $result);
                     if (\XLite\Core\Config::getInstance()->CDev->USPS->debug_enabled) {
                         \XLite\Logger::logCustom('USPS', var_export(array('Request URL' => $postURL, 'Request XML' => $xmlData, 'Response' => \XLite\Core\XML::getInstance()->getFormattedXML($result)), true));
                     }
                 } else {
                     $this->errorMsg = sprintf('Error while connecting to the USPS host (%s)', $this->getApiURL());
                 }
             }
             $response = isset($this->errorMsg) ? array() : $this->parseResponse($result);
             $this->apiCommunicationLog[] = array('request' => $postURL, 'xml' => htmlentities(preg_replace('/(USERID=")([^"]+)/', '\\1***', $xmlData)), 'response' => htmlentities(\XLite\Core\XML::getInstance()->getFormattedXML($result)));
             if (!isset($this->errorMsg) && !isset($response['err_msg']) && !empty($response['postage'])) {
                 foreach ($response['postage'] as $postage) {
                     $rate = new \XLite\Model\Shipping\Rate();
                     $method = $this->getShippingMethod($postage['CLASSID'], $availableMethods);
                     if (!isset($method)) {
                         // Unknown method received: add this to the database with disabled status
                         $method = $this->addShippingMethod($postage);
                     }
                     if ($method && $method->getEnabled()) {
                         // Method is registered and enabled
                         $rate->setMethod($method);
                         $codPrice = 0;
                         $rateValue = doubleval($postage['Rate']);
                         if (!$this->isStaticCODPrice() && isset($postage['SpecialServices'])) {
                             if (isset($postage['SpecialServices'][6]) && 'true' == $postage['SpecialServices'][6]['Available']) {
                                 // Shipping service supports COD
                                 $extraData = new \XLite\Core\CommonCell();
                                 $extraData->cod_supported = true;
                                 $extraData->cod_rate = ($rateValue + doubleval($postage['SpecialServices'][6]['Price'])) * $currencyRate;
                                 $rate->setExtraData($extraData);
                                 if ($data['cod_enabled']) {
                                     // Calculate COD fee if COD payment method is selected
                                     $codPrice = doubleval($postage['SpecialServices'][6]['Price']);
                                 }
                             }
                         } elseif ($this->isStaticCODPrice() && $this->isMethodSupportCOD($method)) {
                             $codStaticPrice = doubleval(\XLite\Core\Config::getInstance()->CDev->USPS->cod_price);
                             if (0 < $codStaticPrice) {
                                 // Shipping service supports COD
                                 $extraData = new \XLite\Core\CommonCell();
                                 $extraData->cod_supported = true;
                                 $extraData->cod_rate = ($rateValue + $codStaticPrice) * $currencyRate;
                                 $rate->setExtraData($extraData);
                                 if ($data['cod_enabled']) {
                                     // Calculate COD fee if COD payment method is selected
                                     $codPrice = $codStaticPrice;
                                 }
                             }
                         }
                         // Base rate is a sum of base rate and COD fee
                         $rate->setBaseRate(($rateValue + $codPrice) * $currencyRate);
                         if (isset($rates[$postage['MailService']])) {
                             // Multipackaging: sum base rate and COD fee for each rated packages
                             $rates[$postage['MailService']]->setBaseRate($rates[$postage['MailService']]->getBaseRate() + $rate->getBaseRate());
                             if ($rate->getExtraData()->cod_rate) {
                                 $extra = $rates[$postage['MailService']]->getExtraData();
                                 $extra->cod_rate = $extra->cod_rate + $rate->getExtraData()->cod_rate;
                                 $rates[$postage['MailService']]->setExtraData($extra);
                             }
                         } else {
                             $rates[$postage['MailService']] = $rate;
                         }
                     }
                 }
             } elseif (!isset($this->errorMsg)) {
                 $this->errorMsg = isset($response['err_msg']) ? $response['err_msg'] : 'Unknown error';
             }
         } catch (\Exception $e) {
             $this->errorMsg = 'Exception: ' . $e->getMessage();
         }
     }
     return $rates;
 }