1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_XmlConnect_CheckoutController extends Mage_XmlConnect_Controller_Action
35: {
36: 37: 38: 39: 40:
41: public function preDispatch()
42: {
43: parent::preDispatch();
44: if (!Mage::getSingleton('customer/session')->isLoggedIn()
45: && !Mage::getSingleton('checkout/session')->getQuote()->isAllowedGuestCheckout()
46: ) {
47: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
48: $this->_message(
49: $this->__('Customer not logged in.'),
50: self::MESSAGE_STATUS_ERROR,
51: array('logged_in' => '0')
52: );
53: return ;
54: }
55: }
56:
57: 58: 59: 60: 61:
62: public function getOnepage()
63: {
64: return Mage::getSingleton('checkout/type_onepage');
65: }
66:
67: 68: 69: 70: 71:
72: public function indexAction()
73: {
74: if (!Mage::helper('checkout')->canOnepageCheckout()) {
75: $this->_message($this->__('Onepage checkout is disabled.'), self::MESSAGE_STATUS_ERROR);
76: return;
77: }
78:
79: $quote = $this->getOnepage()->getQuote();
80: if ($quote->getHasError()) {
81: $this->_message($this->__('Cart has some errors.'), self::MESSAGE_STATUS_ERROR);
82: return;
83: } else if (!$quote->hasItems()) {
84: $this->_message($this->__('Cart is empty.'), self::MESSAGE_STATUS_ERROR);
85: return;
86: } else if (!$quote->validateMinimumAmount()) {
87: $error = Mage::getStoreConfig('sales/minimum_order/error_message');
88: $this->_message($error, self::MESSAGE_STATUS_ERROR);
89: return;
90: }
91: Mage::getSingleton('checkout/session')->setCartWasUpdated(false);
92: $this->getOnepage()->initCheckout();
93:
94: try {
95: $this->loadLayout(false);
96: $this->renderLayout();
97: } catch (Mage_Core_Exception $e) {
98: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
99: } catch (Exception $e) {
100: $this->_message($this->__('Unable to load checkout.'), self::MESSAGE_STATUS_ERROR);
101: Mage::logException($e);
102: }
103: }
104:
105: 106: 107: 108: 109:
110: public function newBillingAddressFormAction()
111: {
112: try {
113: $this->loadLayout(false);
114: $this->renderLayout();
115: } catch (Mage_Core_Exception $e) {
116: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
117: } catch (Exception $e) {
118: $this->_message($this->__('Unable to load billing address form.'), self::MESSAGE_STATUS_ERROR);
119: Mage::logException($e);
120: }
121: }
122:
123: 124: 125: 126: 127:
128: public function newShippingAddressFormAction()
129: {
130: try {
131: $this->loadLayout(false);
132: $this->renderLayout();
133: } catch (Mage_Core_Exception $e) {
134: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
135: } catch (Exception $e) {
136: $this->_message($this->__('Unable to load shipping address form.'), self::MESSAGE_STATUS_ERROR);
137: Mage::logException($e);
138: }
139: }
140:
141: 142: 143: 144: 145:
146: public function billingAddressAction()
147: {
148: try {
149: $this->loadLayout(false);
150: $this->renderLayout();
151: } catch (Mage_Core_Exception $e) {
152: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
153: } catch (Exception $e) {
154: $this->_message($this->__('Unable to load billing address.'), self::MESSAGE_STATUS_ERROR);
155: Mage::logException($e);
156: }
157: }
158:
159: 160: 161: 162: 163:
164: public function saveBillingAddressAction()
165: {
166: if (!$this->getRequest()->isPost()) {
167: $this->_message($this->__('Specified invalid data.'), self::MESSAGE_STATUS_ERROR);
168: return;
169: }
170:
171: $data = $this->getRequest()->getPost('billing', array());
172: $customerAddressId = $this->getRequest()->getPost('billing_address_id', false);
173: if (isset($data['email'])) {
174: $data['email'] = trim($data['email']);
175: }
176: $result = $this->getOnepage()->saveBilling($data, $customerAddressId);
177: if (!isset($result['error'])) {
178: $this->_message($this->__('Billing address has been set.'), self::MESSAGE_STATUS_SUCCESS);
179: } else {
180: if (!is_array($result['message'])) {
181: $result['message'] = array($result['message']);
182: }
183: $this->_message(implode('. ', $result['message']), self::MESSAGE_STATUS_ERROR);
184: }
185: }
186:
187: 188: 189: 190: 191:
192: public function shippingAddressAction()
193: {
194: try {
195: $this->loadLayout(false);
196: $this->renderLayout();
197: } catch (Mage_Core_Exception $e) {
198: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
199: } catch (Exception $e) {
200: $this->_message($this->__('Unable to load billing address.'), self::MESSAGE_STATUS_ERROR);
201: Mage::logException($e);
202: }
203: }
204:
205: 206: 207: 208: 209:
210: public function saveShippingAddressAction()
211: {
212: if (!$this->getRequest()->isPost()) {
213: $this->_message($this->__('Specified invalid data.'), self::MESSAGE_STATUS_ERROR);
214: return;
215: }
216:
217: $data = $this->getRequest()->getPost('shipping', array());
218: $customerAddressId = $this->getRequest()->getPost('shipping_address_id', false);
219: 220: 221:
222: $useForShipping = $this->getRequest()->getPost('use_for_shipping');
223:
224: $billingAddress = $this->getOnepage()->getQuote()->getBillingAddress();
225: 226: 227: 228:
229: if (is_null($useForShipping)) {
230: $useForShipping = $this->_checkUseForShipping($data, $billingAddress, $customerAddressId);
231: }
232:
233: if ($useForShipping) {
234: 235: 236:
237: $customerAddressId = $billingAddress->getId();
238: 239: 240:
241: $data['same_as_billing'] = true;
242: }
243: $result = $this->getOnepage()->saveShipping($data, $customerAddressId);
244: if (!isset($result['error'])) {
245: $this->_message($this->__('Shipping address has been set.'), self::MESSAGE_STATUS_SUCCESS);
246: } else {
247: if (!is_array($result['message'])) {
248: $result['message'] = array($result['message']);
249: }
250: $this->_message(implode('. ', $result['message']), self::MESSAGE_STATUS_ERROR);
251: }
252: }
253:
254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266:
267: protected function _checkUseForShipping(array $data, $billingAddress, $shippingAddressId)
268: {
269: $useForShipping = !$shippingAddressId || $billingAddress->getId() == $shippingAddressId;
270:
271: if ($useForShipping) {
272: foreach ($data as $key => $value) {
273: if ($key == 'save_in_address_book') {
274: continue;
275: }
276: $billingData = $billingAddress->getDataUsingMethod($key);
277: if (is_array($value) && is_array($billingData)) {
278: foreach ($value as $k => $v) {
279: if (!isset($billingData[$k]) || $billingData[$k] != trim($v)) {
280: $useForShipping = false;
281: break;
282: }
283: }
284: } else {
285: if (is_string($value) && $billingData != trim($value)) {
286: $useForShipping = false;
287: break;
288: } else {
289: $useForShipping = false;
290: break;
291: }
292: }
293: }
294: }
295: return $useForShipping;
296: }
297:
298: 299: 300: 301: 302:
303: public function shippingMethodsAction()
304: {
305: try {
306: $result = array('error' => $this->__('Error.'));
307: $this->getOnepage()->getQuote()->getShippingAddress()->setCollectShippingRates(true);
308: $this->getOnepage()->getQuote()->collectTotals()->save();
309: $this->loadLayout(false);
310: $this->renderLayout();
311: return;
312: } catch (Mage_Core_Exception $e) {
313: $result['error'] = $e->getMessage();
314: }
315: $this->_message($result['error'], self::MESSAGE_STATUS_ERROR);
316: }
317:
318: 319: 320: 321: 322:
323: public function saveShippingMethodAction()
324: {
325: if (!$this->getRequest()->isPost()) {
326: $this->_message($this->__('Specified invalid data.'), self::MESSAGE_STATUS_ERROR);
327: return;
328: }
329:
330: $data = $this->getRequest()->getPost('shipping_method', '');
331: $result = $this->getOnepage()->saveShippingMethod($data);
332: if (!$result) {
333:
334: Mage::dispatchEvent('checkout_controller_onepage_save_shipping_method', array(
335: 'request' => $this->getRequest(), 'quote' => $this->getOnepage()->getQuote()
336: ));
337: $this->getOnepage()->getQuote()->collectTotals()->save();
338:
339: $this->_message($this->__('Shipping method has been set.'), self::MESSAGE_STATUS_SUCCESS);
340: } elseif(isset($result['error'])) {
341: if (!is_array($result['message'])) {
342: $result['message'] = array($result['message']);
343: }
344: Mage::dispatchEvent('checkout_controller_onepage_save_shipping_method', array(
345: 'request' => $this->getRequest(), 'quote' => $this->getOnepage()->getQuote()
346: ));
347: $this->getOnepage()->getQuote()->collectTotals()->save();
348: $this->_message(implode('. ', $result['message']), self::MESSAGE_STATUS_ERROR);
349: }
350: }
351:
352:
353: 354: 355: 356: 357:
358: public function saveMethodAction()
359: {
360: if ($this->getRequest()->isPost()) {
361: $method = (string) $this->getRequest()->getPost('method');
362: $result = $this->getOnepage()->saveCheckoutMethod($method);
363: if (!isset($result['error'])) {
364: $this->_message($this->__('Payment Method has been set.'), self::MESSAGE_STATUS_SUCCESS);
365: } else {
366: if (!is_array($result['message'])) {
367: $result['message'] = array($result['message']);
368: }
369: $this->_message(implode('. ', $result['message']), self::MESSAGE_STATUS_ERROR);
370: }
371: }
372: }
373:
374: 375: 376: 377: 378:
379: public function paymentMethodsAction()
380: {
381: try {
382: $this->loadLayout(false);
383: $this->renderLayout();
384: return;
385: } catch (Mage_Core_Exception $e) {
386: $result['error'] = $e->getMessage();
387: }
388: $this->_message($result['error'], self::MESSAGE_STATUS_ERROR);
389: }
390:
391: 392: 393: 394: 395:
396: public function savePaymentAction()
397: {
398: if (!$this->getRequest()->isPost()) {
399: $this->_message($this->__('Specified invalid data.'), self::MESSAGE_STATUS_ERROR);
400: return;
401: }
402: try {
403:
404: $result = array();
405: $data = $this->getRequest()->getPost('payment', array());
406: $result = $this->getOnepage()->savePayment($data);
407: $this->_message($this->__('Payment method was successfully set.'), self::MESSAGE_STATUS_SUCCESS);
408: return;
409: } catch (Mage_Payment_Exception $e) {
410: $result['error'] = $e->getMessage();
411: } catch (Mage_Core_Exception $e) {
412: $result['error'] = $e->getMessage();
413: } catch (Exception $e) {
414: Mage::logException($e);
415: $result['error'] = $e->getMessage();
416: }
417: $this->_message($result['error'], self::MESSAGE_STATUS_ERROR);
418: }
419:
420: 421: 422: 423: 424:
425: public function orderReviewAction()
426: {
427: $this->getOnepage()->getQuote()->collectTotals()->save();
428: try {
429: $this->loadLayout(false);
430: $this->renderLayout();
431: } catch (Mage_Core_Exception $e) {
432: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
433: } catch (Exception $e) {
434: $this->_message($this->__('Unable to load order review.'), self::MESSAGE_STATUS_ERROR);
435: Mage::logException($e);
436: }
437: }
438:
439: 440: 441: 442: 443:
444: public function saveOrderAction()
445: {
446: if (!$this->getRequest()->isPost()) {
447: $this->_message($this->__('Specified invalid data.'), self::MESSAGE_STATUS_ERROR);
448: return;
449: }
450:
451: try {
452: if ($requiredAgreements = Mage::helper('checkout')->getRequiredAgreementIds()) {
453: $postedAgreements = array_keys($this->getRequest()->getPost('agreement', array()));
454: if (array_diff($requiredAgreements, $postedAgreements)) {
455: $error = $this->__('Please agree to all the terms and conditions before placing the order.');
456: $this->_message($error, self::MESSAGE_STATUS_ERROR);
457: return;
458: }
459: }
460: if ($data = $this->getRequest()->getPost('payment', false)) {
461: $this->getOnepage()->getQuote()->getPayment()->importData($data);
462: }
463: $this->getOnepage()->saveOrder();
464:
465:
466: $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
467: $message->addChild('status', self::MESSAGE_STATUS_SUCCESS);
468:
469: $orderId = $this->getOnepage()->getLastOrderId();
470:
471: $text = $this->__('Thank you for your purchase! ');
472: $text .= $this->__('Your order # is: %s. ', $orderId);
473: $text .= $this->__('You will receive an order confirmation email with details of your order and a link to track its progress.');
474: $message->addChild('text', $text);
475:
476: $message->addChild('order_id', $orderId);
477:
478: $this->getOnepage()->getQuote()->save();
479: $this->getOnepage()->getCheckout()->clear();
480:
481: $this->getResponse()->setBody($message->asNiceXml());
482: return;
483: } catch (Mage_Core_Exception $e) {
484: Mage::logException($e);
485: Mage::helper('checkout')->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
486: $error = $e->getMessage();
487: } catch (Exception $e) {
488: Mage::logException($e);
489: Mage::helper('checkout')->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
490: $error = $this->__('An error occurred while processing your order. Please contact us or try again later.');
491: }
492: $this->getOnepage()->getQuote()->save();
493: $this->_message($error, self::MESSAGE_STATUS_ERROR);
494: }
495: }
496: