Container.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Block\Form;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Payment\Model\Method\AbstractMethod;
  9. /**
  10. * Base container block for payment methods forms
  11. *
  12. * @method \Magento\Quote\Model\Quote getQuote()
  13. *
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Container extends \Magento\Framework\View\Element\Template
  18. {
  19. /**
  20. * @var \Magento\Payment\Helper\Data
  21. */
  22. protected $_paymentHelper;
  23. /**
  24. * @var \Magento\Payment\Model\Checks\SpecificationFactory
  25. */
  26. protected $methodSpecificationFactory;
  27. /**
  28. * @var \Magento\Payment\Api\PaymentMethodListInterface
  29. */
  30. private $paymentMethodList;
  31. /**
  32. * @var \Magento\Payment\Model\Method\InstanceFactory
  33. */
  34. private $paymentMethodInstanceFactory;
  35. /**
  36. * @var array
  37. * @since 100.1.3
  38. */
  39. protected $additionalChecks;
  40. /**
  41. * @param \Magento\Framework\View\Element\Template\Context $context
  42. * @param \Magento\Payment\Helper\Data $paymentHelper
  43. * @param \Magento\Payment\Model\Checks\SpecificationFactory $methodSpecificationFactory
  44. * @param array $data
  45. * @param array $additionalChecks
  46. */
  47. public function __construct(
  48. \Magento\Framework\View\Element\Template\Context $context,
  49. \Magento\Payment\Helper\Data $paymentHelper,
  50. \Magento\Payment\Model\Checks\SpecificationFactory $methodSpecificationFactory,
  51. array $data = [],
  52. array $additionalChecks = []
  53. ) {
  54. $this->_paymentHelper = $paymentHelper;
  55. $this->methodSpecificationFactory = $methodSpecificationFactory;
  56. $this->additionalChecks = $additionalChecks;
  57. parent::__construct($context, $data);
  58. }
  59. /**
  60. * Prepare children blocks
  61. *
  62. * @return $this
  63. */
  64. protected function _prepareLayout()
  65. {
  66. /**
  67. * Create child blocks for payment methods forms
  68. */
  69. foreach ($this->getMethods() as $method) {
  70. $this->setChild(
  71. 'payment.method.' . $method->getCode(),
  72. $this->_paymentHelper->getMethodFormBlock($method, $this->_layout)
  73. );
  74. }
  75. return parent::_prepareLayout();
  76. }
  77. /**
  78. * Check payment method model
  79. *
  80. * @param \Magento\Payment\Model\MethodInterface $method
  81. * @return bool
  82. */
  83. protected function _canUseMethod($method)
  84. {
  85. $checks = array_merge(
  86. [
  87. AbstractMethod::CHECK_USE_FOR_COUNTRY,
  88. AbstractMethod::CHECK_USE_FOR_CURRENCY,
  89. AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
  90. AbstractMethod::CHECK_ZERO_TOTAL
  91. ],
  92. $this->additionalChecks
  93. );
  94. return $this->methodSpecificationFactory->create($checks)->isApplicable(
  95. $method,
  96. $this->getQuote()
  97. );
  98. }
  99. /**
  100. * Check and prepare payment method model
  101. *
  102. * Redeclare this method in child classes for declaring method info instance
  103. *
  104. * @param \Magento\Payment\Model\MethodInterface $method
  105. * @return $this
  106. */
  107. protected function _assignMethod($method)
  108. {
  109. $method->setInfoInstance($this->getQuote()->getPayment());
  110. return $this;
  111. }
  112. /**
  113. * Declare template for payment method form block
  114. *
  115. * @param string $method
  116. * @param string $template
  117. * @return $this
  118. */
  119. public function setMethodFormTemplate($method = '', $template = '')
  120. {
  121. if (!empty($method) && !empty($template)) {
  122. if ($block = $this->getChildBlock('payment.method.' . $method)) {
  123. $block->setTemplate($template);
  124. }
  125. }
  126. return $this;
  127. }
  128. /**
  129. * Retrieve available payment methods
  130. *
  131. * @return array
  132. */
  133. public function getMethods()
  134. {
  135. $methods = $this->getData('methods');
  136. if ($methods === null) {
  137. $quote = $this->getQuote();
  138. $store = $quote ? $quote->getStoreId() : null;
  139. $methods = [];
  140. foreach ($this->getPaymentMethodList()->getActiveList($store) as $method) {
  141. $methodInstance = $this->getPaymentMethodInstanceFactory()->create($method);
  142. if ($methodInstance->isAvailable($quote) && $this->_canUseMethod($methodInstance)) {
  143. $this->_assignMethod($methodInstance);
  144. $methods[] = $methodInstance;
  145. }
  146. }
  147. $this->setData('methods', $methods);
  148. }
  149. return $methods;
  150. }
  151. /**
  152. * Retrieve code of current payment method
  153. *
  154. * @return string|false
  155. */
  156. public function getSelectedMethodCode()
  157. {
  158. $methods = $this->getMethods();
  159. if (!empty($methods)) {
  160. reset($methods);
  161. return current($methods)->getCode();
  162. }
  163. return false;
  164. }
  165. /**
  166. * Get payment method list.
  167. *
  168. * @return \Magento\Payment\Api\PaymentMethodListInterface
  169. * @deprecated 100.1.3
  170. */
  171. private function getPaymentMethodList()
  172. {
  173. if ($this->paymentMethodList === null) {
  174. $this->paymentMethodList = ObjectManager::getInstance()->get(
  175. \Magento\Payment\Api\PaymentMethodListInterface::class
  176. );
  177. }
  178. return $this->paymentMethodList;
  179. }
  180. /**
  181. * Get payment method instance factory.
  182. *
  183. * @return \Magento\Payment\Model\Method\InstanceFactory
  184. * @deprecated 100.1.3
  185. */
  186. private function getPaymentMethodInstanceFactory()
  187. {
  188. if ($this->paymentMethodInstanceFactory === null) {
  189. $this->paymentMethodInstanceFactory = ObjectManager::getInstance()->get(
  190. \Magento\Payment\Model\Method\InstanceFactory::class
  191. );
  192. }
  193. return $this->paymentMethodInstanceFactory;
  194. }
  195. }