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_MepController extends Mage_XmlConnect_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected $_checkout = null;
42:
43: 44: 45: 46: 47:
48: protected $_quote = false;
49:
50: 51: 52: 53: 54:
55: public function preDispatch()
56: {
57: parent::preDispatch();
58: if (!Mage::getSingleton('customer/session')->isLoggedIn()
59: && !Mage::getSingleton('checkout/session')->getQuote()->isAllowedGuestCheckout()
60: ) {
61: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
62: $this->_message(
63: $this->__('Customer not logged in.'), self::MESSAGE_STATUS_ERROR, array('logged_in' => '0')
64: );
65: return;
66: }
67: }
68:
69: 70: 71: 72: 73:
74: public function indexAction()
75: {
76: try {
77: if (is_object(Mage::getConfig()->getNode('modules/Enterprise_GiftCardAccount'))) {
78: $giftcardInfoBlock = $this->getLayout()->addBlock(
79: 'enterprise_giftcardaccount/checkout_onepage_payment_additional', 'giftcard_info'
80: );
81:
82: if (intval($giftcardInfoBlock->getAppliedGiftCardAmount())) {
83: $this->_message(
84: $this->__('Paypal MEP doesn\'t support checkout with any discount.'),
85: self::MESSAGE_STATUS_ERROR
86: );
87: return;
88: }
89: }
90:
91: $this->_initCheckout();
92: $this->_checkout->initCheckout();
93: $this->_message($this->__('Checkout has been initialized.'), self::MESSAGE_STATUS_SUCCESS);
94: return;
95: } catch (Mage_Core_Exception $e) {
96: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
97: } catch (Exception $e) {
98: $this->_message($this->__('Unable to start MEP Checkout.'), self::MESSAGE_STATUS_ERROR);
99: Mage::logException($e);
100: }
101: }
102:
103: 104: 105: 106: 107:
108: public function saveShippingAddressAction()
109: {
110: if (!$this->getRequest()->isPost()) {
111: $this->_message($this->__('Specified invalid data.'), self::MESSAGE_STATUS_ERROR);
112: return;
113: }
114:
115: try {
116: $this->_initCheckout();
117: $data = $this->getRequest()->getPost('shipping', array());
118:
119: array_walk_recursive($data, create_function('&$val', '$val = trim($val);'));
120:
121: if (!empty($data['region']) && isset($data['country_id'])) {
122: $region = Mage::getModel('directory/region')->loadByCode($data['region'], $data['country_id']);
123: if ($region && $region->getId()) {
124: $data['region_id'] = $region->getId();
125: }
126: }
127:
128: $result = $this->_checkout->saveShipping($data);
129: if (!isset($result['error'])) {
130: $this->_message(
131: $this->__('Shipping address has been set.'),
132: self::MESSAGE_STATUS_SUCCESS
133: );
134: } else {
135: if (!is_array($result['message'])) {
136: $result['message'] = array($result['message']);
137: }
138: $this->_message(implode('. ', $result['message']), self::MESSAGE_STATUS_ERROR);
139: }
140: } catch (Mage_Core_Exception $e) {
141: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
142: Mage::logException($e);
143: } catch (Exception $e) {
144: $this->_message($this->__('Unable to save shipping address.'), self::MESSAGE_STATUS_ERROR);
145: Mage::logException($e);
146: }
147: }
148:
149: 150: 151: 152: 153:
154: public function shippingMethodsAction()
155: {
156: try {
157: $this->_initCheckout();
158: $this->loadLayout(false);
159: $this->renderLayout();
160: } catch (Mage_Core_Exception $e) {
161: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
162: } catch (Exception $e) {
163: $this->_message($this->__('Unable to get shipping methods list.'), self::MESSAGE_STATUS_ERROR);
164: Mage::logException($e);
165: }
166: }
167:
168: 169: 170: 171: 172:
173: public function saveShippingMethodAction()
174: {
175: if (!$this->getRequest()->isPost()) {
176: $this->_message($this->__('Specified invalid data.'), self::MESSAGE_STATUS_ERROR);
177: return;
178: }
179:
180: try {
181: $this->_initCheckout();
182: $data = $this->getRequest()->getPost('shipping_method', '');
183: $this->_getQuote()->getShippingAddress()->setShippingMethod($data)->setCollectShippingRates(true)->save();
184: $result = $this->_checkout->saveShippingMethod($data);
185:
186: if (!isset($result['error'])) {
187:
188: $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
189: $message->addChild('status', self::MESSAGE_STATUS_SUCCESS);
190: $message->addChild('text', $this->__('Shipping method has been set.'));
191: if ($this->_getQuote()->isVirtual()) {
192: $quoteAddress = $this->_getQuote()->getBillingAddress();
193: } else {
194: $quoteAddress = $this->_getQuote()->getShippingAddress();
195: }
196: $taxAmount = Mage::helper('core')->currency($quoteAddress->getBaseTaxAmount(), false, false);
197: $message->addChild('tax_amount', Mage::helper('xmlconnect')->formatPriceForXml($taxAmount));
198: $this->_getQuote()->collectTotals()->save();
199: $this->getResponse()->setBody($message->asNiceXml());
200: } else {
201: if (!is_array($result['message'])) {
202: $result['message'] = array($result['message']);
203: }
204: $this->_message(implode('. ', $result['message']), self::MESSAGE_STATUS_ERROR);
205: }
206: } catch (Mage_Core_Exception $e) {
207: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
208: } catch (Exception $e) {
209: $this->_message($this->__('Unable to save shipping method.'), self::MESSAGE_STATUS_ERROR);
210: Mage::logException($e);
211: }
212: }
213:
214: 215: 216: 217: 218:
219: public function cartTotalsAction()
220: {
221: try {
222: $this->_initCheckout();
223: $this->loadLayout(false);
224: $this->renderLayout();
225: return;
226: } catch (Mage_Core_Exception $e) {
227: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
228: } catch (Exception $e) {
229: $this->_message($this->__('Unable to collect cart totals.'), self::MESSAGE_STATUS_ERROR);
230: Mage::logException($e);
231: }
232: }
233:
234: 235: 236: 237: 238:
239: public function saveOrderAction()
240: {
241: if (!$this->getRequest()->isPost()) {
242: $this->_message($this->__('Specified invalid data.'), self::MESSAGE_STATUS_ERROR);
243: return;
244: }
245:
246: try {
247: 248: 249:
250: $this->_initCheckout();
251:
252: 253: 254:
255: $data = $this->getRequest()->getPost('payment', array());
256:
257: if (Mage::getSingleton('customer/session')->isLoggedIn()) {
258: $data['payer'] = Mage::getSingleton('customer/session')->getCustomer()->getEmail();
259: }
260:
261: $this->_checkout->savePayment($data);
262:
263: 264: 265:
266: $this->_checkout->saveOrder();
267:
268: 269: 270:
271:
272: $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
273: $message->addChild('status', self::MESSAGE_STATUS_SUCCESS);
274:
275: $orderId = $this->_checkout->getLastOrderId();
276:
277: $text = $this->__('Thank you for your purchase! ');
278: $text .= $this->__('Your order # is: %s. ', $orderId);
279: $text .= $this->__('You will receive an order confirmation email with details of your order and a link to track its progress.');
280: $message->addChild('text', $text);
281:
282: $message->addChild('order_id', $orderId);
283: $this->getResponse()->setBody($message->asNiceXml());
284: return;
285: } catch (Mage_Core_Exception $e) {
286: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
287: } catch (Exception $e) {
288: $this->_message($this->__('Unable to place the order.'), self::MESSAGE_STATUS_ERROR);
289: Mage::logException($e);
290: }
291: }
292:
293: 294: 295: 296: 297: 298:
299: protected function _initCheckout()
300: {
301:
302: $quote = $this->_getQuote();
303: if (!$quote->hasItems() || $quote->getHasError()) {
304: Mage::throwException($this->__('Unable to initialize MEP Checkout.'));
305: }
306: if (!$quote->validateMinimumAmount()) {
307: $error = Mage::getStoreConfig('sales/minimum_order/error_message');
308: Mage::throwException($error);
309: }
310: $this->_getCheckoutSession()->setCartWasUpdated(false);
311:
312: $this->_checkout = Mage::getSingleton('xmlconnect/paypal_mep_checkout', array('quote' => $quote));
313: }
314:
315: 316: 317: 318: 319:
320: protected function _getCheckoutSession()
321: {
322: return Mage::getSingleton('checkout/session');
323: }
324:
325: 326: 327: 328: 329:
330: protected function _getQuote()
331: {
332: if (!$this->_quote) {
333: $this->_quote = $this->_getCheckoutSession()->getQuote();
334: }
335: return $this->_quote;
336: }
337: }
338: