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_Paypal_MeclController extends Mage_XmlConnect_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected $_configType = 'xmlconnect/payment_method_paypal_config';
42:
43: 44: 45: 46: 47:
48: protected $_configMethod = Mage_XmlConnect_Model_Payment_Method_Paypal_Mecl::MECL_METHOD_CODE;
49:
50: 51: 52: 53: 54:
55: protected $_checkoutType = 'xmlconnect/paypal_mecl_checkout';
56:
57: 58: 59: 60: 61:
62: protected $_checkout = null;
63:
64: 65: 66: 67: 68:
69: protected $_config = null;
70:
71: 72: 73: 74: 75:
76: protected $_quote = false;
77:
78: 79: 80:
81: protected function _construct()
82: {
83: parent::_construct();
84: $this->_config = Mage::getModel($this->_configType, array($this->_configMethod));
85: }
86:
87: 88: 89: 90: 91:
92: public function preDispatch()
93: {
94: parent::preDispatch();
95: if (!Mage::getSingleton('customer/session')->isLoggedIn()
96: && !Mage::getSingleton('checkout/session')->getQuote()->isAllowedGuestCheckout()
97: ) {
98: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
99: $this->_message($this->__('Customer not logged in.'), self::MESSAGE_STATUS_ERROR,
100: array('logged_in' => '0')
101: );
102: return;
103: }
104: }
105:
106: 107: 108:
109: public function startAction()
110: {
111: try {
112: $this->_initCheckout();
113:
114: $customer = Mage::getSingleton('customer/session')->getCustomer();
115: if ($customer && $customer->getId()) {
116: $this->_checkout->setCustomerWithAddressChange(
117: $customer, null, $this->_getQuote()->getShippingAddress()
118: );
119: }
120:
121: $token = $this->_checkout->start(Mage::getUrl('*/*/return'), Mage::getUrl('*/*/cancel'));
122:
123: if ($token) {
124: $this->_initToken($token);
125:
126: $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
127: $message->addChild('status', self::MESSAGE_STATUS_SUCCESS);
128: $message->addChild('token', $token);
129: $this->getResponse()->setBody($message->asNiceXml());
130: } else {
131: $this->_message($this->__('Token has not been set.'), self::MESSAGE_STATUS_ERROR);
132: }
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 start Mobile Express Checkout.'), self::MESSAGE_STATUS_ERROR);
137: Mage::logException($e);
138: }
139: }
140:
141: 142: 143: 144:
145: public function returnAction()
146: {
147: try {
148: $this->_initCheckout();
149: $this->_checkout->returnFromPaypal($this->_initToken());
150: $this->_message($this->__('Mobile Express Checkout processed successfully.'), self::MESSAGE_STATUS_SUCCESS);
151: return;
152: } catch (Mage_Core_Exception $e) {
153: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
154: } catch (Exception $e) {
155: Mage::logException($e);
156: $this->_message($this->__('Unable to initialize return action.'), self::MESSAGE_STATUS_ERROR);
157: }
158: }
159:
160: 161: 162:
163: public function reviewAction()
164: {
165: try {
166: $this->_initCheckout();
167: $this->_checkout->prepareOrderReview($this->_initToken());
168: $this->loadLayout(false);
169: $this->_initLayoutMessages('paypal/session');
170:
171: $messages = $this->_getSession()->getMessages(true);
172: $messageArray = array();
173: foreach ($messages->getItems() as $message) {
174: $messageArray[] = $message;
175: }
176:
177: $detailsBlock = $this->getLayout()->getBlock('xmlconnect.cart.paypal.mecl.review');
178: if (count($messageArray)) {
179: $detailsBlock->setPaypalMessages($messageArray);
180: }
181:
182: $detailsBlock->setQuote($this->_getQuote())->getChild('details')->setQuote($this->_getQuote())
183: ->getChild('totals')->setQuote($this->_getQuote());
184: $this->renderLayout();
185: return;
186: } catch (Mage_Core_Exception $e) {
187: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
188: } catch (Exception $e) {
189: $this->_message($this->__('Unable to initialize express checkout review.'), self::MESSAGE_STATUS_ERROR);
190: Mage::logException($e);
191: }
192: }
193:
194: 195: 196:
197: public function shippingMethodsAction()
198: {
199: try {
200: $this->_initCheckout();
201: $this->_checkout->prepareOrderReview($this->_initToken());
202: $this->loadLayout(false);
203:
204: $this->getLayout()->getBlock('xmlconnect.cart.paypal.mecl.shippingmethods')->setQuote($this->_getQuote());
205: $this->renderLayout();
206: return;
207: } catch (Mage_Core_Exception $e) {
208: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
209: } catch (Exception $e) {
210: $this->_message(
211: $this->__('Unable to initialize express checkout shipping method list.'),
212: self::MESSAGE_STATUS_ERROR
213: );
214: Mage::logException($e);
215: }
216: }
217:
218: 219: 220:
221: public function saveShippingMethodAction()
222: {
223: try {
224: $this->_initCheckout();
225: if ($this->getRequest()->getParam('shipping_method', false)) {
226: $this->_checkout->updateShippingMethod($this->getRequest()->getParam('shipping_method'));
227: $this->_message($this->__('Shipping method successfully updated'), self::MESSAGE_STATUS_SUCCESS);
228: } else {
229: $this->_message($this->__('Shipping method is required'), self::MESSAGE_STATUS_ERROR);
230: }
231: return;
232: } catch (Mage_Core_Exception $e) {
233: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
234: } catch (Exception $e) {
235: $this->_message($this->__('Unable to update shipping method.'), self::MESSAGE_STATUS_ERROR);
236: Mage::logException($e);
237: }
238: }
239:
240: 241: 242:
243: public function placeOrderAction()
244: {
245: try {
246: $this->_initCheckout();
247: $this->_checkout->place($this->_initToken());
248:
249:
250: $session = $this->_getCheckoutSession();
251: $session->clearHelperData();
252:
253:
254: $quoteId = $this->_getQuote()->getId();
255: $session->setLastQuoteId($quoteId)->setLastSuccessQuoteId($quoteId);
256:
257:
258: $order = $this->_checkout->getOrder();
259: if ($order) {
260: $orderId = $order->getId();
261: $realOrderId = $order->getIncrementId();
262: $session->setLastOrderId($order->getId())->setLastRealOrderId($order->getIncrementId());
263: }
264:
265:
266: $profiles = $this->_checkout->getRecurringPaymentProfiles();
267: if ($profiles) {
268: $ids = array();
269: foreach($profiles as $profile) {
270: $ids[] = $profile->getId();
271: }
272: $session->setLastRecurringProfileIds($ids);
273: }
274:
275: $this->_initToken(false);
276:
277:
278: $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
279: $message->addChild('status', self::MESSAGE_STATUS_SUCCESS);
280:
281: $text = $this->__('Thank you for your purchase! ');
282: $text .= $this->__('Your order # is: %s. ', $realOrderId);
283: $text .= $this->__('You will receive an order confirmation email with details of your order and a link to track its progress.');
284: $message->addChild('text', $text);
285: $message->addChild('order_id', $orderId);
286: $this->getResponse()->setBody($message->asNiceXml());
287: return;
288: } catch (Mage_Core_Exception $e) {
289: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
290: } catch (Exception $e) {
291: $this->_message($this->__('Unable to place the order.'), self::MESSAGE_STATUS_ERROR);
292: Mage::logException($e);
293: }
294: }
295:
296: 297: 298:
299: public function cancelAction()
300: {
301: try {
302: $this->_initToken(false);
303:
304: $orderId = $this->_getCheckoutSession()->getLastOrderId();
305: $order = ($orderId) ? Mage::getModel('sales/order')->load($orderId) : false;
306:
307: if ($order && $order->getId() && $order->getQuoteId() == $this->_getCheckoutSession()->getQuoteId()) {
308: $order->cancel()->save();
309: $this->_getCheckoutSession()->unsLastQuoteId()->unsLastSuccessQuoteId()->unsLastOrderId()
310: ->unsLastRealOrderId();
311: }
312:
313: $this->_message($this->__('Mobile Express Checkout has been canceled.'), self::MESSAGE_STATUS_SUCCESS);
314: } catch (Mage_Core_Exception $e) {
315: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
316: } catch (Exception $e) {
317: $this->_message($this->__('Unable to cancel Mobile Express Checkout.'), self::MESSAGE_STATUS_ERROR);
318: Mage::logException($e);
319: }
320: }
321:
322: 323: 324: 325: 326: 327:
328: protected function _initCheckout()
329: {
330: $quote = $this->_getQuote();
331: if (!$quote->hasItems() || $quote->getHasError()) {
332: Mage::throwException($this->__('Unable to initialize PayPal Mobile Express Checkout.'));
333: }
334: if (!$quote->validateMinimumAmount()) {
335: $error = Mage::getStoreConfig('sales/minimum_order/error_message');
336: Mage::throwException($error);
337: }
338: $this->_getCheckoutSession()->setCartWasUpdated(false);
339:
340: $this->_checkout = Mage::getSingleton($this->_checkoutType, array(
341: 'config' => $this->_config, 'quote' => $quote
342: ));
343: }
344:
345: 346: 347: 348: 349:
350: protected function _getCheckoutSession()
351: {
352: return Mage::getSingleton('checkout/session');
353: }
354:
355: 356: 357: 358: 359:
360: protected function _getQuote()
361: {
362: if (!$this->_quote) {
363: $this->_quote = $this->_getCheckoutSession()->getQuote();
364: }
365: return $this->_quote;
366: }
367:
368: 369: 370: 371: 372: 373: 374: 375:
376: protected function _initToken($setToken = null)
377: {
378: if (null !== $setToken) {
379: if (false === $setToken) {
380: if (!$this->_getSession()->getExpressCheckoutToken()) {
381: Mage::throwException($this->__('PayPal Mobile Express Checkout Token does not exist.'));
382: }
383: $this->_getSession()->unsExpressCheckoutToken();
384: } else {
385: $this->_getSession()->setExpressCheckoutToken($setToken);
386: }
387: return $this;
388: }
389:
390: $setToken = $this->getRequest()->getParam('token');
391: if ($setToken) {
392: if ($setToken !== $this->_getSession()->getExpressCheckoutToken()) {
393: Mage::throwException($this->__('Wrong PayPal Mobile Express Checkout Token specified.'));
394: }
395: } else {
396: $setToken = $this->_getSession()->getExpressCheckoutToken();
397: }
398: return $setToken;
399: }
400:
401: 402: 403: 404: 405:
406: private function _getSession()
407: {
408: return Mage::getSingleton('paypal/session');
409: }
410: }
411: