PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Authorizenet/controllers/Directpost/PaymentController.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 200 lines | 121 code | 11 blank | 68 comment | 14 complexity | a7692b6dc5c40240ff779f2c8b14be63 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_Authorizenet
  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. * DirtectPost Payment Controller
  28. *
  29. * @category Mage
  30. * @package Mage_Authorizenet
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Authorizenet_Directpost_PaymentController extends Mage_Core_Controller_Front_Action
  34. {
  35. /**
  36. * @return Mage_Checkout_Model_Session
  37. */
  38. protected function _getCheckout()
  39. {
  40. return Mage::getSingleton('checkout/session');
  41. }
  42. /**
  43. * Get session model
  44. * @return Mage_Authorizenet_Model_Directpost_Session
  45. */
  46. protected function _getDirectPostSession()
  47. {
  48. return Mage::getSingleton('authorizenet/directpost_session');
  49. }
  50. /**
  51. * Get iframe block instance
  52. *
  53. * @return Mage_Authorizenet_Block_Directpost_Iframe
  54. */
  55. protected function _getIframeBlock()
  56. {
  57. return $this->getLayout()->createBlock('directpost/iframe');
  58. }
  59. /**
  60. * Response action.
  61. * Action for Authorize.net SIM Relay Request.
  62. */
  63. public function responseAction()
  64. {
  65. $data = $this->getRequest()->getPost();
  66. /* @var $paymentMethod Mage_Authorizenet_Model_DirectPost */
  67. $paymentMethod = Mage::getModel('authorizenet/directpost');
  68. $result = array();
  69. if (!empty($data['x_invoice_num'])) {
  70. $result['x_invoice_num'] = $data['x_invoice_num'];
  71. }
  72. try {
  73. if (!empty($data['store_id'])) {
  74. $paymentMethod->setStore($data['store_id']);
  75. }
  76. $paymentMethod->process($data);
  77. $result['success'] = 1;
  78. }
  79. catch (Mage_Core_Exception $e) {
  80. Mage::logException($e);
  81. $result['success'] = 0;
  82. $result['error_msg'] = $e->getMessage();
  83. }
  84. catch (Exception $e) {
  85. Mage::logException($e);
  86. $result['success'] = 0;
  87. $result['error_msg'] = $this->__('There was an error processing your order. Please contact us or try again later.');
  88. }
  89. if (!empty($data['controller_action_name'])) {
  90. if (!empty($data['key'])) {
  91. $result['key'] = $data['key'];
  92. }
  93. $result['controller_action_name'] = $data['controller_action_name'];
  94. $result['is_secure'] = isset($data['is_secure']) ? $data['is_secure'] : false;
  95. $params['redirect'] = Mage::helper('authorizenet')->getRedirectIframeUrl($result);
  96. }
  97. $block = $this->_getIframeBlock()->setParams($params);
  98. $this->getResponse()->setBody($block->toHtml());
  99. }
  100. /**
  101. * Retrieve params and put javascript into iframe
  102. *
  103. */
  104. public function redirectAction()
  105. {
  106. $redirectParams = $this->getRequest()->getParams();
  107. $params = array();
  108. if (!empty($redirectParams['success'])
  109. && isset($redirectParams['x_invoice_num'])
  110. && isset($redirectParams['controller_action_name'])
  111. ) {
  112. $this->_getDirectPostSession()->unsetData('quote_id');
  113. $params['redirect_parent'] = Mage::helper('authorizenet')->getSuccessOrderUrl($redirectParams);
  114. }
  115. if (!empty($redirectParams['error_msg'])) {
  116. $cancelOrder = empty($redirectParams['x_invoice_num']);
  117. $this->_returnCustomerQuote($cancelOrder, $redirectParams['error_msg']);
  118. }
  119. $block = $this->_getIframeBlock()->setParams(array_merge($params, $redirectParams));
  120. $this->getResponse()->setBody($block->toHtml());
  121. }
  122. /**
  123. * Send request to authorize.net
  124. *
  125. */
  126. public function placeAction()
  127. {
  128. $paymentParam = $this->getRequest()->getParam('payment');
  129. $controller = $this->getRequest()->getParam('controller');
  130. if (isset($paymentParam['method'])) {
  131. $params = Mage::helper('authorizenet')->getSaveOrderUrlParams($controller);
  132. $this->_getDirectPostSession()->setQuoteId($this->_getCheckout()->getQuote()->getId());
  133. $this->_forward(
  134. $params['action'],
  135. $params['controller'],
  136. $params['module'],
  137. $this->getRequest()->getParams()
  138. );
  139. } else {
  140. $result = array(
  141. 'error_messages' => $this->__('Please, choose payment method'),
  142. 'goto_section' => 'payment'
  143. );
  144. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
  145. }
  146. }
  147. /**
  148. * Return customer quote by ajax
  149. *
  150. */
  151. public function returnQuoteAction()
  152. {
  153. $this->_returnCustomerQuote();
  154. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode(array('success' => 1)));
  155. }
  156. /**
  157. * Return customer quote
  158. *
  159. * @param bool $cancelOrder
  160. * @param string $errorMsg
  161. */
  162. protected function _returnCustomerQuote($cancelOrder = false, $errorMsg = '')
  163. {
  164. $incrementId = $this->_getDirectPostSession()->getLastOrderIncrementId();
  165. if ($incrementId &&
  166. $this->_getDirectPostSession()
  167. ->isCheckoutOrderIncrementIdExist($incrementId)
  168. ) {
  169. /* @var $order Mage_Sales_Model_Order */
  170. $order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);
  171. if ($order->getId()) {
  172. $quote = Mage::getModel('sales/quote')
  173. ->load($order->getQuoteId());
  174. if ($quote->getId()) {
  175. $quote->setIsActive(1)
  176. ->setReservedOrderId(NULL)
  177. ->save();
  178. $this->_getCheckout()->replaceQuote($quote);
  179. }
  180. $this->_getDirectPostSession()->removeCheckoutOrderIncrementId($incrementId);
  181. $this->_getDirectPostSession()->unsetData('quote_id');
  182. if ($cancelOrder) {
  183. $order->registerCancellation($errorMsg)->save();
  184. }
  185. }
  186. }
  187. }
  188. }