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_Paypal
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: * Payflow Advanced Checkout Controller
29: *
30: * @category Mage
31: * @package Mage_Paypal
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Paypal_PayflowadvancedController extends Mage_Paypal_Controller_Express_Abstract
35: {
36: /**
37: * When a customer cancel payment from payflow gateway.
38: *
39: * @return void
40: */
41: public function cancelPaymentAction()
42: {
43: $gotoSection = $this->_cancelPayment();
44: $redirectBlock = $this->_getIframeBlock()
45: ->setGotoSection($gotoSection)
46: ->setTemplate('paypal/payflowadvanced/redirect.phtml');
47: $this->getResponse()->setBody($redirectBlock->toHtml());
48: }
49:
50: /**
51: * When a customer return to website from payflow gateway.
52: *
53: * @return void
54: */
55: public function returnUrlAction()
56: {
57: $redirectBlock = $this->_getIframeBlock()
58: ->setTemplate('paypal/payflowadvanced/redirect.phtml');
59:
60: $session = $this->_getCheckout();
61: if ($session->getLastRealOrderId()) {
62: $order = Mage::getModel('sales/order')->loadByIncrementId($session->getLastRealOrderId());
63:
64: if ($order && $order->getIncrementId() == $session->getLastRealOrderId()) {
65: $allowedOrderStates = array(
66: Mage_Sales_Model_Order::STATE_PROCESSING,
67: Mage_Sales_Model_Order::STATE_COMPLETE
68: );
69: if (in_array($order->getState(), $allowedOrderStates)) {
70: $session->unsLastRealOrderId();
71: $redirectBlock->setGotoSuccessPage(true);
72: } else {
73: $gotoSection = $this->_cancelPayment(strval($this->getRequest()->getParam('RESPMSG')));
74: $redirectBlock->setGotoSection($gotoSection);
75: $redirectBlock->setErrorMsg($this->__('Payment has been declined. Please try again.'));
76: }
77: }
78: }
79:
80: $this->getResponse()->setBody($redirectBlock->toHtml());
81: }
82:
83: /**
84: * Submit transaction to Payflow getaway into iframe
85: *
86: * @return void
87: */
88: public function formAction()
89: {
90: $this->getResponse()
91: ->setBody($this->_getIframeBlock()->toHtml());
92: }
93:
94: /**
95: * Get response from PayPal by silent post method
96: *
97: * @return void
98: */
99: public function silentPostAction()
100: {
101: $data = $this->getRequest()->getPost();
102: if (isset($data['INVNUM'])) {
103: /** @var $paymentModel Mage_Paypal_Model_Payflowadvanced */
104: $paymentModel = Mage::getModel('paypal/payflowadvanced');
105: try {
106: $paymentModel->process($data);
107: } catch (Exception $e) {
108: Mage::logException($e);
109: }
110: }
111: }
112:
113: /**
114: * Cancel order, return quote to customer
115: *
116: * @param string $errorMsg
117: * @return bool|string
118: */
119: protected function _cancelPayment($errorMsg = '')
120: {
121: $gotoSection = false;
122: $session = $this->_getCheckout();
123: if ($session->getLastRealOrderId()) {
124: $order = Mage::getModel('sales/order')->loadByIncrementId($session->getLastRealOrderId());
125: if ($order->getId()) {
126: //Cancel order
127: if ($order->getState() != Mage_Sales_Model_Order::STATE_CANCELED) {
128: $order->registerCancellation($errorMsg)->save();
129: }
130: $quote = Mage::getModel('sales/quote')
131: ->load($order->getQuoteId());
132: //Return quote
133: if ($quote->getId()) {
134: $quote->setIsActive(1)
135: ->setReservedOrderId(NULL)
136: ->save();
137: $session->replaceQuote($quote);
138: }
139: //Unset data
140: $session->unsLastRealOrderId();
141: //Redirect to payment step
142: $gotoSection = 'payment';
143: }
144: }
145:
146: return $gotoSection;
147: }
148:
149: /**
150: * Get frontend checkout session object
151: *
152: * @return Mage_Checkout_Model_Session
153: */
154: protected function _getCheckout()
155: {
156: return Mage::getSingleton('checkout/session');
157: }
158:
159: /**
160: * Get iframe block
161: *
162: * @return Mage_Paypal_Block_Payflow_Advanced_Iframe
163: */
164: protected function _getIframeBlock()
165: {
166: $this->loadLayout('paypal_payflow_advanced_iframe');
167: return $this->getLayout()
168: ->getBlock('payflow.advanced.iframe');
169: }
170: }
171: