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_Payment
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: * Base container block for payment methods forms
29: *
30: * @category Mage
31: * @package Mage_Payment
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Payment_Block_Form_Container extends Mage_Core_Block_Template
35: {
36: /**
37: * Prepare children blocks
38: */
39: protected function _prepareLayout()
40: {
41: /**
42: * Create child blocks for payment methods forms
43: */
44: foreach ($this->getMethods() as $method) {
45: $this->setChild(
46: 'payment.method.'.$method->getCode(),
47: $this->helper('payment')->getMethodFormBlock($method)
48: );
49: }
50:
51: return parent::_prepareLayout();
52: }
53:
54: protected function _canUseMethod($method)
55: {
56: if (!$method->canUseForCountry($this->getQuote()->getBillingAddress()->getCountry())) {
57: return false;
58: }
59:
60: if (!$method->canUseForCurrency($this->getQuote()->getStore()->getBaseCurrencyCode())) {
61: return false;
62: }
63:
64: /**
65: * Checking for min/max order total for assigned payment method
66: */
67: $total = $this->getQuote()->getBaseGrandTotal();
68: $minTotal = $method->getConfigData('min_order_total');
69: $maxTotal = $method->getConfigData('max_order_total');
70:
71: if((!empty($minTotal) && ($total < $minTotal)) || (!empty($maxTotal) && ($total > $maxTotal))) {
72: return false;
73: }
74: return true;
75: }
76:
77: /**
78: * Check and prepare payment method model
79: *
80: * Redeclare this method in child classes for declaring method info instance
81: *
82: * @return bool
83: */
84: protected function _assignMethod($method)
85: {
86: $method->setInfoInstance($this->getQuote()->getPayment());
87: return $this;
88: }
89:
90: /**
91: * Declare template for payment method form block
92: *
93: * @param string $method
94: * @param string $template
95: * @return Mage_Payment_Block_Form_Container
96: */
97: public function setMethodFormTemplate($method='', $template='')
98: {
99: if (!empty($method) && !empty($template)) {
100: if ($block = $this->getChild('payment.method.'.$method)) {
101: $block->setTemplate($template);
102: }
103: }
104: return $this;
105: }
106:
107: /**
108: * Retrieve availale payment methods
109: *
110: * @return array
111: */
112: public function getMethods()
113: {
114: $methods = $this->getData('methods');
115: if (is_null($methods)) {
116: $quote = $this->getQuote();
117: $store = $quote ? $quote->getStoreId() : null;
118: $methods = $this->helper('payment')->getStoreMethods($store, $quote);
119: $total = $quote->getBaseSubtotal() + $quote->getShippingAddress()->getBaseShippingAmount();
120: foreach ($methods as $key => $method) {
121: if ($this->_canUseMethod($method)
122: && ($total != 0
123: || $method->getCode() == 'free'
124: || ($quote->hasRecurringItems() && $method->canManageRecurringProfiles()))) {
125: $this->_assignMethod($method);
126: } else {
127: unset($methods[$key]);
128: }
129: }
130: $this->setData('methods', $methods);
131: }
132: return $methods;
133: }
134:
135: /**
136: * Retrieve code of current payment method
137: *
138: * @return mixed
139: */
140: public function getSelectedMethodCode()
141: {
142: $methods = $this->getMethods();
143: if (!empty($methods)) {
144: reset($methods);
145: return current($methods)->getCode();
146: }
147: return false;
148: }
149: }
150: