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: require_once 'Mage/Adminhtml/controllers/Sales/Order/CreateController.php';
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Authorizenet_Adminhtml_Authorizenet_Directpost_PaymentController
36: extends Mage_Adminhtml_Sales_Order_CreateController
37: {
38: 39: 40: 41: 42:
43: protected function _getDirectPostSession()
44: {
45: return Mage::getSingleton('authorizenet/directpost_session');
46: }
47:
48: 49: 50: 51: 52:
53: protected function _getOrderSession()
54: {
55: return Mage::getSingleton('adminhtml/session_quote');
56: }
57:
58: 59: 60: 61: 62:
63: protected function _getOrderCreateModel()
64: {
65: return Mage::getSingleton('adminhtml/sales_order_create');
66: }
67:
68: 69: 70: 71:
72: public function placeAction()
73: {
74: $paymentParam = $this->getRequest()->getParam('payment');
75: $controller = $this->getRequest()->getParam('controller');
76: $this->getRequest()->setPost('collect_shipping_rates', 1);
77: $this->_processActionData('save');
78:
79:
80: $orderData = $this->getRequest()->getPost('order');
81: $sendConfirmationFlag = 0;
82: if ($orderData) {
83: $sendConfirmationFlag = (!empty($orderData['send_confirmation'])) ? 1 : 0;
84: } else {
85: $orderData = array();
86: }
87:
88: if (isset($paymentParam['method'])) {
89: $saveOrderFlag = Mage::getStoreConfig('payment/'.$paymentParam['method'].'/create_order_before');
90: $result = array();
91: $params = Mage::helper('authorizenet')->getSaveOrderUrlParams($controller);
92:
93: $this->_getOrderCreateModel()->setPaymentData($paymentParam);
94: $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($paymentParam);
95:
96: $orderData['send_confirmation'] = 0;
97: $this->getRequest()->setPost('order', $orderData);
98:
99: try {
100:
101: $oldOrder = $this->_getOrderCreateModel()->getSession()->getOrder();
102: $oldOrder->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_CANCEL, false);
103:
104: $order = $this->_getOrderCreateModel()
105: ->setIsValidate(true)
106: ->importPostData($this->getRequest()->getPost('order'))
107: ->createOrder();
108:
109: $payment = $order->getPayment();
110: if ($payment && $payment->getMethod() == Mage::getModel('authorizenet/directpost')->getCode()) {
111:
112: $session = $this->_getDirectPostSession();
113: $session->addCheckoutOrderIncrementId($order->getIncrementId());
114: $session->setLastOrderIncrementId($order->getIncrementId());
115:
116: $requestToPaygate = $payment->getMethodInstance()->generateRequestFromOrder($order);
117: $requestToPaygate->setControllerActionName($controller);
118: $requestToPaygate->setOrderSendConfirmation($sendConfirmationFlag);
119: $requestToPaygate->setStoreId($this->_getOrderCreateModel()->getQuote()->getStoreId());
120:
121: $adminUrl = Mage::getSingleton('adminhtml/url');
122: if ($adminUrl->useSecretKey()) {
123: $requestToPaygate->setKey(
124: $adminUrl->getSecretKey('authorizenet_directpost_payment','redirect')
125: );
126: }
127: $result['directpost'] = array('fields' => $requestToPaygate->getData());
128: }
129:
130: $result['success'] = 1;
131: $isError = false;
132: }
133: catch (Mage_Core_Exception $e) {
134: $message = $e->getMessage();
135: if( !empty($message) ) {
136: $this->_getSession()->addError($message);
137: }
138: $isError = true;
139: }
140: catch (Exception $e) {
141: $this->_getSession()->addException($e, $this->__('Order saving error: %s', $e->getMessage()));
142: $isError = true;
143: }
144:
145: if ($isError) {
146: $result['success'] = 0;
147: $result['error'] = 1;
148: $result['redirect'] = Mage::getSingleton('adminhtml/url')->getUrl('*/sales_order_create/');
149: }
150:
151: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
152: }
153: else {
154: $result = array(
155: 'error_messages' => $this->__('Please, choose payment method')
156: );
157: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
158: }
159: }
160:
161: 162: 163: 164:
165: public function redirectAction()
166: {
167: $redirectParams = $this->getRequest()->getParams();
168: $params = array();
169: if (!empty($redirectParams['success'])
170: && isset($redirectParams['x_invoice_num'])
171: && isset($redirectParams['controller_action_name'])
172: ) {
173: $params['redirect_parent'] = Mage::helper('authorizenet')->getSuccessOrderUrl($redirectParams);
174: $this->_getDirectPostSession()->unsetData('quote_id');
175:
176: $oldOrder = $this->_getOrderCreateModel()->getSession()->getOrder();
177: if ($oldOrder->getId()) {
178:
179: $order = Mage::getModel('sales/order')->loadByIncrementId($redirectParams['x_invoice_num']);
180: if ($order->getId()) {
181: $oldOrder->cancel()
182: ->save();
183: $order->save();
184: $this->_getOrderCreateModel()->getSession()->unsOrderId();
185: }
186: }
187:
188: $this->_getSession()->clear();
189: $this->_getDirectPostSession()->removeCheckoutOrderIncrementId($redirectParams['x_invoice_num']);
190: Mage::getSingleton('adminhtml/session')->clear();
191: Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The order has been created.'));
192: }
193:
194: if (!empty($redirectParams['error_msg'])) {
195: $cancelOrder = empty($redirectParams['x_invoice_num']);
196: $this->_returnQuote($cancelOrder, $redirectParams['error_msg']);
197: }
198:
199: $block = $this->getLayout()
200: ->createBlock('directpost/iframe')
201: ->setParams(array_merge($params, $redirectParams));
202: $this->getResponse()->setBody($block->toHtml());
203: }
204:
205: 206: 207: 208:
209: public function returnQuoteAction()
210: {
211: $this->_returnQuote();
212: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode(array('success' => 1)));
213: }
214:
215: 216: 217: 218: 219: 220:
221: protected function _returnQuote($cancelOrder = false, $errorMsg = '')
222: {
223: $incrementId = $this->_getDirectPostSession()->getLastOrderIncrementId();
224: if ($incrementId &&
225: $this->_getDirectPostSession()
226: ->isCheckoutOrderIncrementIdExist($incrementId)
227: ) {
228:
229: $order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);
230: if ($order->getId()) {
231: $this->_getDirectPostSession()->removeCheckoutOrderIncrementId($order->getIncrementId());
232: if ($cancelOrder && $order->getState() == Mage_Sales_Model_Order::STATE_PENDING_PAYMENT) {
233: $order->registerCancellation($errorMsg)->save();
234: }
235: }
236: }
237: }
238: }
239: