Example #1
0
 /**
  * Calculate available shipping amounts and taxes
  */
 protected function _responseMerchantCalculationCallback()
 {
     $merchantCalculations = new GoogleMerchantCalculations($this->getCurrency());
     $quote = $this->_loadQuote();
     $billingAddress = $quote->getBillingAddress();
     $address = $quote->getShippingAddress();
     $googleAddress = $this->getData('root/calculate/addresses/anonymous-address');
     $googleAddresses = array();
     if (isset($googleAddress['id'])) {
         $googleAddresses[] = $googleAddress;
     } else {
         $googleAddresses = $googleAddress;
     }
     $methods = Mage::getStoreConfig('google/checkout_shipping_merchant/allowed_methods', $this->getStoreId());
     $methods = unserialize($methods);
     $limitCarrier = array();
     foreach ($methods['method'] as $method) {
         if ($method) {
             list($carrierCode, $methodCode) = explode('/', $method);
             $limitCarrier[$carrierCode] = $carrierCode;
         }
     }
     $limitCarrier = array_values($limitCarrier);
     foreach ($googleAddresses as $googleAddress) {
         $addressId = $googleAddress['id'];
         $regionCode = $googleAddress['region']['VALUE'];
         $countryCode = $googleAddress['country-code']['VALUE'];
         $regionModel = Mage::getModel('directory/region')->loadByCode($regionCode, $countryCode);
         $regionId = $regionModel->getId();
         $address->setCountryId($countryCode)->setRegion($regionCode)->setRegionId($regionId)->setCity($googleAddress['city']['VALUE'])->setPostcode($googleAddress['postal-code']['VALUE'])->setLimitCarrier($limitCarrier);
         $billingAddress->setCountryId($countryCode)->setRegion($regionCode)->setRegionId($regionId)->setCity($googleAddress['city']['VALUE'])->setPostcode($googleAddress['postal-code']['VALUE'])->setLimitCarrier($limitCarrier);
         $billingAddress->collectTotals();
         $shippingTaxClass = $this->_getTaxClassForShipping($quote);
         $gRequestMethods = $this->getData('root/calculate/shipping/method');
         if ($gRequestMethods) {
             // Make stable format of $gRequestMethods for convenient usage
             if (array_key_exists('VALUE', $gRequestMethods)) {
                 $gRequestMethods = array($gRequestMethods);
             }
             // Form list of mapping Google method names to applicable address rates
             $rates = array();
             $address->setCollectShippingRates(true)->collectShippingRates();
             foreach ($address->getAllShippingRates() as $rate) {
                 if ($rate instanceof Mage_Shipping_Model_Rate_Result_Error) {
                     continue;
                 }
                 $methodName = sprintf('%s - %s', $rate->getCarrierTitle(), $rate->getMethodTitle());
                 $rates[$methodName] = $rate;
             }
             foreach ($gRequestMethods as $method) {
                 $result = new GoogleResult($addressId);
                 $methodName = $method['name'];
                 if (isset($rates[$methodName])) {
                     $rate = $rates[$methodName];
                     $address->setShippingMethod($rate->getCode())->setLimitCarrier($rate->getCarrier())->setCollectShippingRates(true)->collectTotals();
                     $shippingRate = $address->getBaseShippingAmount() - $address->getBaseShippingDiscountAmount();
                     $result->SetShippingDetails($methodName, $shippingRate, 'true');
                     if ($this->getData('root/calculate/tax/VALUE') == 'true') {
                         $taxAmount = $address->getBaseTaxAmount();
                         $taxAmount += $billingAddress->getBaseTaxAmount();
                         $result->setTaxDetails($taxAmount);
                     }
                 } else {
                     if ($shippingTaxClass && $this->getData('root/calculate/tax/VALUE') == 'true') {
                         $i = 1;
                         $price = Mage::getStoreConfig('google/checkout_shipping_flatrate/price_' . $i, $quote->getStoreId());
                         $price = number_format($price, 2, '.', '');
                         $price = (double) Mage::helper('tax')->getShippingPrice($price, false, false);
                         $address->setShippingMethod(null);
                         $address->setCollectShippingRates(true)->collectTotals();
                         $billingAddress->setCollectShippingRates(true)->collectTotals();
                         $address->setBaseShippingAmount($price);
                         $address->setShippingAmount($this->_reCalculateToStoreCurrency($price, $quote));
                         $this->_applyShippingTaxClass($address, $shippingTaxClass);
                         $taxAmount = $address->getBaseTaxAmount();
                         $taxAmount += $billingAddress->getBaseTaxAmount();
                         $result->SetShippingDetails($methodName, $price - $address->getBaseShippingDiscountAmount(), 'true');
                         $result->setTaxDetails($taxAmount);
                         $i++;
                     } else {
                         $result->SetShippingDetails($methodName, 0, 'false');
                     }
                 }
                 $merchantCalculations->AddResult($result);
             }
         } else {
             if ($this->getData('root/calculate/tax/VALUE') == 'true') {
                 $address->setShippingMethod(null);
                 $address->setCollectShippingRates(true)->collectTotals();
                 $billingAddress->setCollectShippingRates(true)->collectTotals();
                 if (!Mage::helper('googlecheckout')->isShippingCarrierActive($this->getStoreId())) {
                     $this->_applyShippingTaxClass($address, $shippingTaxClass);
                 }
                 $taxAmount = $address->getBaseTaxAmount();
                 $taxAmount += $billingAddress->getBaseTaxAmount();
                 $result = new GoogleResult($addressId);
                 $result->setTaxDetails($taxAmount);
                 $merchantCalculations->addResult($result);
             }
         }
     }
     $this->getGResponse()->ProcessMerchantCalculations($merchantCalculations);
 }
Example #2
0
 /**
  * Calculate available shipping amounts and taxes
  */
 protected function _responseMerchantCalculationCallback()
 {
     $merchantCalculations = new GoogleMerchantCalculations($this->getCurrency());
     $quoteId = $this->getData('root/shopping-cart/merchant-private-data/quote-id/VALUE');
     $storeId = $this->getData('root/shopping-cart/merchant-private-data/store-id/VALUE');
     $quote = Mage::getModel('sales/quote')->setStoreId($storeId)->load($quoteId);
     $billingAddress = $quote->getBillingAddress();
     $address = $quote->getShippingAddress();
     $googleAddress = $this->getData('root/calculate/addresses/anonymous-address');
     $googleAddresses = array();
     if (isset($googleAddress['id'])) {
         $googleAddresses[] = $googleAddress;
     } else {
         $googleAddresses = $googleAddress;
     }
     $methods = Mage::getStoreConfig('google/checkout_shipping_merchant/allowed_methods', $this->getStoreId());
     $methods = unserialize($methods);
     $limitCarrier = array();
     foreach ($methods['method'] as $method) {
         if ($method) {
             list($carrierCode, $methodCode) = explode('/', $method);
             $limitCarrier[] = $carrierCode;
         }
     }
     foreach ($googleAddresses as $googleAddress) {
         $addressId = $googleAddress['id'];
         $regionCode = $googleAddress['region']['VALUE'];
         $countryCode = $googleAddress['country-code']['VALUE'];
         $regionModel = Mage::getModel('directory/region')->loadByCode($regionCode, $countryCode);
         $regionId = $regionModel->getId();
         $address->setCountryId($countryCode)->setRegion($regionCode)->setRegionId($regionId)->setCity($googleAddress['city']['VALUE'])->setPostcode($googleAddress['postal-code']['VALUE'])->setLimitCarrier($limitCarrier);
         $billingAddress->setCountryId($countryCode)->setRegion($regionCode)->setRegionId($regionId)->setCity($googleAddress['city']['VALUE'])->setPostcode($googleAddress['postal-code']['VALUE'])->setLimitCarrier($limitCarrier);
         $address->setCollectShippingRates(true)->collectShippingRates()->collectTotals();
         $billingAddress->collectTotals();
         if ($gRequestMethods = $this->getData('root/calculate/shipping/method')) {
             $carriers = array();
             $errors = array();
             foreach (Mage::getStoreConfig('carriers', $storeId) as $carrierCode => $carrierConfig) {
                 if (!isset($carrierConfig['title'])) {
                     continue;
                 }
                 $title = $carrierConfig['title'];
                 foreach ($gRequestMethods as $method) {
                     $methodName = is_array($method) ? $method['name'] : $method;
                     if ($title && $method && strpos($methodName, $title) === 0) {
                         $carriers[$carrierCode] = $title;
                         $errors[$title] = true;
                     }
                 }
             }
             $result = Mage::getModel('shipping/shipping')->collectRatesByAddress($address, array_keys($carriers))->getResult();
             $rates = array();
             $rateCodes = array();
             foreach ($result->getAllRates() as $rate) {
                 if ($rate instanceof Mage_Shipping_Model_Rate_Result_Error) {
                     $errors[$rate->getCarrierTitle()] = 1;
                 } else {
                     $k = $rate->getCarrierTitle() . ' - ' . $rate->getMethodTitle();
                     if ($address->getFreeShipping()) {
                         $price = 0;
                     } else {
                         $price = $rate->getPrice();
                     }
                     if ($price) {
                         $price = Mage::helper('tax')->getShippingPrice($price, false, $address, null, $storeId);
                     }
                     $rates[$k] = $price;
                     $rateCodes[$k] = $rate->getCarrier() . '_' . $rate->getMethod();
                     unset($errors[$rate->getCarrierTitle()]);
                 }
             }
             foreach ($gRequestMethods as $method) {
                 $methodName = is_array($method) ? $method['name'] : $method;
                 $result = new GoogleResult($addressId);
                 if (!empty($errors)) {
                     $continue = false;
                     foreach ($errors as $carrier => $dummy) {
                         if (strpos($methodName, $carrier) === 0) {
                             $result->SetShippingDetails($methodName, 0, "false");
                             $merchantCalculations->AddResult($result);
                             $continue = true;
                             break;
                         }
                     }
                     if ($continue) {
                         continue;
                     }
                 }
                 if (isset($rates[$methodName])) {
                     if ($this->getData('root/calculate/tax/VALUE') == 'true') {
                         $address->setShippingMethod($rateCodes[$methodName]);
                         $address->collectTotals();
                         $taxAmount = $address->getBaseTaxAmount();
                         $taxAmount += $billingAddress->getBaseTaxAmount();
                         $result->setTaxDetails($taxAmount);
                     }
                     $result->SetShippingDetails($methodName, $rates[$methodName], "true");
                     $merchantCalculations->AddResult($result);
                 }
             }
         } elseif ($this->getData('root/calculate/tax/VALUE') == 'true') {
             $address->setShippingMethod(null);
             $address->setCollectShippingRates(true)->collectTotals();
             $billingAddress->setCollectShippingRates(true)->collectTotals();
             $taxAmount = $address->getBaseTaxAmount();
             $taxAmount += $billingAddress->getBaseTaxAmount();
             $result = new GoogleResult($addressId);
             $result->setTaxDetails($taxAmount);
             $merchantCalculations->addResult($result);
         }
     }
     $this->getGResponse()->ProcessMerchantCalculations($merchantCalculations);
 }
Example #3
0
 protected function _responseMerchantCalculationCallback()
 {
     $quoteId = $this->getData('root/shopping-cart/merchant-private-data/quote-id/VALUE');
     $quote = Mage::getModel('sales/quote')->load($quoteId);
     $address = $quote->getShippingAddress();
     $googleAddress = $this->getData('root/calculate/addresses/anonymous-address');
     $addressId = $googleAddress['id'];
     $address->setCountryId($googleAddress['country-code']['VALUE'])->setRegion($googleAddress['region']['VALUE'])->setCity($googleAddress['city']['VALUE'])->setPostcode($googleAddress['postal-code']['VALUE']);
     $merchantCalculations = new GoogleMerchantCalculations($this->getCurrency());
     if ($gRequestMethods = $this->getData('root/calculate/shipping/method')) {
         $carriers = array();
         foreach (Mage::getStoreConfig('carriers') as $carrierCode => $carrierConfig) {
             foreach ($gRequestMethods as $method) {
                 $title = (string) $carrierConfig->title;
                 if ($title && strpos($method['name'], $title) === 0) {
                     $carriers[$carrierCode] = $title;
                 }
             }
         }
         $result = Mage::getModel('shipping/shipping')->collectRatesByAddress($address, array_keys($carriers))->getResult();
         $errors = array();
         $rates = array();
         foreach ($result->getAllRates() as $rate) {
             if ($rate instanceof Mage_Shipping_Model_Rate_Result_Error) {
                 $errors[$rate->getCarrierTitle()] = 1;
             } else {
                 $rates[$rate->getCarrierTitle() . ' - ' . $rate->getMethodTitle()] = $rate->getPrice();
             }
         }
         foreach ($gRequestMethods as $method) {
             $methodName = $method['name'];
             $result = new GoogleResult($addressId);
             if (!empty($errors)) {
                 $continue = false;
                 foreach ($errors as $carrier => $dummy) {
                     if (strpos($methodName, $carrier) === 0) {
                         $result->SetShippingDetails($methodName, 0, "false");
                         $merchantCalculations->AddResult($result);
                         $continue = true;
                         break;
                     }
                 }
                 if ($continue) {
                     continue;
                 }
             }
             if (!empty($rates[$methodName])) {
                 $result->SetShippingDetails($methodName, $rates[$methodName], "true");
                 $merchantCalculations->AddResult($result);
             }
         }
     }
     if ($this->getData('root/calculate/tax/VALUE') == 'true') {
         $address->setCollectShippingRates(false)->collectTotals();
         $taxAmount = $address->getTaxAmount();
         $result = new GoogleResult($addressId);
         $result->setTaxDetails($taxAmount);
         $merchantCalculations->addResult($result);
     }
     $this->getGResponse()->ProcessMerchantCalculations($merchantCalculations);
 }