_paymentHelper = $paymentHelper; $this->methodSpecificationFactory = $methodSpecificationFactory; $this->additionalChecks = $additionalChecks; parent::__construct($context, $data); } /** * Prepare children blocks * * @return $this */ protected function _prepareLayout() { /** * Create child blocks for payment methods forms */ foreach ($this->getMethods() as $method) { $this->setChild( 'payment.method.' . $method->getCode(), $this->_paymentHelper->getMethodFormBlock($method, $this->_layout) ); } return parent::_prepareLayout(); } /** * Check payment method model * * @param \Magento\Payment\Model\MethodInterface $method * @return bool */ protected function _canUseMethod($method) { $checks = array_merge( [ AbstractMethod::CHECK_USE_FOR_COUNTRY, AbstractMethod::CHECK_USE_FOR_CURRENCY, AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX, AbstractMethod::CHECK_ZERO_TOTAL ], $this->additionalChecks ); return $this->methodSpecificationFactory->create($checks)->isApplicable( $method, $this->getQuote() ); } /** * Check and prepare payment method model * * Redeclare this method in child classes for declaring method info instance * * @param \Magento\Payment\Model\MethodInterface $method * @return $this */ protected function _assignMethod($method) { $method->setInfoInstance($this->getQuote()->getPayment()); return $this; } /** * Declare template for payment method form block * * @param string $method * @param string $template * @return $this */ public function setMethodFormTemplate($method = '', $template = '') { if (!empty($method) && !empty($template)) { if ($block = $this->getChildBlock('payment.method.' . $method)) { $block->setTemplate($template); } } return $this; } /** * Retrieve available payment methods * * @return array */ public function getMethods() { $methods = $this->getData('methods'); if ($methods === null) { $quote = $this->getQuote(); $store = $quote ? $quote->getStoreId() : null; $methods = []; foreach ($this->getPaymentMethodList()->getActiveList($store) as $method) { $methodInstance = $this->getPaymentMethodInstanceFactory()->create($method); if ($methodInstance->isAvailable($quote) && $this->_canUseMethod($methodInstance)) { $this->_assignMethod($methodInstance); $methods[] = $methodInstance; } } $this->setData('methods', $methods); } return $methods; } /** * Retrieve code of current payment method * * @return string|false */ public function getSelectedMethodCode() { $methods = $this->getMethods(); if (!empty($methods)) { reset($methods); return current($methods)->getCode(); } return false; } /** * Get payment method list. * * @return \Magento\Payment\Api\PaymentMethodListInterface * @deprecated 100.1.3 */ private function getPaymentMethodList() { if ($this->paymentMethodList === null) { $this->paymentMethodList = ObjectManager::getInstance()->get( \Magento\Payment\Api\PaymentMethodListInterface::class ); } return $this->paymentMethodList; } /** * Get payment method instance factory. * * @return \Magento\Payment\Model\Method\InstanceFactory * @deprecated 100.1.3 */ private function getPaymentMethodInstanceFactory() { if ($this->paymentMethodInstanceFactory === null) { $this->paymentMethodInstanceFactory = ObjectManager::getInstance()->get( \Magento\Payment\Model\Method\InstanceFactory::class ); } return $this->paymentMethodInstanceFactory; } }