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