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: * Paypal expess checkout shortcut link
29: */
30: class Mage_Paypal_Block_Express_Shortcut extends Mage_Core_Block_Template
31: {
32: /**
33: * Position of "OR" label against shortcut
34: */
35: const POSITION_BEFORE = 'before';
36: const POSITION_AFTER = 'after';
37:
38: /**
39: * Whether the block should be eventually rendered
40: *
41: * @var bool
42: */
43: protected $_shouldRender = true;
44:
45: /**
46: * Payment method code
47: *
48: * @var string
49: */
50: protected $_paymentMethodCode = Mage_Paypal_Model_Config::METHOD_WPP_EXPRESS;
51:
52: /**
53: * Start express action
54: *
55: * @var string
56: */
57: protected $_startAction = 'paypal/express/start';
58:
59: /**
60: * Express checkout model factory name
61: *
62: * @var string
63: */
64: protected $_checkoutType = 'paypal/express_checkout';
65:
66: protected function _beforeToHtml()
67: {
68: $result = parent::_beforeToHtml();
69: $config = Mage::getModel('paypal/config', array($this->_paymentMethodCode));
70: $isInCatalog = $this->getIsInCatalogProduct();
71: $quote = ($isInCatalog || '' == $this->getIsQuoteAllowed())
72: ? null : Mage::getSingleton('checkout/session')->getQuote();
73:
74: // check visibility on cart or product page
75: $context = $isInCatalog ? 'visible_on_product' : 'visible_on_cart';
76: if (!$config->$context) {
77: $this->_shouldRender = false;
78: return $result;
79: }
80:
81: if ($isInCatalog) {
82: // Show PayPal shortcut on a product view page only if product has nonzero price
83: /** @var $currentProduct Mage_Catalog_Model_Product */
84: $currentProduct = Mage::registry('current_product');
85: if (!is_null($currentProduct)) {
86: $productPrice = (float)$currentProduct->getFinalPrice();
87: if (empty($productPrice) && !$currentProduct->isGrouped()) {
88: $this->_shouldRender = false;
89: return $result;
90: }
91: }
92: }
93: // validate minimum quote amount and validate quote for zero grandtotal
94: if (null !== $quote && (!$quote->validateMinimumAmount()
95: || (!$quote->getGrandTotal() && !$quote->hasNominalItems()))) {
96: $this->_shouldRender = false;
97: return $result;
98: }
99:
100: // check payment method availability
101: $methodInstance = Mage::helper('payment')->getMethodInstance($this->_paymentMethodCode);
102: if (!$methodInstance || !$methodInstance->isAvailable($quote)) {
103: $this->_shouldRender = false;
104: return $result;
105: }
106:
107: // set misc data
108: $this->setShortcutHtmlId($this->helper('core')->uniqHash('ec_shortcut_'))
109: ->setCheckoutUrl($this->getUrl($this->_startAction))
110: ;
111:
112: // use static image if in catalog
113: if ($isInCatalog || null === $quote) {
114: $this->setImageUrl($config->getExpressCheckoutShortcutImageUrl(Mage::app()->getLocale()->getLocaleCode()));
115: } else {
116: $this->setImageUrl(Mage::getModel($this->_checkoutType, array(
117: 'quote' => $quote,
118: 'config' => $config,
119: ))->getCheckoutShortcutImageUrl());
120: }
121:
122: // ask whether to create a billing agreement
123: $customerId = Mage::getSingleton('customer/session')->getCustomerId(); // potential issue for caching
124: if (Mage::helper('paypal')->shouldAskToCreateBillingAgreement($config, $customerId)) {
125: $this->setConfirmationUrl($this->getUrl($this->_startAction,
126: array(Mage_Paypal_Model_Express_Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT => 1)
127: ));
128: $this->setConfirmationMessage(Mage::helper('paypal')->__('Would you like to sign a billing agreement to streamline further purchases with PayPal?'));
129: }
130:
131: return $result;
132: }
133:
134: /**
135: * Render the block if needed
136: *
137: * @return string
138: */
139: protected function _toHtml()
140: {
141: if (!$this->_shouldRender) {
142: return '';
143: }
144: return parent::_toHtml();
145: }
146:
147: /**
148: * Check is "OR" label position before shortcut
149: *
150: * @return bool
151: */
152: public function isOrPositionBefore()
153: {
154: return ($this->getIsInCatalogProduct() && !$this->getShowOrPosition())
155: || ($this->getShowOrPosition() && $this->getShowOrPosition() == self::POSITION_BEFORE);
156:
157: }
158:
159: /**
160: * Check is "OR" label position after shortcut
161: *
162: * @return bool
163: */
164: public function isOrPositionAfter()
165: {
166: return (!$this->getIsInCatalogProduct() && !$this->getShowOrPosition())
167: || ($this->getShowOrPosition() && $this->getShowOrPosition() == self::POSITION_AFTER);
168: }
169: }
170: