00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Checkout_Model_Type_Onepage
00029 {
00030
00031
00032
00033
00034
00035 public function getCheckout()
00036 {
00037 return Mage::getSingleton('checkout/session');
00038 }
00039
00040
00041
00042
00043
00044
00045 public function getQuote()
00046 {
00047 return $this->getCheckout()->getQuote();
00048 }
00049
00050
00051
00052
00053
00054
00055 public function initCheckout()
00056 {
00057 $checkout = $this->getCheckout();
00058 if (is_array($checkout->getStepData())) {
00059 foreach ($checkout->getStepData() as $step=>$data) {
00060 if (!($step==='login'
00061 || Mage::getSingleton('customer/session')->isLoggedIn() && $step==='billing')) {
00062 $checkout->setStepData($step, 'allow', false);
00063 }
00064 }
00065 }
00066
00067
00068
00069
00070 $customer = Mage::getSingleton('customer/session')->getCustomer();
00071 if ($customer) {
00072 $this->getQuote()->assignCustomer($customer);
00073 }
00074 if ($this->getQuote()->getIsMultiShipping()) {
00075 $this->getQuote()->setIsMultiShipping(false);
00076 $this->getQuote()->save();
00077 }
00078 return $this;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087 public function saveCheckoutMethod($method)
00088 {
00089 if (empty($method)) {
00090 $res = array(
00091 'error' => -1,
00092 'message' => Mage::helper('checkout')->__('Invalid data')
00093 );
00094 return $res;
00095 }
00096
00097 $this->getQuote()->setCheckoutMethod($method)->save();
00098 $this->getCheckout()->setStepData('billing', 'allow', true);
00099 return array();
00100 }
00101
00102
00103
00104
00105
00106
00107
00108 public function getAddress($addressId)
00109 {
00110 $address = Mage::getModel('customer/address')->load((int)$addressId);
00111 $address->explodeStreetAddress();
00112 if ($address->getRegionId()) {
00113 $address->setRegion($address->getRegionId());
00114 }
00115 return $address;
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125 public function saveBilling($data, $customerAddressId)
00126 {
00127 if (empty($data)) {
00128 $res = array(
00129 'error' => -1,
00130 'message' => Mage::helper('checkout')->__('Invalid data')
00131 );
00132 return $res;
00133 }
00134
00135 $address = $this->getQuote()->getBillingAddress();
00136 if (!empty($customerAddressId)) {
00137 $customerAddress = Mage::getModel('customer/address')->load($customerAddressId);
00138 if ($customerAddress->getId()) {
00139 if ($customerAddress->getCustomerId() != $this->getQuote()->getCustomerId()) {
00140 return array('error' => 1,
00141 'message' => Mage::helper('checkout')->__('Customer Address is not valid.')
00142 );
00143 }
00144 $address->importCustomerAddress($customerAddress);
00145 }
00146 } else {
00147 unset($data['address_id']);
00148 $address->addData($data);
00149
00150 }
00151
00152 if (($validateRes = $address->validate())!==true) {
00153 $res = array(
00154 'error' => 1,
00155 'message' => $validateRes
00156 );
00157 return $res;
00158 }
00159
00160 if (!$this->getQuote()->getCustomerId() && Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER == $this->getQuote()->getCheckoutMethod()) {
00161 if ($this->_customerEmailExists($address->getEmail(), Mage::app()->getWebsite()->getId())) {
00162 return array('error' => 1,
00163 'message' => Mage::helper('checkout')->__('There is already a customer registered using this email address')
00164 );
00165 }
00166 }
00167
00168 $address->implodeStreetAddress();
00169
00170 if (!$this->getQuote()->isVirtual()) {
00171
00172
00173
00174 $usingCase = isset($data['use_for_shipping']) ? (int) $data['use_for_shipping'] : 0;
00175
00176 switch($usingCase) {
00177 case 0:
00178 $shipping = $this->getQuote()->getShippingAddress();
00179 $shipping->setSameAsBilling(0);
00180 break;
00181 case 1:
00182 $billing = clone $address;
00183 $billing->unsAddressId()->unsAddressType();
00184 $shipping = $this->getQuote()->getShippingAddress();
00185 $shippingMethod = $shipping->getShippingMethod();
00186 $shipping->addData($billing->getData())
00187 ->setSameAsBilling(1)
00188 ->setShippingMethod($shippingMethod)
00189 ->setCollectShippingRates(true);
00190 $this->getCheckout()->setStepData('shipping', 'complete', true);
00191 break;
00192 }
00193 }
00194
00195 if (true !== $result = $this->_processValidateCustomer($address)) {
00196 return $result;
00197 }
00198
00199 $this->getQuote()->collectTotals();
00200 $this->getQuote()->save();
00201
00202 $this->getCheckout()
00203 ->setStepData('billing', 'allow', true)
00204 ->setStepData('billing', 'complete', true)
00205 ->setStepData('shipping', 'allow', true);
00206
00207 return array();
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217 protected function _processValidateCustomer(Mage_Sales_Model_Quote_Address $address)
00218 {
00219
00220 $dob = '';
00221 if ($address->getDob()) {
00222 $dob = Mage::app()->getLocale()->date($address->getDob(), null, null, false)->toString('yyyy-MM-dd');
00223 $this->getQuote()->setCustomerDob($dob);
00224 }
00225
00226
00227 if ($address->getTaxvat()) {
00228 $this->getQuote()->setCustomerTaxvat($address->getTaxvat());
00229 }
00230
00231
00232 if (Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER == $this->getQuote()->getCheckoutMethod()) {
00233
00234 $customer = Mage::getModel('customer/customer');
00235 $this->getQuote()->setPasswordHash($customer->encryptPassword($address->getCustomerPassword()));
00236
00237
00238 foreach (array(
00239 'firstname' => 'firstname',
00240 'lastname' => 'lastname',
00241 'email' => 'email',
00242 'password' => 'customer_password',
00243 'confirmation' => 'confirm_password',
00244 'taxvat' => 'taxvat',
00245 ) as $key => $dataKey) {
00246 $customer->setData($key, $address->getData($dataKey));
00247 }
00248 if ($dob) {
00249 $customer->setDob($dob);
00250 }
00251 $validationResult = $customer->validate();
00252 if (true !== $validationResult && is_array($validationResult)) {
00253 return array(
00254 'error' => -1,
00255 'message' => implode(', ', $validationResult)
00256 );
00257 }
00258 } elseif(Mage_Sales_Model_Quote::CHECKOUT_METHOD_GUEST == $this->getQuote()->getCheckoutMethod()) {
00259 $email = $address->getData('email');
00260 if (!Zend_Validate::is($email, 'EmailAddress')) {
00261 return array(
00262 'error' => -1,
00263 'message' => Mage::helper('checkout')->__('Invalid email address "%s"', $email)
00264 );
00265 }
00266 }
00267
00268 return true;
00269 }
00270
00271 public function saveShipping($data, $customerAddressId)
00272 {
00273 if (empty($data)) {
00274 $res = array(
00275 'error' => -1,
00276 'message' => Mage::helper('checkout')->__('Invalid data')
00277 );
00278 return $res;
00279 }
00280 $address = $this->getQuote()->getShippingAddress();
00281
00282 if (!empty($customerAddressId)) {
00283 $customerAddress = Mage::getModel('customer/address')->load($customerAddressId);
00284 if ($customerAddress->getId()) {
00285 if ($customerAddress->getCustomerId() != $this->getQuote()->getCustomerId()) {
00286 return array('error' => 1,
00287 'message' => Mage::helper('checkout')->__('Customer Address is not valid.')
00288 );
00289 }
00290 $address->importCustomerAddress($customerAddress);
00291 }
00292 } else {
00293 unset($data['address_id']);
00294 $address->addData($data);
00295 }
00296 $address->implodeStreetAddress();
00297 $address->setCollectShippingRates(true);
00298
00299 if (($validateRes = $address->validate())!==true) {
00300 $res = array(
00301 'error' => 1,
00302 'message' => $validateRes
00303 );
00304 return $res;
00305 }
00306
00307 $this->getQuote()->collectTotals()->save();
00308
00309 $this->getCheckout()
00310 ->setStepData('shipping', 'complete', true)
00311 ->setStepData('shipping_method', 'allow', true);
00312
00313 return array();
00314 }
00315
00316 public function saveShippingMethod($shippingMethod)
00317 {
00318 if (empty($shippingMethod)) {
00319 $res = array(
00320 'error' => -1,
00321 'message' => Mage::helper('checkout')->__('Invalid shipping method.')
00322 );
00323 return $res;
00324 }
00325 $rate = $this->getQuote()->getShippingAddress()->getShippingRateByCode($shippingMethod);
00326 if (!$rate) {
00327 $res = array(
00328 'error' => -1,
00329 'message' => Mage::helper('checkout')->__('Invalid shipping method.')
00330 );
00331 return $res;
00332 }
00333 $this->getQuote()->getShippingAddress()->setShippingMethod($shippingMethod);
00334 $this->getQuote()->collectTotals()->save();
00335
00336 $this->getCheckout()
00337 ->setStepData('shipping_method', 'complete', true)
00338 ->setStepData('payment', 'allow', true);
00339
00340 return array();
00341 }
00342
00343 public function savePayment($data)
00344 {
00345 if (empty($data)) {
00346 $res = array(
00347 'error' => -1,
00348 'message' => Mage::helper('checkout')->__('Invalid data')
00349 );
00350 return $res;
00351 }
00352 $payment = $this->getQuote()->getPayment();
00353 $payment->importData($data);
00354
00355 $this->getQuote()->getShippingAddress()->setPaymentMethod($payment->getMethod());
00356 $this->getQuote()->collectTotals()->save();
00357
00358 $this->getCheckout()
00359 ->setStepData('payment', 'complete', true)
00360 ->setStepData('review', 'allow', true);
00361
00362 return array();
00363 }
00364
00365 protected function validateOrder()
00366 {
00367 $helper = Mage::helper('checkout');
00368 if ($this->getQuote()->getIsMultiShipping()) {
00369 Mage::throwException($helper->__('Invalid checkout type.'));
00370 }
00371
00372 if (!$this->getQuote()->isVirtual()) {
00373 $address = $this->getQuote()->getShippingAddress();
00374 $addressValidation = $address->validate();
00375 if ($addressValidation !== true) {
00376 Mage::throwException($helper->__('Please check shipping address information.'));
00377 }
00378 $method= $address->getShippingMethod();
00379 $rate = $address->getShippingRateByCode($method);
00380 if (!$this->getQuote()->isVirtual() && (!$method || !$rate)) {
00381 Mage::throwException($helper->__('Please specify shipping method.'));
00382 }
00383 }
00384
00385 $addressValidation = $this->getQuote()->getBillingAddress()->validate();
00386 if ($addressValidation !== true) {
00387 Mage::throwException($helper->__('Please check billing address information.'));
00388 }
00389
00390 if (!($this->getQuote()->getPayment()->getMethod())) {
00391 Mage::throwException($helper->__('Please select valid payment method.'));
00392 }
00393 }
00394
00395
00396
00397
00398
00399
00400 public function saveOrder()
00401 {
00402
00403 $this->validateOrder();
00404 $billing = $this->getQuote()->getBillingAddress();
00405 if (!$this->getQuote()->isVirtual()) {
00406 $shipping = $this->getQuote()->getShippingAddress();
00407 }
00408 switch ($this->getQuote()->getCheckoutMethod()) {
00409 case Mage_Sales_Model_Quote::CHECKOUT_METHOD_GUEST:
00410 if (!$this->getQuote()->isAllowedGuestCheckout()) {
00411 Mage::throwException(Mage::helper('checkout')->__('Sorry, guest checkout is not enabled. Please try again or contact store owner.'));
00412 }
00413 $this->getQuote()->setCustomerId(null)
00414 ->setCustomerEmail($billing->getEmail())
00415 ->setCustomerIsGuest(true)
00416 ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
00417 break;
00418
00419 case Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER:
00420 $customer = Mage::getModel('customer/customer');
00421
00422
00423 $customerBilling = $billing->exportCustomerAddress();
00424 $customer->addAddress($customerBilling);
00425
00426 if (!$this->getQuote()->isVirtual() && !$shipping->getSameAsBilling()) {
00427 $customerShipping = $shipping->exportCustomerAddress();
00428 $customer->addAddress($customerShipping);
00429 }
00430
00431 if ($this->getQuote()->getCustomerDob() && !$billing->getCustomerDob()) {
00432 $billing->setCustomerDob($this->getQuote()->getCustomerDob());
00433 }
00434
00435 if ($this->getQuote()->getCustomerTaxvat() && !$billing->getCustomerTaxvat()) {
00436 $billing->setCustomerTaxvat($this->getQuote()->getCustomerTaxvat());
00437 }
00438
00439 Mage::helper('core')->copyFieldset('checkout_onepage_billing', 'to_customer', $billing, $customer);
00440
00441 $customer->setPassword($customer->decryptPassword($this->getQuote()->getPasswordHash()));
00442 $customer->setPasswordHash($customer->hashPassword($customer->getPassword()));
00443
00444 $this->getQuote()->setCustomer($customer);
00445 break;
00446
00447 default:
00448 $customer = Mage::getSingleton('customer/session')->getCustomer();
00449
00450 if (!$billing->getCustomerId() || $billing->getSaveInAddressBook()) {
00451 $customerBilling = $billing->exportCustomerAddress();
00452 $customer->addAddress($customerBilling);
00453 }
00454 if (!$this->getQuote()->isVirtual() &&
00455 ((!$shipping->getCustomerId() && !$shipping->getSameAsBilling()) ||
00456 (!$shipping->getSameAsBilling() && $shipping->getSaveInAddressBook()))) {
00457
00458 $customerShipping = $shipping->exportCustomerAddress();
00459 $customer->addAddress($customerShipping);
00460 }
00461 $customer->setSavedFromQuote(true);
00462 $customer->save();
00463
00464 $changed = false;
00465 if (isset($customerBilling) && !$customer->getDefaultBilling()) {
00466 $customer->setDefaultBilling($customerBilling->getId());
00467 $changed = true;
00468 }
00469 if (!$this->getQuote()->isVirtual() && isset($customerBilling) && !$customer->getDefaultShipping() && $shipping->getSameAsBilling()) {
00470 $customer->setDefaultShipping($customerBilling->getId());
00471 $changed = true;
00472 }
00473 elseif (!$this->getQuote()->isVirtual() && isset($customerShipping) && !$customer->getDefaultShipping()){
00474 $customer->setDefaultShipping($customerShipping->getId());
00475 $changed = true;
00476 }
00477
00478 if ($changed) {
00479 $customer->save();
00480 }
00481 }
00482
00483 $this->getQuote()->reserveOrderId();
00484 $convertQuote = Mage::getModel('sales/convert_quote');
00485
00486
00487 if ($this->getQuote()->isVirtual()) {
00488 $order = $convertQuote->addressToOrder($billing);
00489 }
00490 else {
00491 $order = $convertQuote->addressToOrder($shipping);
00492 }
00493
00494 $order->setBillingAddress($convertQuote->addressToOrderAddress($billing));
00495
00496 if (!$this->getQuote()->isVirtual()) {
00497 $order->setShippingAddress($convertQuote->addressToOrderAddress($shipping));
00498 }
00499
00500 $order->setPayment($convertQuote->paymentToOrderPayment($this->getQuote()->getPayment()));
00501
00502 foreach ($this->getQuote()->getAllItems() as $item) {
00503 $orderItem = $convertQuote->itemToOrderItem($item);
00504 if ($item->getParentItem()) {
00505 $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
00506 }
00507 $order->addItem($orderItem);
00508 }
00509
00510
00511
00512
00513 Mage::dispatchEvent('checkout_type_onepage_save_order', array('order'=>$order, 'quote'=>$this->getQuote()));
00514
00515 if ($this->getQuote()->getCheckoutMethod() == Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER) {
00516 if ($this->_customerEmailExists($customer->getEmail(), Mage::app()->getWebsite()->getId())) {
00517 Mage::throwException(Mage::helper('checkout')->__('There is already a customer registered using this email address'));
00518 }
00519 }
00520 $order->place();
00521
00522 if ($this->getQuote()->getCheckoutMethod()==Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER) {
00523 $customer->save();
00524 $customerBillingId = $customerBilling->getId();
00525 if (!$this->getQuote()->isVirtual()) {
00526 $customerShippingId = isset($customerShipping) ? $customerShipping->getId() : $customerBillingId;
00527 $customer->setDefaultShipping($customerShippingId);
00528 }
00529 $customer->setDefaultBilling($customerBillingId);
00530 $customer->save();
00531
00532 $this->getQuote()->setCustomerId($customer->getId());
00533
00534 $order->setCustomerId($customer->getId());
00535 Mage::helper('core')->copyFieldset('customer_account', 'to_order', $customer, $order);
00536
00537 $billing->setCustomerId($customer->getId())->setCustomerAddressId($customerBillingId);
00538 if (!$this->getQuote()->isVirtual()) {
00539 $shipping->setCustomerId($customer->getId())->setCustomerAddressId($customerShippingId);
00540 }
00541
00542 if ($customer->isConfirmationRequired()) {
00543 $customer->sendNewAccountEmail('confirmation');
00544 }
00545 else {
00546 $customer->sendNewAccountEmail();
00547 }
00548 }
00549
00550
00551
00552
00553
00554 $redirectUrl = $this->getQuote()->getPayment()->getOrderPlaceRedirectUrl();
00555 if(!$redirectUrl){
00556 $order->setEmailSent(true);
00557 }
00558
00559 $order->save();
00560
00561 Mage::dispatchEvent('checkout_type_onepage_save_order_after', array('order'=>$order, 'quote'=>$this->getQuote()));
00562
00563
00564
00565
00566
00567
00568 $orderId = $order->getIncrementId();
00569 $this->getCheckout()->setLastQuoteId($this->getQuote()->getId());
00570 $this->getCheckout()->setLastOrderId($order->getId());
00571 $this->getCheckout()->setLastRealOrderId($order->getIncrementId());
00572 $this->getCheckout()->setRedirectUrl($redirectUrl);
00573
00574
00575
00576
00577 if(!$redirectUrl){
00578 $order->sendNewOrderEmail();
00579 }
00580
00581 if ($this->getQuote()->getCheckoutMethod(true)==Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER
00582 && !Mage::getSingleton('customer/session')->isLoggedIn()) {
00583
00584
00585
00586
00587
00588 $this->getQuote()->save();
00589 if ($customer->isConfirmationRequired()) {
00590 Mage::getSingleton('checkout/session')->addSuccess(Mage::helper('customer')->__('Account confirmation is required. Please, check your e-mail for confirmation link. To resend confirmation email please <a href="%s">click here</a>.',
00591 Mage::helper('customer')->getEmailConfirmationUrl($customer->getEmail())
00592 ));
00593 }
00594 else {
00595 Mage::getSingleton('customer/session')->loginById($customer->getId());
00596 }
00597 }
00598
00599
00600
00601 $this->getCheckout()->setLastSuccessQuoteId($this->getQuote()->getId());
00602
00603 $this->getQuote()->setIsActive(false);
00604 $this->getQuote()->save();
00605
00606 return $this;
00607 }
00608
00609
00610
00611
00612
00613
00614
00615
00616 protected function _customerEmailExists($email, $websiteId = null)
00617 {
00618 $customer = Mage::getModel('customer/customer');
00619 if ($websiteId) {
00620 $customer->setWebsiteId($websiteId);
00621 }
00622 $customer->loadByEmail($email);
00623 if ($customer->getId()) {
00624 return $customer;
00625 }
00626 return false;
00627 }
00628
00629
00630
00631
00632
00633
00634 public function getLastOrderId()
00635 {
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652 $order = Mage::getModel('sales/order');
00653 $order->load($this->getCheckout()->getLastOrderId());
00654 $orderId = $order->getIncrementId();
00655 return $orderId;
00656 }
00657 }