PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/community/Phoenix/Moneybookers/Model/Event.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 270 lines | 162 code | 22 blank | 86 comment | 24 complexity | 767af04bd7f1a84c4cd91db1391aa71f 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. * @category Phoenix
  16. * @package Phoenix_Moneybookers
  17. * @copyright Copyright (c) 2009 Phoenix Medien GmbH & Co. KG (http://www.phoenix-medien.de)
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. */
  20. /**
  21. * Moneybookers notification processor model
  22. */
  23. class Phoenix_Moneybookers_Model_Event
  24. {
  25. const MONEYBOOKERS_STATUS_FAIL = -2;
  26. const MONEYBOOKERS_STATUS_CANCEL = -1;
  27. const MONEYBOOKERS_STATUS_PENDING = 0;
  28. const MONEYBOOKERS_STATUS_SUCCESS = 2;
  29. /*
  30. * @param Mage_Sales_Model_Order
  31. */
  32. protected $_order = null;
  33. /**
  34. * Event request data
  35. * @var array
  36. */
  37. protected $_eventData = array();
  38. /**
  39. * Enent request data setter
  40. * @param array $data
  41. * @return Phoenix_Moneybookers_Model_Event
  42. */
  43. public function setEventData(array $data)
  44. {
  45. $this->_eventData = $data;
  46. return $this;
  47. }
  48. /**
  49. * Event request data getter
  50. * @param string $key
  51. * @return array|string
  52. */
  53. public function getEventData($key = null)
  54. {
  55. if (null === $key) {
  56. return $this->_eventData;
  57. }
  58. return isset($this->_eventData[$key]) ? $this->_eventData[$key] : null;
  59. }
  60. /**
  61. * Get singleton of Checkout Session Model
  62. *
  63. * @return Mage_Checkout_Model_Session
  64. */
  65. protected function _getCheckout()
  66. {
  67. return Mage::getSingleton('checkout/session');
  68. }
  69. /**
  70. * Process status notification from Monebookers server
  71. *
  72. * @return String
  73. */
  74. public function processStatusEvent()
  75. {
  76. try {
  77. $params = $this->_validateEventData();
  78. $msg = '';
  79. switch($params['status']) {
  80. case self::MONEYBOOKERS_STATUS_FAIL: //fail
  81. $msg = Mage::helper('moneybookers')->__('Payment failed.');
  82. $this->_processCancel($msg);
  83. break;
  84. case self::MONEYBOOKERS_STATUS_CANCEL: //cancel
  85. $msg = Mage::helper('moneybookers')->__('Payment was canceled.');
  86. $this->_processCancel($msg);
  87. break;
  88. case self::MONEYBOOKERS_STATUS_PENDING: //pending
  89. $msg = Mage::helper('moneybookers')->__('Pending bank transfer created.');
  90. $this->_processSale($params['status'], $msg);
  91. break;
  92. case self::MONEYBOOKERS_STATUS_SUCCESS: //ok
  93. $msg = Mage::helper('moneybookers')->__('The amount has been authorized and captured by Moneybookers.');
  94. $this->_processSale($params['status'], $msg);
  95. break;
  96. }
  97. return $msg;
  98. } catch (Mage_Core_Exception $e) {
  99. return $e->getMessage();
  100. } catch(Exception $e) {
  101. Mage::logException($e);
  102. }
  103. return;
  104. }
  105. /**
  106. * Process cancelation
  107. */
  108. public function cancelEvent() {
  109. try {
  110. $this->_validateEventData(false);
  111. $this->_processCancel('Payment was canceled.');
  112. return Mage::helper('moneybookers')->__('The order has been canceled.');
  113. } catch (Mage_Core_Exception $e) {
  114. return $e->getMessage();
  115. } catch(Exception $e) {
  116. Mage::logException($e);
  117. }
  118. return '';
  119. }
  120. /**
  121. * Validate request and return QuoteId
  122. * Can throw Mage_Core_Exception and Exception
  123. *
  124. * @return int
  125. */
  126. public function successEvent(){
  127. $this->_validateEventData(false);
  128. return $this->_order->getQuoteId();
  129. }
  130. /**
  131. * Processed order cancelation
  132. * @param string $msg Order history message
  133. */
  134. protected function _processCancel($msg)
  135. {
  136. $this->_order->cancel();
  137. $this->_order->addStatusToHistory(Mage_Sales_Model_Order::STATE_CANCELED, $msg);
  138. $this->_order->save();
  139. }
  140. /**
  141. * Processes payment confirmation, creates invoice if necessary, updates order status,
  142. * sends order confirmation to customer
  143. * @param string $msg Order history message
  144. */
  145. protected function _processSale($status, $msg)
  146. {
  147. switch ($status) {
  148. case self::MONEYBOOKERS_STATUS_SUCCESS:
  149. $this->_createInvoice();
  150. $this->_order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, $msg);
  151. // save transaction ID
  152. $this->_order->getPayment()->setLastTransId($this->getEventData('mb_transaction_id'));
  153. // send new order email
  154. $this->_order->sendNewOrderEmail();
  155. $this->_order->setEmailSent(true);
  156. break;
  157. case self::MONEYBOOKERS_STATUS_PENDING:
  158. $this->_order->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT, true, $msg);
  159. // save transaction ID
  160. $this->_order->getPayment()->setLastTransId($this->getEventData('mb_transaction_id'));
  161. break;
  162. }
  163. $this->_order->save();
  164. }
  165. /**
  166. * Builds invoice for order
  167. */
  168. protected function _createInvoice()
  169. {
  170. if (!$this->_order->canInvoice()) {
  171. return;
  172. }
  173. $invoice = $this->_order->prepareInvoice();
  174. $invoice->register()->capture();
  175. $this->_order->addRelatedObject($invoice);
  176. }
  177. /**
  178. * Checking returned parameters
  179. * Thorws Mage_Core_Exception if error
  180. * @param bool $fullCheck Whether to make additional validations such as payment status, transaction signature etc.
  181. *
  182. * @return array $params request params
  183. */
  184. protected function _validateEventData($fullCheck = true)
  185. {
  186. // get request variables
  187. $params = $this->_eventData;
  188. if (empty($params)) {
  189. Mage::throwException('Request does not contain any elements.');
  190. }
  191. // check order ID
  192. if (empty($params['transaction_id'])
  193. || ($fullCheck == false && $this->_getCheckout()->getMoneybookersRealOrderId() != $params['transaction_id'])
  194. ) {
  195. Mage::throwException('Missing or invalid order ID.');
  196. }
  197. // load order for further validation
  198. $this->_order = Mage::getModel('sales/order')->loadByIncrementId($params['transaction_id']);
  199. if (!$this->_order->getId()) {
  200. Mage::throwException('Order not found.');
  201. }
  202. if (0 !== strpos($this->_order->getPayment()->getMethodInstance()->getCode(), 'moneybookers_')) {
  203. Mage::throwException('Unknown payment method.');
  204. }
  205. // make additional validation
  206. if ($fullCheck) {
  207. // check payment status
  208. if (empty($params['status'])) {
  209. Mage::throwException('Unknown payment status.');
  210. }
  211. // check transaction signature
  212. if (empty($params['md5sig'])) {
  213. Mage::throwException('Invalid transaction signature.');
  214. }
  215. $checkParams = array('merchant_id', 'transaction_id', 'secret', 'mb_amount', 'mb_currency', 'status');
  216. $md5String = '';
  217. foreach ($checkParams as $key) {
  218. if ($key == 'merchant_id') {
  219. $md5String .= Mage::getStoreConfig(Phoenix_Moneybookers_Helper_Data::XML_PATH_CUSTOMER_ID,
  220. $this->_order->getStoreId()
  221. );
  222. } elseif ($key == 'secret') {
  223. $secretKey = Mage::getStoreConfig(
  224. Phoenix_Moneybookers_Helper_Data::XML_PATH_SECRET_KEY,
  225. $this->_order->getStoreId()
  226. );
  227. if (empty($secretKey)) {
  228. Mage::throwException('Secret key is empty.');
  229. }
  230. $md5String .= strtoupper(md5($secretKey));
  231. } elseif (isset($params[$key])) {
  232. $md5String .= $params[$key];
  233. }
  234. }
  235. $md5String = strtoupper(md5($md5String));
  236. if ($md5String != $params['md5sig']) {
  237. Mage::throwException('Hash is not valid.');
  238. }
  239. // check transaction amount if currency matches
  240. if ($this->_order->getOrderCurrencyCode() == $params['mb_currency']) {
  241. if (round($this->_order->getGrandTotal(), 2) != $params['mb_amount']) {
  242. Mage::throwException('Transaction amount does not match.');
  243. }
  244. }
  245. }
  246. return $params;
  247. }
  248. }