PageRenderTime 1522ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Paybox/controllers/SystemController.php

https://github.com/jweiss/Magento-Example
PHP | 364 lines | 227 code | 52 blank | 85 comment | 31 complexity | 32456866b47ec453b12f30b0e02cd4a5 MD5 | raw file
  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_Paybox
  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. * Paybox System Checkout Controller
  28. *
  29. * @category Mage
  30. * @package Mage_Paybox
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Paybox_SystemController extends Mage_Core_Controller_Front_Action
  34. {
  35. protected $_payboxResponse = null;
  36. protected $_responseStatus = false;
  37. /**
  38. * seting response after returning from paybox
  39. *
  40. * @param array $response
  41. * @return object $this
  42. */
  43. protected function setPayboxResponse($response)
  44. {
  45. if (count($response)) {
  46. $this->_payboxResponse = $response;
  47. }
  48. return $this;
  49. }
  50. /**
  51. * Get System Model
  52. *
  53. * @return Mage_Paybox_Model_System
  54. */
  55. public function getModel()
  56. {
  57. return Mage::getSingleton('paybox/system');
  58. }
  59. /**
  60. * Get Checkout Singleton
  61. *
  62. * @return Mage_Checkout_Model_Session
  63. */
  64. public function getCheckout()
  65. {
  66. return Mage::getSingleton('checkout/session');
  67. }
  68. /**
  69. * Redirect action. Redirect customer to Paybox
  70. *
  71. */
  72. public function redirectAction()
  73. {
  74. $session = $this->getCheckout();
  75. $session->setPayboxQuoteId($session->getQuoteId());
  76. $order = Mage::getModel('sales/order');
  77. $order->loadByIncrementId($session->getLastRealOrderId());
  78. $order->addStatusToHistory($order->getStatus(), $this->__('The customer was redirected to Paybox'));
  79. $order->save();
  80. $session->setPayboxOrderId(Mage::helper('core')->encrypt($session->getLastRealOrderId()));
  81. $session->setPayboxPaymentAction(
  82. $order->getPayment()->getMethodInstance()->getPaymentAction()
  83. );
  84. $this->getResponse()->setBody(
  85. $this->getLayout()
  86. ->createBlock('paybox/system_redirect')
  87. ->setOrder($order)
  88. ->toHtml()
  89. );
  90. $session->unsQuoteId();
  91. }
  92. /**
  93. * Customer returning to this action if payment was successe
  94. */
  95. public function successAction()
  96. {
  97. $this->setPayboxResponse($this->getRequest()->getParams());
  98. if ($this->_checkResponse()) {
  99. $order = Mage::getModel('sales/order');
  100. $order->loadByIncrementId($this->_payboxResponse['ref']);
  101. if (!$order->getId()) {
  102. Mage::throwException($this->__('There are no orders.'));
  103. }
  104. if (Mage::helper('core')->decrypt($this->getCheckout()->getPayboxOrderId()) != $this->_payboxResponse['ref']) {
  105. Mage::throwException($this->__('Order does not match.'));
  106. }
  107. $this->getCheckout()->unsPayboxOrderId();
  108. if (($order->getBaseGrandTotal()*100) != $this->_payboxResponse['amount']) {
  109. Mage::throwException($this->__('Amount does not match.'));
  110. }
  111. if ($this->_payboxResponse['error'] == '00000') {
  112. $order->addStatusToHistory($order->getStatus(), $this->__('The customer has returned from Paybox.'));
  113. $redirectTo = 'checkout/onepage/success';
  114. if ($this->getCheckout()->getPayboxPaymentAction() == Mage_Paybox_Model_System::PBX_PAYMENT_ACTION_ATHORIZE_CAPTURE) {
  115. $this->getCheckout()->unsPayboxPaymentAction();
  116. $order->getPayment()
  117. ->getMethodInstance()
  118. ->setTransactionId($this->_payboxResponse['trans']);
  119. if ($this->_createInvoice($order)) {
  120. $order->addStatusToHistory($order->getStatus(), $this->__('The invoice has been created.'));
  121. } else {
  122. $order->addStatusToHistory($order->getStatus(), $this->__('Cannot create the invoice.'));
  123. $redirectTo = '*/*/failure';
  124. }
  125. }
  126. $session = $this->getCheckout();
  127. $session->setQuoteId($session->getPayboxQuoteId(true));
  128. $session->getQuote()->setIsActive(false)->save();
  129. $session->unsPayboxQuoteId();
  130. } else {
  131. $redirectTo = '*/*/failure';
  132. $order->cancel();
  133. $order->addStatusToHistory($order->getStatus(), $this->__('The customer was rejected by Paybox.'));
  134. }
  135. $order->sendNewOrderEmail();
  136. $order->save();
  137. $this->_redirect($redirectTo);
  138. } else {
  139. $this->norouteAction();
  140. return;
  141. }
  142. }
  143. /**
  144. * Action when payment was declined by Paybox
  145. */
  146. public function refuseAction()
  147. {
  148. $this->setPayboxResponse($this->getRequest()->getParams());
  149. if ($this->_checkResponse()) {
  150. $this->getCheckout()->unsPayboxQuoteId();
  151. $this->getCheckout()->setPayboxErrorMessage('Order was canceled by Paybox');
  152. $order = Mage::getModel('sales/order')
  153. ->loadByIncrementId($this->_payboxResponse['ref']);
  154. $order->cancel();
  155. $order->addStatusToHistory($order->getStatus(), $this->__('The customer was refused by Paybox.'));
  156. $order->save();
  157. $this->_redirect('*/*/failure');
  158. } else {
  159. $this->norouteAction();
  160. return;
  161. }
  162. }
  163. /**
  164. * Action when customer cancele payment or press button to back to shop
  165. */
  166. public function declineAction()
  167. {
  168. $this->setPayboxResponse($this->getRequest()->getParams());
  169. if ($this->_checkResponse()) {
  170. $order = Mage::getModel('sales/order')
  171. ->loadByIncrementId($this->_payboxResponse['ref']);
  172. $order->cancel();
  173. $order->addStatusToHistory($order->getStatus(), $this->__('The order was canceled by the customer.'));
  174. $order->save();
  175. $session = $this->getCheckout();
  176. $session->setQuoteId($session->getPayboxQuoteId(true));
  177. $session->getQuote()->setIsActive(false)->save();
  178. $session->unsPayboxQuoteId();
  179. $this->_redirect('checkout/cart');
  180. } else {
  181. $this->norouteAction();
  182. return;
  183. }
  184. }
  185. /**
  186. * Redirect action. Redirect to Paybox using commandline mode
  187. *
  188. */
  189. public function commandlineAction()
  190. {
  191. $session = $this->getCheckout();
  192. $session->setPayboxQuoteId($session->getQuoteId());
  193. $order = Mage::getModel('sales/order')
  194. ->loadByIncrementId($this->getCheckout()->getLastRealOrderId());
  195. $order->addStatusToHistory(
  196. $order->getStatus(), $this->__('The customer was redirected to Paybox using \'command line\' mode.')
  197. );
  198. $order->save();
  199. $session->setPayboxOrderId(Mage::helper('core')->encrypt($session->getLastRealOrderId()));
  200. $session->setPayboxPaymentAction(
  201. $order->getPayment()->getMethodInstance()->getPaymentAction()
  202. );
  203. $session->unsQuoteId();
  204. $payment = $order->getPayment()->getMethodInstance();
  205. $fieldsArr = $payment->getFormFields();
  206. $paramStr = '';
  207. foreach ($fieldsArr as $k => $v) {
  208. $paramStr .= $k.'='.$v.' ';
  209. }
  210. $paramStr = str_replace(';', '\;', $paramStr);
  211. $result = shell_exec(Mage::getBaseDir().'/'.$this->getModel()->getPayboxFile().' '.$paramStr);
  212. if (isset($fieldsArr['PBX_PING']) && $fieldsArr['PBX_PING'] == '1') {
  213. $fieldsArr['PBX_PING'] = '0';
  214. $fieldsArr['PBX_PAYBOX'] = trim(substr($result, strpos($result, 'http')));
  215. $paramStr = '';
  216. foreach ($fieldsArr as $k => $v) {
  217. $paramStr .= $k.'='.$v.' ';
  218. }
  219. $paramStr = str_replace(';', '\;', $paramStr);
  220. $result = shell_exec(Mage::getBaseDir().'/'.$this->getModel()->getPayboxFile().' '.$paramStr);
  221. }
  222. $this->loadLayout(false);
  223. $this->getResponse()->setBody($result);
  224. $this->renderLayout();
  225. }
  226. /**
  227. * Error action. If request params to Paybox has mistakes
  228. *
  229. */
  230. public function errorAction()
  231. {
  232. if (!$this->getCheckout()->getPayboxQuoteId()) {
  233. $this->norouteAction();
  234. return;
  235. }
  236. $session = $this->getCheckout();
  237. $session->setQuoteId($session->getPayboxQuoteId(true));
  238. $session->getQuote()->setIsActive(false)->save();
  239. $session->unsPayboxQuoteId();
  240. if (!$this->getRequest()->getParam('NUMERR')) {
  241. $this->norouteAction();
  242. return;
  243. }
  244. $this->loadLayout();
  245. $this->getCheckout()
  246. ->setPayboxErrorNumber(
  247. $this->getRequest()->getParam('NUMERR')
  248. );
  249. $this->renderLayout();
  250. }
  251. /**
  252. * Failure action.
  253. * Displaying information if customer was redirecting to cancel or decline actions
  254. *
  255. */
  256. public function failureAction()
  257. {
  258. if (!$this->getCheckout()->getPayboxErrorMessage()) {
  259. $this->norouteAction();
  260. return;
  261. }
  262. $this->loadLayout();
  263. $this->renderLayout();
  264. }
  265. /**
  266. * Checking response and Paybox session variables
  267. *
  268. * @return unknown
  269. */
  270. protected function _checkResponse()
  271. {
  272. if (!$this->getCheckout()->getPayboxQuoteId()) {
  273. $this->norouteAction();
  274. return;
  275. }
  276. if (!$this->getCheckout()->getPayboxOrderId()) {
  277. $this->norouteAction();
  278. return;
  279. }
  280. if (!$this->getCheckout()->getPayboxPaymentAction()) {
  281. $this->norouteAction();
  282. return;
  283. }
  284. if (!$this->_payboxResponse) {
  285. return false;
  286. }
  287. //check for valid response
  288. if ($this->getModel()->checkResponse($this->_payboxResponse)) {
  289. return true;
  290. }
  291. return true;
  292. }
  293. /**
  294. * Creating invoice
  295. *
  296. * @param Mage_Sales_Model_Order $order
  297. * @return bool
  298. */
  299. protected function _createInvoice(Mage_Sales_Model_Order $order)
  300. {
  301. if ($order->canInvoice()) {
  302. $invoice = $order->prepareInvoice();
  303. $invoice->register()->capture();
  304. Mage::getModel('core/resource_transaction')
  305. ->addObject($invoice)
  306. ->addObject($invoice->getOrder())
  307. ->save();
  308. return true;
  309. }
  310. return false;
  311. }
  312. }