/** * @param Mage_Sales_Model_Quote $quote * @return Mage_Sales_Model_Order * @throws Exception */ public function createNewOrder($quote) { $convert = Mage::getModel('sales/convert_quote'); if ($quote->isVirtual()) { $this->setOrder($convert->addressToOrder($quote->getBillingAddress())); } else { $this->setOrder($convert->addressToOrder($quote->getShippingAddress())); } $this->getOrder()->setBillingAddress($convert->addressToOrderAddress($quote->getBillingAddress())); if ($quote->getBillingAddress()->getCustomerAddress()) { $this->getOrder()->getBillingAddress()->setCustomerAddress($quote->getBillingAddress()->getCustomerAddress()); } if (!$quote->isVirtual()) { $this->getOrder()->setShippingAddress($convert->addressToOrderAddress($quote->getShippingAddress())); if ($quote->getShippingAddress()->getCustomerAddress()) { $this->getOrder()->getShippingAddress()->setCustomerAddress($quote->getShippingAddress()->getCustomerAddress()); } } $this->getOrder()->setPayment($convert->paymentToOrderPayment($quote->getPayment())); $this->getOrder()->getPayment()->setTransactionId($quote->getPayment()->getTransactionId()); foreach ($quote->getAllItems() as $item) { /** @var Mage_Sales_Model_Order_Item $item */ $orderItem = $convert->itemToOrderItem($item); if ($item->getParentItem()) { $orderItem->setParentItem($this->getOrder()->getItemByQuoteItemId($item->getParentItem()->getId())); } $this->getOrder()->addItem($orderItem); } $this->getOrder()->setQuote($quote); $this->getOrder()->setExtOrderId($quote->getPayment()->getTransactionId()); $this->getOrder()->setCanSendNewEmailFlag(false); $this->_initTransaction($quote); return $this->getOrder(); }
/** * @param Mage_Sales_Model_Quote $quote * * @return array[] */ public function validateQuote(Mage_Sales_Model_Quote $quote) { $errors = []; if (!$quote->isVirtual()) { // Copy data from billing address if ($quote->getShippingAddress()->getSameAsBilling()) { $quote->getShippingAddress()->importCustomerAddress($quote->getBillingAddress()->exportCustomerAddress()); $quote->getShippingAddress()->setSameAsBilling(1); } $addressErrors = $this->validateQuoteAddress($quote->getShippingAddress()); if (!empty($addressErrors)) { $errors['shipping_address'] = $addressErrors; } $method = $quote->getShippingAddress()->getShippingMethod(); $rate = $quote->getShippingAddress()->getShippingRateByCode($method); if (!$method || !$rate) { $errors['shipping_method'] = [$this->__('Please specify a valid shipping method.')]; } } $addressErrors = $this->validateQuoteAddress($quote->getBillingAddress()); if (!empty($addressErrors)) { $errors['billing_address'] = $addressErrors; } try { if (!$quote->getPayment()->getMethod() || !$quote->getPayment()->getMethodInstance()) { $errors['payment'] = [$this->__('Please select a valid payment method.')]; } } catch (Mage_Core_Exception $e) { $errors['payment'] = [$this->__('Please select a valid payment method.')]; } return $errors; }
/** * Convert the resource model collection to an array * * @param Mage_Sales_Model_Quote $quote * * @return array */ public function prepareCollection(Mage_Sales_Model_Quote $quote, $applyCollectionModifiers = true) { // This collection should always be a key/value hash and never a simple array $data = new ArrayObject(); if ($quote->isVirtual()) { return $data; } // Store current state $actionType = $this->getActionType(); $operation = $this->getOperation(); // Change state $this->setActionType(self::ACTION_TYPE_COLLECTION); $this->setOperation(self::OPERATION_RETRIEVE); // Get filter $filter = $this->getFilter(); // Prepare collection foreach ($this->getCrosssellProducts($quote, $applyCollectionModifiers) as $product) { $data[$product->getSku()] = $this->prepareProduct($product, $filter); } // Restore old state $this->setActionType($actionType); $this->setOperation($operation); // Return prepared outbound data return $data; }
/** * collect reward points that customer earned (per each item and address) total * * @param Mage_Sales_Model_Quote_Address $address * @param Mage_Sales_Model_Quote $quote * @return Magestore_RewardPoints_Model_Total_Quote_Point */ public function collect($address, $quote) { if (!Mage::helper('rewardpoints')->isEnable($quote->getStoreId())) { return $this; } // get points that customer can earned by Rates if ($quote->isVirtual()) { $address = $quote->getBillingAddress(); } else { $address = $quote->getShippingAddress(); } $baseGrandTotal = $quote->getBaseGrandTotal(); if (!Mage::getStoreConfigFlag(Magestore_RewardPoints_Helper_Calculation_Earning::XML_PATH_EARNING_BY_SHIPPING, $quote->getStoreId())) { $baseGrandTotal -= $address->getBaseShippingAmount(); } if (!Mage::getStoreConfigFlag(Magestore_RewardPoints_Helper_Calculation_Earning::XML_PATH_EARNING_BY_TAX, $quote->getStoreId())) { $baseGrandTotal -= $address->getBaseTaxAmount(); } $baseGrandTotal = max(0, $baseGrandTotal); $earningPoints = Mage::helper('rewardpoints/calculation_earning')->getRateEarningPoints($baseGrandTotal, $quote->getStoreId()); if ($earningPoints > 0) { $address->setRewardpointsEarn($earningPoints); } Mage::dispatchEvent('rewardpoints_collect_earning_total_points_before', array('address' => $address)); // Update earning point for each items $this->_updateEarningPoints($address); Mage::dispatchEvent('rewardpoints_collect_earning_total_points_after', array('address' => $address)); return $this; }
/** * Prepare quote customer address information and set the customer on the quote * * @return self */ protected function _prepareCustomerQuote() { $shipping = $this->_quote->isVirtual() ? null : $this->_quote->getShippingAddress(); $customer = $this->_getCustomerSession()->getCustomer(); $customerBilling = $this->_prepareCustomerBilling($customer); if ($shipping) { $customerShipping = $this->_prepareCustomerShipping($customer, $shipping); if ($customerBilling && !$customer->getDefaultShipping() && $shipping->getSameAsBilling()) { $customerBilling->setIsDefaultShipping(true); } elseif (isset($customerShipping) && !$customer->getDefaultShipping()) { $customerShipping->setIsDefaultShipping(true); } } $this->_quote->setCustomer($customer); return $this; }
/** * Specify quote payment method * * @param array $data * @return array */ public function savePayment($data) { if ($this->_quote->isVirtual()) { $this->_quote->getBillingAddress()->setPaymentMethod($this->_methodType); } else { $this->_quote->getShippingAddress()->setPaymentMethod($this->_methodType); } $payment = $this->_quote->getPayment(); $data['method'] = $this->_methodType; $payment->importData($data); $email = isset($data['payer']) ? $data['payer'] : null; $payment->setAdditionalInformation(self::PAYMENT_INFO_PAYER_EMAIL, $email); $payment->setAdditionalInformation(self::PAYMENT_INFO_TRANSACTION_ID, isset($data['transaction_id']) ? $data['transaction_id'] : null); $this->_quote->setCustomerEmail($email); $this->_quote->collectTotals()->save(); return array(); }
/** * Convert the resource model collection to an array * * @param Mage_Sales_Model_Quote $quote * * @return array */ public function prepareCollection(Mage_Sales_Model_Quote $quote) { if ($quote->isVirtual()) { return []; } // Store current state $actionType = $this->getActionType(); $operation = $this->getOperation(); // Change state $this->setActionType(self::ACTION_TYPE_COLLECTION); $this->setOperation(self::OPERATION_RETRIEVE); $data = []; // Load and prep shipping address $address = $quote->getShippingAddress(); $address->setCollectShippingRates(true); $address->collectShippingRates(); $address->save(); // Load rates /** @var Mage_Sales_Model_Resource_Quote_Address_Rate_Collection $rateCollection */ $rateCollection = $address->getShippingRatesCollection(); $rates = []; foreach ($rateCollection as $rate) { /** @var Mage_Sales_Model_Quote_Address_Rate $rate */ if (!$rate->isDeleted() && $rate->getCarrierInstance()) { $rates[] = $rate; } } uasort($rates, [$this, 'sortRates']); // Get filter $filter = $this->getFilter(); // Prepare rates foreach ($rates as $rate) { /** @var Mage_Sales_Model_Quote_Address_Rate $rate */ $data[] = $this->prepareRate($rate, $filter); } // Restore old state $this->setActionType($actionType); $this->setOperation($operation); // Return prepared outbound data return $data; }
/** * Specify quote payment method * * @param array $data * @return array */ public function savePayment($data) { if ($this->_quote->isVirtual()) { $this->_quote->getBillingAddress()->setPaymentMethod(isset($data['method']) ? $data['method'] : null); } else { $this->_quote->getShippingAddress()->setPaymentMethod(isset($data['method']) ? $data['method'] : null); } // shipping totals may be affected by payment method if (!$this->_quote->isVirtual() && $this->_quote->getShippingAddress()) { $this->_quote->getShippingAddress()->setCollectShippingRates(true); } // $data['checks'] = Mage_Payment_Model_Method_Abstract::CHECK_USE_CHECKOUT // | Mage_Payment_Model_Method_Abstract::CHECK_USE_FOR_COUNTRY // | Mage_Payment_Model_Method_Abstract::CHECK_USE_FOR_CURRENCY // | Mage_Payment_Model_Method_Abstract::CHECK_ORDER_TOTAL_MIN_MAX // | Mage_Payment_Model_Method_Abstract::CHECK_ZERO_TOTAL; $data['checks'] = array(); $payment = $this->_quote->getPayment(); $payment->importData($data); $this->_quote->save(); }
/** * Changed By Adam 06/11/2014: Fix bug hidden tax * pre collect total for quote/address and return quote total * * @param Mage_Sales_Model_Quote $quote * @param null|Mage_Sales_Model_Quote_Address $address * @return float */ public function getQuoteBaseTotal($quote, $address = null) { $cacheKey = 'quote_base_total'; if ($this->hasCache($cacheKey)) { return $this->getCache($cacheKey); } if (is_null($address)) { if ($quote->isVirtual()) { $address = $quote->getBillingAddress(); } else { $address = $quote->getShippingAddress(); } } $baseTotal = 0; foreach ($address->getAllItems() as $item) { if ($item->getParentItemId()) { continue; } if ($item->getHasChildren() && $item->isChildrenCalculated()) { foreach ($item->getChildren() as $child) { $baseTotal += $item->getQty() * ($child->getQty() * $this->_getItemBasePrice($child)) - $child->getBaseDiscountAmount() - $child->getMagestoreBaseDiscount(); } } elseif ($item->getProduct()) { $baseTotal += $item->getQty() * $this->_getItemBasePrice($item) - $item->getBaseDiscountAmount() - $item->getMagestoreBaseDiscount(); } } // if (Mage::getStoreConfig(self::XML_PATH_SPEND_FOR_SHIPPING, $quote->getStoreId())) { // $shippingAmount = $address->getShippingAmountForDiscount(); // if ($shippingAmount !== null) { // $baseShippingAmount = $address->getBaseShippingAmountForDiscount(); // } else { // $baseShippingAmount = $address->getBaseShippingAmount(); // } // $baseTotal += $baseShippingAmount - $address->getBaseShippingDiscountAmount() - $address->getMagestoreBaseDiscountForShipping(); // } $this->saveCache($cacheKey, $baseTotal); return $baseTotal; }
/** * Get disabled steps * @param Mage_Sales_Model_Quote $quote checkout quote * @return array */ public function getDisabledSectionHash($quote) { if (Mage::registry('bDisabledSectionRegistered')) { return Mage::registry('disabledSectionHash'); } $originalCodes = array('shipping', 'shipping_method', 'payment'); if ($quote->isVirtual()) { $originalCodes = array('payment'); } $disabledSectionHash = array(); foreach ($originalCodes as $stepKey) { if (!Mage::getStoreConfig('aitconfcheckout/' . $stepKey . '/active')) { $needForDisable = true; if ($stepKey == 'shipping_method') { $needForDisable = $this->_getShippingMethods(); } elseif ($stepKey == 'payment') { $needForDisable = $this->_getPaymentMethods($quote); } if ($needForDisable) { $disabledSectionHash[] = $stepKey; } } } /* {#AITOC_COMMENT_END#} $iStoreId = Mage::app()->getStore()->getId(); $iSiteId = Mage::app()->getWebsite()->getId(); $performer = Aitoc_Aitsys_Abstract_Service::get()->platform()->getModule('Aitoc_Aitconfcheckout')->getLicense()->getPerformer(); $ruler = $performer->getRuler(); if (!($ruler->checkRule('store', $iStoreId, 'store') || $ruler->checkRule('store', $iSiteId, 'website'))) { $disabledSectionHash = array(); } {#AITOC_COMMENT_START#} */ Mage::register('disabledSectionHash', $disabledSectionHash); Mage::register('bDisabledSectionRegistered', true); return $disabledSectionHash; }
function CreateMagentoShopRequest(Mage_Sales_Model_Quote $quote) { $request = new Byjuno_Cdp_Helper_Api_Classes_ByjunoRequest(); $request->setClientId(Mage::getStoreConfig('byjuno/api/clientid', Mage::app()->getStore())); $request->setUserID(Mage::getStoreConfig('byjuno/api/userid', Mage::app()->getStore())); $request->setPassword(Mage::getStoreConfig('byjuno/api/password', Mage::app()->getStore())); $request->setVersion("1.00"); try { $request->setRequestEmail(Mage::getStoreConfig('byjuno/api/mail', Mage::app()->getStore())); } catch (Exception $e) { } $b = $quote->getCustomerDob(); if (!empty($b)) { $request->setDateOfBirth(Mage::getModel('core/date')->date('Y-m-d', strtotime($b))); } Mage::getSingleton('checkout/session')->setData('dob_amasty', ''); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && !empty($_POST["billing"]["dob"])) { $request->setDateOfBirth(Mage::getModel('core/date')->date('Y-m-d', strtotime($_POST["billing"]["dob"]))); Mage::getSingleton('checkout/session')->setData('dob_amasty', $_POST["billing"]["dob"]); } $g = $quote->getCustomerGender(); if (!empty($g)) { if ($g == '1') { $request->setGender('1'); } else { if ($g == '2') { $request->setGender('2'); } } } $request->setRequestId(uniqid((string) $quote->getBillingAddress()->getId() . "_")); $reference = $quote->getCustomer()->getId(); if (empty($reference)) { $request->setCustomerReference("guest_" . $quote->getBillingAddress()->getId()); } else { $request->setCustomerReference($quote->getCustomer()->getId()); } if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && !empty($_POST["method"]) && $_POST["method"] == 'guest') { $request->setCustomerReference(uniqid("guest_")); } $request->setFirstName((string) $quote->getBillingAddress()->getFirstname()); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && !empty($_POST["billing"]["firstname"])) { $request->setFirstName((string) $_POST["billing"]["firstname"]); } $request->setLastName((string) $quote->getBillingAddress()->getLastname()); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && !empty($_POST["billing"]["lastname"])) { $request->setLastName((string) $_POST["billing"]["lastname"]); } $request->setFirstLine(trim((string) $quote->getBillingAddress()->getStreetFull())); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && !empty($_POST["billing"]["street"])) { $street = ''; if (!empty($_POST["billing"]["street"][0])) { $street .= $_POST["billing"]["street"][0]; } if (!empty($_POST["billing"]["street"][1])) { $street .= $_POST["billing"]["street"][1]; } $request->setFirstLine((string) trim($street)); } $request->setCountryCode(strtoupper((string) $quote->getBillingAddress()->getCountry())); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && !empty($_POST["billing"]["country_id"])) { $request->setCountryCode((string) $_POST["billing"]["country_id"]); } $request->setPostCode((string) $quote->getBillingAddress()->getPostcode()); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && !empty($_POST["billing"]["postcode"])) { $request->setPostCode($_POST["billing"]["postcode"]); } $request->setTown((string) $quote->getBillingAddress()->getCity()); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && !empty($_POST["billing"]["city"])) { $request->setTown($_POST["billing"]["city"]); } $request->setFax((string) trim($quote->getBillingAddress()->getFax(), '-')); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && !empty($_POST["billing"]["fax"])) { $request->setFax(trim($_POST["billing"]["fax"], '-')); } $request->setLanguage((string) substr(Mage::app()->getLocale()->getLocaleCode(), 0, 2)); if ($quote->getBillingAddress()->getCompany()) { $request->setCompanyName1($quote->getBillingAddress()->getCompany()); } if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && !empty($_POST["billing"]["company"])) { $request->setCompanyName1(trim($_POST["billing"]["company"], '-')); } $request->setTelephonePrivate((string) trim($quote->getBillingAddress()->getTelephone(), '-')); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && !empty($_POST["billing"]["telephone"])) { $request->setTelephonePrivate(trim($_POST["billing"]["telephone"], '-')); } $request->setEmail((string) $quote->getBillingAddress()->getEmail()); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && !empty($_POST["billing"]["email"])) { $request->setEmail((string) $_POST["billing"]["email"]); } Mage::getSingleton('checkout/session')->setData('gender_amasty', ''); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty') { $g = isset($_POST["billing"]["gender"]) ? $_POST["billing"]["gender"] : ''; $request->setGender($g); Mage::getSingleton('checkout/session')->setData('gender_amasty', $g); } if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && !empty($_POST["billing"]["prefix"])) { if (strtolower($_POST["billing"]["prefix"]) == 'herr') { $request->setGender('1'); Mage::getSingleton('checkout/session')->setData('gender_amasty', '1'); } else { if (strtolower($_POST["billing"]["prefix"]) == 'frau') { $request->setGender('2'); Mage::getSingleton('checkout/session')->setData('gender_amasty', '2'); } } } $extraInfo["Name"] = 'ORDERCLOSED'; $extraInfo["Value"] = 'NO'; $request->setExtraInfo($extraInfo); $extraInfo["Name"] = 'ORDERAMOUNT'; $extraInfo["Value"] = number_format($quote->getGrandTotal(), 2, '.', ''); $request->setExtraInfo($extraInfo); $extraInfo["Name"] = 'ORDERCURRENCY'; $extraInfo["Value"] = $quote->getBaseCurrencyCode(); $request->setExtraInfo($extraInfo); $extraInfo["Name"] = 'IP'; $extraInfo["Value"] = $this->getClientIp(); $request->setExtraInfo($extraInfo); $sesId = Mage::getSingleton('checkout/session')->getData("byjuno_session_id"); if (Mage::getStoreConfig('byjuno/api/tmxenabled', Mage::app()->getStore()) == 'enable' && !empty($sesId)) { $extraInfo["Name"] = 'DEVICE_FINGERPRINT_ID'; $extraInfo["Value"] = Mage::getSingleton('checkout/session')->getData("byjuno_session_id"); $request->setExtraInfo($extraInfo); } /* shipping information */ if (!$quote->isVirtual()) { $extraInfo["Name"] = 'DELIVERY_FIRSTNAME'; $extraInfo["Value"] = $quote->getShippingAddress()->getFirstname(); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && empty($_POST["shipping"]["same_as_billing"])) { if (!empty($_POST["shipping"]["firstname"])) { $extraInfo["Value"] = $_POST["shipping"]["firstname"]; } } $request->setExtraInfo($extraInfo); $extraInfo["Name"] = 'DELIVERY_LASTNAME'; $extraInfo["Value"] = $quote->getShippingAddress()->getLastname(); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && empty($_POST["shipping"]["same_as_billing"])) { if (!empty($_POST["shipping"]["lastname"])) { $extraInfo["Value"] = $_POST["shipping"]["lastname"]; } } $request->setExtraInfo($extraInfo); $extraInfo["Name"] = 'DELIVERY_FIRSTLINE'; $extraInfo["Value"] = trim($quote->getShippingAddress()->getStreetFull()); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && empty($_POST["shipping"]["same_as_billing"])) { $extraInfo["Value"] = ''; if (!empty($_POST["shipping"]["street"][0])) { $extraInfo["Value"] = $_POST["shipping"]["street"][0]; } if (!empty($_POST["shipping"]["street"][1])) { $extraInfo["Value"] .= ' ' . $_POST["shipping"]["street"][1]; } $extraInfo["Value"] = trim($extraInfo["Value"]); } $request->setExtraInfo($extraInfo); $extraInfo["Name"] = 'DELIVERY_HOUSENUMBER'; $extraInfo["Value"] = ''; $request->setExtraInfo($extraInfo); $extraInfo["Name"] = 'DELIVERY_COUNTRYCODE'; $extraInfo["Value"] = strtoupper($quote->getShippingAddress()->getCountry()); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && empty($_POST["shipping"]["same_as_billing"])) { if (!empty($_POST["shipping"]["country_id"])) { $extraInfo["Value"] = $_POST["shipping"]["country_id"]; } } $request->setExtraInfo($extraInfo); $extraInfo["Name"] = 'DELIVERY_POSTCODE'; $extraInfo["Value"] = $quote->getShippingAddress()->getPostcode(); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty' && empty($_POST["shipping"]["same_as_billing"])) { if (!empty($_POST["shipping"]["postcode"])) { $extraInfo["Value"] = $_POST["shipping"]["postcode"]; } } $request->setExtraInfo($extraInfo); $extraInfo["Name"] = 'DELIVERY_TOWN'; $extraInfo["Value"] = $quote->getShippingAddress()->getCity(); if (Mage::getStoreConfig('byjuno/api/plugincheckouttype', Mage::app()->getStore()) == 'amasty') { if (!empty($_POST["shipping"]["same_as_billing"]) && $_POST["shipping"]["same_as_billing"] == '1' && !empty($_POST["billing"]["city"])) { $extraInfo["Value"] = $_POST["billing"]["city"]; } else { if (!empty($_POST["shipping"]["city"])) { $extraInfo["Value"] = $_POST["shipping"]["city"]; } } } $request->setExtraInfo($extraInfo); if ($quote->getShippingAddress()->getCompany() != '' && Mage::getStoreConfig('byjuno/api/businesstobusiness', Mage::app()->getStore()) == 'enable') { $extraInfo["Name"] = 'DELIVERY_COMPANYNAME'; $extraInfo["Value"] = $quote->getShippingAddress()->getCompany(); $request->setExtraInfo($extraInfo); } } $extraInfo["Name"] = 'CONNECTIVTY_MODULE'; $extraInfo["Value"] = 'Byjuno Magento module 4.0.0'; $request->setExtraInfo($extraInfo); return $request; }
/** * Prepare quote for customer order submit * * @param Mage_Sales_Model_Quote $quote * @return Mage_Checkout_Model_Api_Resource_Customer */ protected function _prepareCustomerQuote(Mage_Sales_Model_Quote $quote) { $billing = $quote->getBillingAddress(); $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress(); $customer = $quote->getCustomer(); if (!$billing->getCustomerId() || $billing->getSaveInAddressBook()) { $customerBilling = $billing->exportCustomerAddress(); $customer->addAddress($customerBilling); $billing->setCustomerAddress($customerBilling); } if ($shipping && (!$shipping->getCustomerId() && !$shipping->getSameAsBilling() || !$shipping->getSameAsBilling() && $shipping->getSaveInAddressBook())) { $customerShipping = $shipping->exportCustomerAddress(); $customer->addAddress($customerShipping); $shipping->setCustomerAddress($customerShipping); } if (isset($customerBilling) && !$customer->getDefaultBilling()) { $customerBilling->setIsDefaultBilling(true); } if ($shipping && isset($customerShipping) && !$customer->getDefaultShipping()) { $customerShipping->setIsDefaultShipping(true); } else { if (isset($customerBilling) && !$customer->getDefaultShipping()) { $customerBilling->setIsDefaultShipping(true); } } $quote->setCustomer($customer); return $this; }
/** * BillSAFE does not allow: * - virtual quotes * - differing shipping/billing address * * @param Mage_Sales_Model_Quote $quote * @return bool */ protected function isAllowedBillSafe(Mage_Sales_Model_Quote $quote) { if ($quote->isVirtual()) { return false; } $billingAddress = $quote->getBillingAddress(); $shippingAddress = $quote->getShippingAddress(); if (!$shippingAddress->getSameAsBilling()) { // Double check, in case the customer has chosen to enter a separate shipping address, but filled in the same values as in billing address: if (!$this->helper()->addressesAreEqual($billingAddress, $shippingAddress)) { return false; } } return true; }
/** * checks if an addresscheck must be performed * * @param $addressType * @param Payone_Core_Model_Config_Protect_AddressCheck $config * @param Mage_Sales_Model_Quote $quote * @param $useForShipping * @return bool */ protected function mustCheckAddress($addressType, Payone_Core_Model_Config_Protect_AddressCheck $config, Mage_Sales_Model_Quote $quote, $useForShipping) { // check if address is shipping-address an shipping-address has to be checked if ($addressType === 'shipping' && !$this->_alreadyCheckedAndChangeWasDenied($addressType) && $config->mustCheckShipping()) { return true; } // check if address is billing-address if ($addressType === 'billing' && !$this->_alreadyCheckedAndChangeWasDenied($addressType)) { // check if billing-address has to be checked if ($config->mustCheckBilling()) { return true; } // check if billing-address is used for shipping address and shipping-address has to be checked if ($useForShipping === true and $config->mustCheckShipping() and !$quote->isVirtual()) { return true; } // check if billing-address has to be checked for virtual order if ($quote->isVirtual() and $config->mustCheckBillingForVirtualOrder()) { return true; } } return false; }
/** * @param Mage_Sales_Model_Quote $quote * @return array|bool */ public function getFeeConfigForQuote(Mage_Sales_Model_Quote $quote) { // No handling fee for virtual quotes if ($quote->isVirtual()) { return false; } $shippingAddress = $quote->getShippingAddress(); $country = $shippingAddress->getCountry(); $shippingMethod = $shippingAddress->getShippingMethod(); $feeConfigs = $this->getFeeConfig(); if (!is_array($feeConfigs)) { return false; } foreach ($feeConfigs as $key => $feeConfig) { if (in_array($shippingMethod, $feeConfig['shipping_method']) === false) { unset($feeConfigs[$key]); continue; } if (array_key_exists('countries', $feeConfig) and in_array($country, $feeConfig['countries']) === false) { unset($feeConfigs[$key]); continue; } } if (count($feeConfigs) > 0) { return array_shift($feeConfigs); } else { return false; } }
/** * Gets the configured Buckaroo Payment fee excl. tax for a given quote. * * @param Mage_Sales_Model_Quote $quote * @param string $paymentMethod * * @return float|int */ protected function _getPaymentFee(Mage_Sales_Model_Quote $quote, $paymentMethod = '') { $storeId = $quote->getStoreId(); /** * Get the fee as configured by the merchant. */ if (empty($paymentMethod)) { $paymentMethod = $quote->getPayment()->getMethod(); } if (!$paymentMethod) { return 0; } $fee = Mage::getStoreConfig(sprintf(self::XPATH_BUCKAROO_FEE, $paymentMethod), $storeId); /** * Determine if the configured fee is a percentage or a flat amount. */ if (strpos($fee, '%') !== false) { $this->_feeIsPercentage = true; /** * If the fee is a percentage, get the configured percentage value and determine over which part of the * quote this percentage needs to be calculated. */ $percentage = floatval(trim($fee)); if (!$quote->isVirtual()) { $address = $quote->getShippingAddress(); } else { $address = $quote->getBillingAddress(); } $calculationAmount = false; $feePercentageMode = Mage::getStoreConfig(self::XPATH_BUCKAROO_FEE_PERCENTAGE_MODE, $storeId); switch ($feePercentageMode) { case 'subtotal': $calculationAmount = $address->getBaseSubtotal() - $address->getBaseDiscountAmount(); $this->_feeIsInclTax = false; break; case 'subtotal_incl_tax': $calculationAmount = $address->getBaseSubtotalInclTax() - $address->getBaseDiscountAmount(); $this->_feeIsInclTax = true; break; case 'grandtotal': $calculationAmount = $address->getBaseSubtotalInclTax() + $address->getBaseShippingInclTax() - $address->getBaseDiscountAmount(); $this->_feeIsInclTax = true; break; //no default } /** * Calculate the flat fee. */ if ($calculationAmount !== false && $calculationAmount > 0) { $fee = $calculationAmount * ($percentage / 100); } else { $fee = 0; } } else { $fee = (double) $fee; } if ($fee <= 0) { return 0; } /** * If the fee is entered without tax, return the fee amount. Otherwise, we need to calculate and remove the tax. */ $feeIsIncludingTax = $this->getFeeIsInclTax($storeId); if (!$feeIsIncludingTax) { return $fee; } /** * Build a tax request to calculate the fee tax. */ $taxRequest = $this->_getBuckarooFeeTaxRequest($quote); if (!$taxRequest) { return $fee; } /** * Get the tax rate for the request. */ $taxRate = $this->_getBuckarooFeeTaxRate($taxRequest); if (!$taxRate || $taxRate <= 0) { return $fee; } /** * Remove the tax from the fee. */ $feeTax = $this->_getBuckarooFeeTax($quote->getShippingAddress(), $taxRate, $fee, true); $fee -= $feeTax; return $fee; }
/** * Place the order * * @return bool */ protected function _processOrder() { if (!$this->_auth()) { return false; } if (!$this->_validateQuote()) { return false; } // Push address to the quote, set totals, // Convert quote to the order, // Set rakuten_order attribute to "1" try { // To avoid duplicates look for order with the same Rakuten order no $orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('ext_order_id', (string) $this->_request->order_no); if (count($orders)) { $this->_debugData['reason'] = 'The same order already placed'; return false; } // Import addresses and other data to quote $this->_quote->setIsActive(true)->reserveOrderId(); $storeId = $this->_quote->getStoreId(); Mage::app()->setCurrentStore(Mage::app()->getStore($storeId)); if ($this->_quote->getQuoteCurrencyCode() != $this->_quote->getBaseCurrencyCode()) { Mage::app()->getStore()->setCurrentCurrencyCode($this->_quote->getQuoteCurrencyCode()); } $billing = $this->_convertAddress('client'); $this->_quote->setBillingAddress($billing); $shipping = $this->_convertAddress('delivery_address'); $this->_quote->setShippingAddress($shipping); $this->_convertTotals($this->_quote->getShippingAddress()); $this->_quote->getPayment()->importData(array('method' => 'rakuten')); /** * Convert quote to order * * @var $convertQuote Mage_Sales_Model_Convert_Quote */ $convertQuote = Mage::getSingleton('sales/convert_quote'); /* @var $order Mage_Sales_Model_Order */ $order = $convertQuote->toOrder($this->_quote); if ($this->_quote->isVirtual()) { $convertQuote->addressToOrder($this->_quote->getBillingAddress(), $order); } else { $convertQuote->addressToOrder($this->_quote->getShippingAddress(), $order); } $order->setExtOrderId((string) $this->_request->order_no); $order->setExtCustomerId((string) $this->_request->client->client_id); if (!$order->getCustomerEmail()) { $order->setCustomerEmail($billing->getEmail())->setCustomerPrefix($billing->getPrefix())->setCustomerFirstname($billing->getFirstname())->setCustomerMiddlename($billing->getMiddlename())->setCustomerLastname($billing->getLastname())->setCustomerSuffix($billing->getSuffix())->setCustomerIsGuest(1); } $order->setBillingAddress($convertQuote->addressToOrderAddress($this->_quote->getBillingAddress())); if (!$this->_quote->isVirtual()) { $order->setShippingAddress($convertQuote->addressToOrderAddress($this->_quote->getShippingAddress())); } /** @var $item Mage_Sales_Model_Quote_Item */ foreach ($this->_quote->getAllItems() as $item) { $orderItem = $convertQuote->itemToOrderItem($item); if ($item->getParentItem()) { $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId())); } $order->addItem($orderItem); } /** * Adding transaction for correct transaction information displaying on the order view in the admin. * It has no influence on the API interaction logic. * * @var $payment Mage_Sales_Model_Order_Payment */ $payment = Mage::getModel('sales/order_payment'); $payment->setMethod('rakuten')->setTransactionId((string) $this->_request->order_no)->setIsTransactionClosed(false); $order->setPayment($payment); $payment->addTransaction(Mage_Sales_Model_Order_Payment_Transaction::TYPE_CAPTURE); $order->setCanShipPartiallyItem(false); $message = ''; if (trim((string) $this->_request->comment_client) != '') { $message .= $this->__('Customer\'s Comment: %s', '<strong>' . trim((string) $this->_request->comment_client) . '</strong><br />'); } $message .= $this->__('Rakuten Order No: %s', '<strong>' . (string) $this->_request->order_no . '</strong><br />') . $this->__('Rakuten Client ID: %s', '<strong>' . (string) $this->_request->client->client_id . '</strong><br />'); $order->addStatusHistoryComment($message); $order->setRakutenOrder(1); // Custom attribute for fast filtering of orders placed via Rakuten Checkout $order->place(); $order->save(); // $order->sendNewOrderEmail(); $this->_quote->setIsActive(false)->save(); Mage::dispatchEvent('checkout_submit_all_after', array('order' => $order, 'quote' => $this->_quote)); } catch (Exception $e) { $this->_debugData['exception'] = $e->getMessage(); Mage::logException($e); return false; } return true; }
/** * pre collect total for quote/address and return quote total * * @param Mage_Sales_Model_Quote $quote * @param null|Mage_Sales_Model_Quote_Address $address * @return float */ public function getQuoteBaseTotal($quote, $address = null) { $cacheKey = 'quote_base_total'; if ($this->hasCache($cacheKey)) { return $this->getCache($cacheKey); } if (is_null($address)) { if ($quote->isVirtual()) { $address = $quote->getBillingAddress(); } else { $address = $quote->getShippingAddress(); } } $baseTotal = $address->getBaseSubtotal() + $address->getBaseDiscountAmount() - $this->getPointItemDiscount() - $address->getMagestoreBaseDiscount(); $applyTaxAfterDiscount = (bool) Mage::getStoreConfig('rewardpoints/spending/discount_before_tax', $quote->getStoreId()); if (Mage::getStoreConfig(self::XML_PATH_SPEND_FOR_TAX, $quote->getStoreId()) && !$applyTaxAfterDiscount) { $baseTotal += $address->getBaseTaxAmount(); } if (Mage::getStoreConfig(self::XML_PATH_SPEND_FOR_SHIPPING, $quote->getStoreId())) { $baseTotal += $address->getBaseShippingAmount(); } if (Mage::getStoreConfig(self::XML_PATH_SPEND_FOR_SHIPPING_TAX, $quote->getStoreId()) && !$applyTaxAfterDiscount) { $baseTotal += $address->getBaseShippingTaxAmount(); } $this->saveCache($cacheKey, $baseTotal); return $baseTotal; }
/** * calculate earning point for order quote * * @param Mage_Sales_Model_Quote $quote * @param int $customerGroupId * @param int $websiteId * @param string $date * @return int */ public function getShoppingCartPoints($quote, $customerGroupId = null, $websiteId = null, $date = null) { if ($quote->isVirtual()) { $address = $quote->getBillingAddress(); } else { $address = $quote->getShippingAddress(); } //webpos $customerId = Mage::getSingleton('checkout/session')->getData('webpos_customerid'); if ($customerId) { $customerGroupId = Mage::getModel('customer/customer')->load($customerId)->getGroupId(); } else { $customerGroupId = Mage_Customer_Model_Group::NOT_LOGGED_IN_ID; } if (is_null($websiteId)) { $websiteId = Mage::app()->getStore($quote->getStoreId())->getWebsiteId(); } if (is_null($date)) { $date = date('Y-m-d', strtotime($quote->getCreatedAt())); } $points = 0; $rules = Mage::getResourceModel('rewardpointsrule/earning_sales_collection')->setAvailableFilter($customerGroupId, $websiteId, $date); $items = $quote->getAllItems(); $this->setStoreId($quote->getStoreId()); foreach ($rules as $rule) { $rule->afterLoad(); if (!$rule->validate($address)) { continue; } $rowTotal = 0; $qtyTotal = 0; foreach ($items as $item) { if ($item->getParentItemId()) { continue; } if ($rule->getActions()->validate($item)) { $rowTotal += max(0, $item->getBaseRowTotal() - $item->getBaseDiscountAmount() - $item->getRewardpointsBaseDiscount()); $qtyTotal += $item->getQty(); } } if (!$qtyTotal) { continue; } if (Mage::getStoreConfigFlag(self::XML_PATH_EARNING_BY_SHIPPING, $quote->getStoreId())) { $rowTotal += $address->getBaseShippingAmount(); } $points += $this->calcSalesPoints($rule->getSimpleAction(), $rule->getPointsEarned(), $rule->getMoneyStep(), $rowTotal, $rule->getQtyStep(), $qtyTotal, $rule->getMaxPointsEarned()); if ($points && $rule->getStopRulesProcessing()) { break; } } return $points; }