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_Authorizenet_Directpost_PaymentController extends Mage_Core_Controller_Front_Action
35: {
36: 37: 38:
39: protected function _getCheckout()
40: {
41: return Mage::getSingleton('checkout/session');
42: }
43:
44: 45: 46: 47: 48:
49: protected function _getDirectPostSession()
50: {
51: return Mage::getSingleton('authorizenet/directpost_session');
52: }
53:
54: 55: 56: 57: 58:
59: protected function _getIframeBlock()
60: {
61: return $this->getLayout()->createBlock('directpost/iframe');
62: }
63:
64: 65: 66: 67:
68: public function responseAction()
69: {
70: $data = $this->getRequest()->getPost();
71:
72: $paymentMethod = Mage::getModel('authorizenet/directpost');
73:
74: $result = array();
75: if (!empty($data['x_invoice_num'])) {
76: $result['x_invoice_num'] = $data['x_invoice_num'];
77: }
78:
79: try {
80: if (!empty($data['store_id'])) {
81: $paymentMethod->setStore($data['store_id']);
82: }
83: $paymentMethod->process($data);
84: $result['success'] = 1;
85: }
86: catch (Mage_Core_Exception $e) {
87: Mage::logException($e);
88: $result['success'] = 0;
89: $result['error_msg'] = $e->getMessage();
90: }
91: catch (Exception $e) {
92: Mage::logException($e);
93: $result['success'] = 0;
94: $result['error_msg'] = $this->__('There was an error processing your order. Please contact us or try again later.');
95: }
96:
97: if (!empty($data['controller_action_name'])) {
98: if (!empty($data['key'])) {
99: $result['key'] = $data['key'];
100: }
101: $result['controller_action_name'] = $data['controller_action_name'];
102: $result['is_secure'] = isset($data['is_secure']) ? $data['is_secure'] : false;
103: $params['redirect'] = Mage::helper('authorizenet')->getRedirectIframeUrl($result);
104: }
105: $block = $this->_getIframeBlock()->setParams($params);
106: $this->getResponse()->setBody($block->toHtml());
107: }
108:
109: 110: 111: 112:
113: public function redirectAction()
114: {
115: $redirectParams = $this->getRequest()->getParams();
116: $params = array();
117: if (!empty($redirectParams['success'])
118: && isset($redirectParams['x_invoice_num'])
119: && isset($redirectParams['controller_action_name'])
120: ) {
121: $this->_getDirectPostSession()->unsetData('quote_id');
122: $params['redirect_parent'] = Mage::helper('authorizenet')->getSuccessOrderUrl($redirectParams);
123: }
124: if (!empty($redirectParams['error_msg'])) {
125: $cancelOrder = empty($redirectParams['x_invoice_num']);
126: $this->_returnCustomerQuote($cancelOrder, $redirectParams['error_msg']);
127: }
128: $block = $this->_getIframeBlock()->setParams(array_merge($params, $redirectParams));
129: $this->getResponse()->setBody($block->toHtml());
130: }
131:
132: 133: 134: 135:
136: public function placeAction()
137: {
138: $paymentParam = $this->getRequest()->getParam('payment');
139: $controller = $this->getRequest()->getParam('controller');
140: if (isset($paymentParam['method'])) {
141: $params = Mage::helper('authorizenet')->getSaveOrderUrlParams($controller);
142: $this->_getDirectPostSession()->setQuoteId($this->_getCheckout()->getQuote()->getId());
143: $this->_forward(
144: $params['action'],
145: $params['controller'],
146: $params['module'],
147: $this->getRequest()->getParams()
148: );
149: } else {
150: $result = array(
151: 'error_messages' => $this->__('Please, choose payment method'),
152: 'goto_section' => 'payment'
153: );
154: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
155: }
156: }
157:
158: 159: 160: 161:
162: public function returnQuoteAction()
163: {
164: $this->_returnCustomerQuote();
165: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode(array('success' => 1)));
166: }
167:
168: 169: 170: 171: 172: 173:
174: protected function _returnCustomerQuote($cancelOrder = false, $errorMsg = '')
175: {
176: $incrementId = $this->_getDirectPostSession()->getLastOrderIncrementId();
177: if ($incrementId &&
178: $this->_getDirectPostSession()
179: ->isCheckoutOrderIncrementIdExist($incrementId)
180: ) {
181:
182: $order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);
183: if ($order->getId()) {
184: $quote = Mage::getModel('sales/quote')
185: ->load($order->getQuoteId());
186: if ($quote->getId()) {
187: $quote->setIsActive(1)
188: ->setReservedOrderId(NULL)
189: ->save();
190: $this->_getCheckout()->replaceQuote($quote);
191: }
192: $this->_getDirectPostSession()->removeCheckoutOrderIncrementId($incrementId);
193: $this->_getDirectPostSession()->unsetData('quote_id');
194: if ($cancelOrder) {
195: $order->registerCancellation($errorMsg)->save();
196: }
197: }
198: }
199: }
200: }
201: