PageRenderTime 65ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/XmlConnect/Model/Paypal/Mep/Checkout.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 340 lines | 174 code | 35 blank | 131 comment | 28 complexity | 9d907b05a4efd0a76a55335ec74767da MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_XmlConnect
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Wrapper that performs Paypal MEP and Checkout communication
  28. *
  29. * @category Mage
  30. * @package Mage_XmlConnect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_XmlConnect_Model_Paypal_Mep_Checkout
  34. {
  35. /**#@+
  36. * Keys for passthrough variables in sales/quote_payment and sales/order_payment
  37. * Uses additional_information as storage
  38. *
  39. * @var string
  40. */
  41. const PAYMENT_INFO_PAYER_EMAIL = 'paypal_payer_email';
  42. const PAYMENT_INFO_TRANSACTION_ID = 'paypal_mep_checkout_transaction_id';
  43. /**#@-*/
  44. /**
  45. * Payment method type
  46. *
  47. * @var string
  48. */
  49. protected $_methodType = Mage_XmlConnect_Model_Payment_Method_Paypal_Mep::MEP_METHOD_CODE;
  50. /**
  51. * Quote model
  52. *
  53. * @var Mage_Sales_Model_Quote
  54. */
  55. protected $_quote = null;
  56. /**
  57. * Checkout session model
  58. *
  59. * @var Mage_Checkout_Model_Session
  60. */
  61. protected $_checkoutSession;
  62. /**
  63. * XmlConnect default helper
  64. *
  65. * @var Mage_XmlConnect_Helper_Data
  66. */
  67. protected $_helper;
  68. /**
  69. * Set quote instances
  70. *
  71. * @throws Mage_Core_Exception
  72. * @param array $params
  73. * @return void
  74. */
  75. public function __construct($params = array())
  76. {
  77. $this->_checkoutSession = Mage::getSingleton('checkout/session');
  78. if (isset($params['quote']) && $params['quote'] instanceof Mage_Sales_Model_Quote) {
  79. $this->_quote = $params['quote'];
  80. } else {
  81. Mage::throwException(Mage::helper('xmlconnect')->__('Quote instance is required.'));
  82. }
  83. }
  84. /**
  85. * Prepare quote, reserve order ID for specified quote
  86. *
  87. * @return string
  88. */
  89. public function initCheckout()
  90. {
  91. $this->_quote->reserveOrderId()->save();
  92. /**
  93. * Reset multishipping flag before any manipulations with quote address
  94. * addAddress method for quote object related on this flag
  95. */
  96. if ($this->_quote->getIsMultiShipping()) {
  97. $this->_quote->setIsMultiShipping(false);
  98. $this->_quote->save();
  99. }
  100. /*
  101. * want to load the correct customer information by assigning to address
  102. * instead of just loading from sales/quote_address
  103. */
  104. $customer = Mage::getSingleton('customer/session')->getCustomer();
  105. if ($customer) {
  106. $this->_quote->assignCustomer($customer);
  107. }
  108. if (!Mage::getSingleton('customer/session')->isLoggedIn()
  109. && Mage::getSingleton('checkout/session')->getQuote()->isAllowedGuestCheckout()) {
  110. $this->_prepareGuestQuote();
  111. }
  112. return $this->_quote->getReservedOrderId();
  113. }
  114. /**
  115. * Save shipping and billing address information to quote
  116. *
  117. * @param array $data
  118. * @return array
  119. */
  120. public function saveShipping($data)
  121. {
  122. if (empty($data)) {
  123. return array('error' => 1, 'message' => Mage::helper('xmlconnect')->__('Invalid data.'));
  124. }
  125. $address = $this->_quote->getBillingAddress();
  126. $this->_applyCountryWorkarounds($data);
  127. if (!in_array($data['country_id'], Mage::getModel('paypal/config')->getSupportedBuyerCountryCodes())) {
  128. return array(
  129. 'error' => 1,
  130. 'message' => Mage::helper('xmlconnect')->__('Buyer country is not allowed by store.')
  131. );
  132. }
  133. if (empty($data['firstname']) && empty($data['lastname'])) {
  134. if (Mage::getSingleton('customer/session')->isLoggedIn()) {
  135. $customer = Mage::getSingleton('customer/session')->getCustomer();
  136. $data['firstname'] = $customer->getFirstname();
  137. $data['lastname'] = $customer->getLastname();
  138. } else {
  139. $data['firstname'] = Mage::helper('xmlconnect')->__('Guest');
  140. $data['lastname'] = Mage::helper('xmlconnect')->__('Guest');
  141. }
  142. }
  143. $address->addData($data);
  144. $this->_ignoreAddressValidation();
  145. $address->implodeStreetAddress();
  146. if (!$this->_quote->isVirtual()) {
  147. $billing = clone $address;
  148. $billing->unsAddressId()->unsAddressType();
  149. $shipping = $this->_quote->getShippingAddress();
  150. $shippingMethod = $shipping->getShippingMethod();
  151. $shipping->addData($billing->getData())
  152. ->setSameAsBilling(1)
  153. ->setShippingMethod($shippingMethod)
  154. ->setCollectShippingRates(true);
  155. }
  156. $this->_quote->collectTotals()->save();
  157. return array();
  158. }
  159. /**
  160. * Specify quote shipping method
  161. *
  162. * @param string $shippingMethod
  163. * @return array
  164. */
  165. public function saveShippingMethod($shippingMethod)
  166. {
  167. if (empty($shippingMethod)) {
  168. return array('error' => 1, 'message' => Mage::helper('xmlconnect')->__('Invalid shipping method.'));
  169. }
  170. $rate = $this->_quote->getShippingAddress()->getShippingRateByCode($shippingMethod);
  171. if (!$rate) {
  172. return array('error' => 1, 'message' => Mage::helper('xmlconnect')->__('Invalid shipping method.'));
  173. }
  174. if (!$this->_quote->getIsVirtual() && $shippingAddress = $this->_quote->getShippingAddress()) {
  175. if ($shippingMethod != $shippingAddress->getShippingMethod()) {
  176. $this->_ignoreAddressValidation();
  177. $this->_quote->getShippingAddress()
  178. ->setShippingMethod($shippingMethod);
  179. $this->_quote->collectTotals()
  180. ->save();
  181. }
  182. }
  183. return array();
  184. }
  185. /**
  186. * Specify quote payment method
  187. *
  188. * @param array $data
  189. * @return array
  190. */
  191. public function savePayment($data)
  192. {
  193. if ($this->_quote->isVirtual()) {
  194. $this->_quote->getBillingAddress()->setPaymentMethod($this->_methodType);
  195. } else {
  196. $this->_quote->getShippingAddress()->setPaymentMethod($this->_methodType);
  197. }
  198. $payment = $this->_quote->getPayment();
  199. $data['method'] = $this->_methodType;
  200. $payment->importData($data);
  201. $email = isset($data['payer']) ? $data['payer'] : null;
  202. $payment->setAdditionalInformation(self::PAYMENT_INFO_PAYER_EMAIL, $email);
  203. $payment->setAdditionalInformation(
  204. self::PAYMENT_INFO_TRANSACTION_ID,
  205. isset($data['transaction_id']) ? $data['transaction_id'] : null
  206. );
  207. $this->_quote->setCustomerEmail($email);
  208. $this->_quote->collectTotals()->save();
  209. return array();
  210. }
  211. /**
  212. * Place the order when customer returned from paypal
  213. * Until this moment all quote data must be valid
  214. *
  215. * @return array
  216. */
  217. public function saveOrder()
  218. {
  219. $this->_ignoreAddressValidation();
  220. $order = Mage::getModel('sales/service_quote', $this->_quote)->submit();
  221. $this->_quote->save();
  222. /**
  223. * Prepare session to success or cancellation page
  224. */
  225. $quoteId = $this->_quote->getId();
  226. $this->_getCheckoutSession()
  227. ->setLastQuoteId($quoteId)
  228. ->setLastSuccessQuoteId($quoteId)
  229. ->setLastOrderId($order->getId())
  230. ->setLastRealOrderId($order->getIncrementId());
  231. if ($order->getState() == Mage_Sales_Model_Order::STATE_PROCESSING) {
  232. try {
  233. $order->sendNewOrderEmail();
  234. } catch (Exception $e) {
  235. Mage::logException($e);
  236. }
  237. }
  238. return array();
  239. }
  240. /**
  241. * Get last order increment id by order id
  242. *
  243. * @return string
  244. */
  245. public function getLastOrderId()
  246. {
  247. $lastId = $this->_getCheckoutSession()->getLastOrderId();
  248. $orderId = false;
  249. if ($lastId) {
  250. $order = Mage::getModel('sales/order');
  251. $order->load($lastId);
  252. $orderId = $order->getIncrementId();
  253. }
  254. return $orderId;
  255. }
  256. /**
  257. * Make sure addresses will be saved without validation errors
  258. *
  259. * @return void
  260. */
  261. protected function _ignoreAddressValidation()
  262. {
  263. $this->_quote->getBillingAddress()->setShouldIgnoreValidation(true);
  264. if (!$this->_quote->getIsVirtual()) {
  265. $this->_quote->getShippingAddress()->setShouldIgnoreValidation(true);
  266. }
  267. }
  268. /**
  269. * Get frontend checkout session object
  270. *
  271. * @return Mage_Checkout_Model_Session
  272. */
  273. protected function _getCheckoutSession()
  274. {
  275. return $this->_checkoutSession;
  276. }
  277. /**
  278. * Prepare quote for guest checkout order submit
  279. *
  280. * @return Mage_XmlConnect_Model_Paypal_Mep_Checkout
  281. */
  282. protected function _prepareGuestQuote()
  283. {
  284. $quote = $this->_quote;
  285. $quote->setCustomerId(null)
  286. ->setCustomerIsGuest(true)
  287. ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
  288. return $this;
  289. }
  290. /**
  291. * Adopt specified request array from PayPal
  292. *
  293. * @param array $request
  294. * @return void
  295. */
  296. protected function _applyCountryWorkarounds(&$request)
  297. {
  298. $request['country_id'] = isset($request['country_id']) ? trim($request['country_id']) : null;
  299. if (empty($request['country_id'])) {
  300. $request['country_id'] = strtoupper(Mage::getStoreConfig('general/country/default'));
  301. } else {
  302. $request['country_id'] = strtoupper($request['country_id']);
  303. }
  304. }
  305. }