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_OnepageController extends Mage_Checkout_Controller_Action
00029 {
00030
00031
00032
00033 public function preDispatch()
00034 {
00035 parent::preDispatch();
00036
00037 if (!$this->_preDispatchValidateCustomer()) {
00038 return $this;
00039 }
00040
00041 return $this;
00042 }
00043
00044 protected function _ajaxRedirectResponse()
00045 {
00046 $this->getResponse()
00047 ->setHeader('HTTP/1.1', '403 Session Expired')
00048 ->setHeader('Login-Required', 'true')
00049 ->sendResponse();
00050 return $this;
00051 }
00052
00053 protected function _expireAjax()
00054 {
00055 if (!$this->getOnepage()->getQuote()->hasItems()
00056 || $this->getOnepage()->getQuote()->getHasError()
00057 || $this->getOnepage()->getQuote()->getIsMultiShipping()) {
00058 $this->_ajaxRedirectResponse();
00059 exit;
00060 }
00061 $action = $this->getRequest()->getActionName();
00062 if (Mage::getSingleton('checkout/session')->getCartWasUpdated(true)
00063 && !in_array($action, array('index', 'progress'))) {
00064 $this->_ajaxRedirectResponse();
00065 exit;
00066 }
00067 Mage::getSingleton('core/translate_inline')->setIsAjaxRequest(true);
00068 }
00069
00070 protected function _getShippingMethodsHtml()
00071 {
00072 $layout = $this->getLayout();
00073 $update = $layout->getUpdate();
00074 $update->load('checkout_onepage_shippingmethod');
00075 $layout->generateXml();
00076 $layout->generateBlocks();
00077 $output = $layout->getOutput();
00078 return $output;
00079 }
00080
00081 protected function _getPaymentMethodsHtml()
00082 {
00083 $layout = $this->getLayout();
00084 $update = $layout->getUpdate();
00085 $update->load('checkout_onepage_paymentmethod');
00086 $layout->generateXml();
00087 $layout->generateBlocks();
00088 $output = $layout->getOutput();
00089 return $output;
00090 }
00091
00092 protected function _getAdditionalHtml()
00093 {
00094 $layout = $this->getLayout();
00095 $update = $layout->getUpdate();
00096 $update->load('checkout_onepage_additional');
00097 $layout->generateXml();
00098 $layout->generateBlocks();
00099 $output = $layout->getOutput();
00100 return $output;
00101 }
00102
00103
00104
00105
00106
00107
00108 public function getOnepage()
00109 {
00110 return Mage::getSingleton('checkout/type_onepage');
00111 }
00112
00113
00114
00115
00116 public function indexAction()
00117 {
00118 if (!Mage::helper('checkout')->canOnepageCheckout()) {
00119 Mage::getSingleton('checkout/session')->addError($this->__('Sorry, Onepage Checkout is disabled.'));
00120 $this->_redirect('checkout/cart');
00121 return;
00122 }
00123 $quote = $this->getOnepage()->getQuote();
00124 if (!$quote->hasItems() || $quote->getHasError()) {
00125 $this->_redirect('checkout/cart');
00126 return;
00127 }
00128 if (!$quote->validateMinimumAmount()) {
00129 $error = Mage::getStoreConfig('sales/minimum_order/error_message');
00130 Mage::getSingleton('checkout/session')->addError($error);
00131 $this->_redirect('checkout/cart');
00132 return;
00133 }
00134 Mage::getSingleton('checkout/session')->setCartWasUpdated(false);
00135 Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri());
00136 $this->getOnepage()->initCheckout();
00137 $this->loadLayout();
00138 $this->_initLayoutMessages('customer/session');
00139 $this->getLayout()->getBlock('head')->setTitle($this->__('Checkout'));
00140 $this->renderLayout();
00141 }
00142
00143
00144
00145
00146 public function progressAction()
00147 {
00148 $this->_expireAjax();
00149 $this->loadLayout(false);
00150 $this->renderLayout();
00151 }
00152
00153 public function shippingMethodAction()
00154 {
00155 $this->_expireAjax();
00156 $this->loadLayout(false);
00157 $this->renderLayout();
00158 }
00159
00160 public function reviewAction()
00161 {
00162 $this->_expireAjax();
00163 $this->loadLayout(false);
00164 $this->renderLayout();
00165 }
00166
00167 public function successAction()
00168 {
00169 if (!$this->getOnepage()->getCheckout()->getLastSuccessQuoteId()) {
00170 $this->_redirect('checkout/cart');
00171 return;
00172 }
00173
00174 $lastQuoteId = $this->getOnepage()->getCheckout()->getLastQuoteId();
00175 $lastOrderId = $this->getOnepage()->getCheckout()->getLastOrderId();
00176
00177 if (!$lastQuoteId || !$lastOrderId) {
00178 $this->_redirect('checkout/cart');
00179 return;
00180 }
00181
00182 Mage::getSingleton('checkout/session')->clear();
00183 $this->loadLayout();
00184 $this->_initLayoutMessages('checkout/session');
00185 Mage::dispatchEvent('checkout_onepage_controller_success_action');
00186 $this->renderLayout();
00187 }
00188
00189 public function failureAction()
00190 {
00191 $lastQuoteId = $this->getOnepage()->getCheckout()->getLastQuoteId();
00192 $lastOrderId = $this->getOnepage()->getCheckout()->getLastOrderId();
00193
00194 if (!$lastQuoteId || !$lastOrderId) {
00195 $this->_redirect('checkout/cart');
00196 return;
00197 }
00198
00199 $this->loadLayout();
00200 $this->renderLayout();
00201 }
00202
00203
00204 public function getAdditionalAction()
00205 {
00206 $this->getResponse()->setBody($this->_getAdditionalHtml());
00207 }
00208
00209
00210
00211
00212 public function getAddressAction()
00213 {
00214 $this->_expireAjax();
00215 $addressId = $this->getRequest()->getParam('address', false);
00216 if ($addressId) {
00217 $address = $this->getOnepage()->getAddress($addressId);
00218 $this->getResponse()->setHeader('Content-type', 'application/x-json');
00219 $this->getResponse()->setBody($address->toJson());
00220 }
00221 }
00222
00223 public function saveMethodAction()
00224 {
00225 $this->_expireAjax();
00226 if ($this->getRequest()->isPost()) {
00227 $method = $this->getRequest()->getPost('method');
00228 $result = $this->getOnepage()->saveCheckoutMethod($method);
00229 $this->getResponse()->setBody(Zend_Json::encode($result));
00230 }
00231 }
00232
00233
00234
00235
00236 public function saveBillingAction()
00237 {
00238 $this->_expireAjax();
00239 if ($this->getRequest()->isPost()) {
00240 $data = $this->getRequest()->getPost('billing', array());
00241 $customerAddressId = $this->getRequest()->getPost('billing_address_id', false);
00242 $result = $this->getOnepage()->saveBilling($data, $customerAddressId);
00243
00244 if (!isset($result['error'])) {
00245
00246 if ($this->getOnepage()->getQuote()->isVirtual()) {
00247 $result['goto_section'] = 'payment';
00248 $result['update_section'] = array(
00249 'name' => 'payment-method',
00250 'html' => $this->_getPaymentMethodsHtml()
00251 );
00252 }
00253 elseif (isset($data['use_for_shipping']) && $data['use_for_shipping'] == 1) {
00254
00255 $result['goto_section'] = 'shipping_method';
00256
00257 $result['update_section'] = array(
00258 'name' => 'shipping-method',
00259 'html' => $this->_getShippingMethodsHtml()
00260 );
00261
00262 $result['allow_sections'] = array('shipping');
00263 $result['duplicateBillingInfo'] = 'true';
00264 }
00265 else {
00266 $result['goto_section'] = 'shipping';
00267 }
00268 }
00269
00270 $this->getResponse()->setBody(Zend_Json::encode($result));
00271 }
00272 }
00273
00274 public function saveShippingAction()
00275 {
00276 $this->_expireAjax();
00277 if ($this->getRequest()->isPost()) {
00278 $data = $this->getRequest()->getPost('shipping', array());
00279 $customerAddressId = $this->getRequest()->getPost('shipping_address_id', false);
00280 $result = $this->getOnepage()->saveShipping($data, $customerAddressId);
00281
00282 if (!isset($result['error'])) {
00283 $result['goto_section'] = 'shipping_method';
00284 $result['update_section'] = array(
00285 'name' => 'shipping-method',
00286 'html' => $this->_getShippingMethodsHtml()
00287 );
00288 }
00289
00290
00291
00292
00293
00294 $this->getResponse()->setBody(Zend_Json::encode($result));
00295 }
00296 }
00297
00298 public function saveShippingMethodAction()
00299 {
00300 $this->_expireAjax();
00301 if ($this->getRequest()->isPost()) {
00302 $data = $this->getRequest()->getPost('shipping_method', '');
00303 $result = $this->getOnepage()->saveShippingMethod($data);
00304
00305
00306
00307 if(!$result) {
00308 Mage::dispatchEvent('checkout_controller_onepage_save_shipping_method', array('request'=>$this->getRequest(), 'quote'=>$this->getOnepage()->getQuote()));
00309 $this->getResponse()->setBody(Zend_Json::encode($result));
00310
00311 $result['goto_section'] = 'payment';
00312 $result['update_section'] = array(
00313 'name' => 'payment-method',
00314 'html' => $this->_getPaymentMethodsHtml()
00315 );
00316
00317
00318 }
00319 $this->getResponse()->setBody(Zend_Json::encode($result));
00320 }
00321
00322 }
00323
00324 public function savePaymentAction()
00325 {
00326 $this->_expireAjax();
00327 if ($this->getRequest()->isPost()) {
00328 $data = $this->getRequest()->getPost('payment', array());
00329
00330
00331
00332
00333 try {
00334 $result = $this->getOnepage()->savePayment($data);
00335 }
00336 catch (Mage_Payment_Exception $e) {
00337 if ($e->getFields()) {
00338 $result['fields'] = $e->getFields();
00339 }
00340 $result['error'] = $e->getMessage();
00341 }
00342 catch (Exception $e) {
00343 $result['error'] = $e->getMessage();
00344 }
00345 $redirectUrl = $this->getOnePage()->getQuote()->getPayment()->getCheckoutRedirectUrl();
00346 if (empty($result['error']) && !$redirectUrl) {
00347 $this->loadLayout('checkout_onepage_review');
00348
00349 $result['goto_section'] = 'review';
00350 $result['update_section'] = array(
00351 'name' => 'review',
00352 'html' => $this->getLayout()->getBlock('root')->toHtml()
00353 );
00354
00355
00356 }
00357
00358 if ($redirectUrl) {
00359 $result['redirect'] = $redirectUrl;
00360 }
00361
00362 $this->getResponse()->setBody(Zend_Json::encode($result));
00363 }
00364 }
00365
00366 public function saveOrderAction()
00367 {
00368 $this->_expireAjax();
00369
00370 $result = array();
00371 try {
00372 if ($requiredAgreements = Mage::helper('checkout')->getRequiredAgreementIds()) {
00373 $postedAgreements = array_keys($this->getRequest()->getPost('agreement', array()));
00374 if ($diff = array_diff($requiredAgreements, $postedAgreements)) {
00375 $result['success'] = false;
00376 $result['error'] = true;
00377 $result['error_messages'] = $this->__('Please agree to all Terms and Conditions before placing the order.');
00378 $this->getResponse()->setBody(Zend_Json::encode($result));
00379 return;
00380 }
00381 }
00382 if ($data = $this->getRequest()->getPost('payment', false)) {
00383 $this->getOnepage()->getQuote()->getPayment()->importData($data);
00384 }
00385 $this->getOnepage()->saveOrder();
00386 $redirectUrl = $this->getOnepage()->getCheckout()->getRedirectUrl();
00387 $result['success'] = true;
00388 $result['error'] = false;
00389 }
00390 catch (Mage_Core_Exception $e) {
00391 Mage::logException($e);
00392 Mage::helper('checkout')->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
00393 $result['success'] = false;
00394 $result['error'] = true;
00395 $result['error_messages'] = $e->getMessage();
00396 $this->getOnepage()->getQuote()->save();
00397 }
00398 catch (Exception $e) {
00399 Mage::logException($e);
00400 Mage::helper('checkout')->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
00401 $result['success'] = false;
00402 $result['error'] = true;
00403 $result['error_messages'] = $this->__('There was an error processing your order. Please contact us or try again later.');
00404 $this->getOnepage()->getQuote()->save();
00405 }
00406
00407
00408
00409
00410
00411 if (isset($redirectUrl)) {
00412 $result['redirect'] = $redirectUrl;
00413 }
00414
00415 $this->getResponse()->setBody(Zend_Json::encode($result));
00416 }
00417
00418 }