PageRenderTime 67ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Payment/Block/Form/Container.php

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