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) 2012 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: /**
28: * Authorizenet directpayment observer
29: *
30: * @category Mage
31: * @package Mage_Authorizenet
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Authorizenet_Model_Directpost_Observer
35: {
36: /**
37: * Save order into registry to use it in the overloaded controller.
38: *
39: * @param Varien_Event_Observer $observer
40: * @return Mage_Authorizenet_Model_Directpost_Observer
41: */
42: public function saveOrderAfterSubmit(Varien_Event_Observer $observer)
43: {
44: /* @var $order Mage_Sales_Model_Order */
45: $order = $observer->getEvent()->getData('order');
46: Mage::register('directpost_order', $order, true);
47:
48: return $this;
49: }
50:
51: /**
52: * Set data for response of frontend saveOrder action
53: *
54: * @param Varien_Event_Observer $observer
55: * @return Mage_Authorizenet_Model_Directpost_Observer
56: */
57: public function addAdditionalFieldsToResponseFrontend(Varien_Event_Observer $observer)
58: {
59: /* @var $order Mage_Sales_Model_Order */
60: $order = Mage::registry('directpost_order');
61:
62: if ($order && $order->getId()) {
63: $payment = $order->getPayment();
64: if ($payment && $payment->getMethod() == Mage::getModel('authorizenet/directpost')->getCode()) {
65: /* @var $controller Mage_Core_Controller_Varien_Action */
66: $controller = $observer->getEvent()->getData('controller_action');
67: $result = Mage::helper('core')->jsonDecode(
68: $controller->getResponse()->getBody('default'),
69: Zend_Json::TYPE_ARRAY
70: );
71:
72: if (empty($result['error'])) {
73: $payment = $order->getPayment();
74: //if success, then set order to session and add new fields
75: $session = Mage::getSingleton('authorizenet/directpost_session');
76: $session->addCheckoutOrderIncrementId($order->getIncrementId());
77: $session->setLastOrderIncrementId($order->getIncrementId());
78: $requestToPaygate = $payment->getMethodInstance()->generateRequestFromOrder($order);
79: $requestToPaygate->setControllerActionName($controller->getRequest()->getControllerName());
80: $requestToPaygate->setIsSecure((string)Mage::app()->getStore()->isCurrentlySecure());
81:
82: $result['directpost'] = array('fields' => $requestToPaygate->getData());
83:
84: $controller->getResponse()->clearHeader('Location');
85: $controller->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
86: }
87: }
88: }
89:
90: return $this;
91: }
92:
93: /**
94: * Update all edit increments for all orders if module is enabled.
95: * Needed for correct work of edit orders in Admin area.
96: *
97: * @param Varien_Event_Observer $observer
98: * @return Mage_Authorizenet_Model_Directpost_Observer
99: */
100: public function updateAllEditIncrements(Varien_Event_Observer $observer)
101: {
102: /* @var $order Mage_Sales_Model_Order */
103: $order = $observer->getEvent()->getData('order');
104: Mage::helper('authorizenet')->updateOrderEditIncrements($order);
105:
106: return $this;
107: }
108: }
109: