public function testCreate() { $specification = $this->getMockBuilder('Magento\\Payment\\Model\\Checks\\SpecificationInterface')->disableOriginalConstructor()->setMethods([])->getMock(); $specificationMapping = [self::SPECIFICATION_KEY => $specification]; $expectedComposite = $this->getMockBuilder('Magento\\Payment\\Model\\Checks\\Composite')->disableOriginalConstructor()->setMethods([])->getMock(); $modelFactory = new SpecificationFactory($this->_compositeFactory, $specificationMapping); $this->_compositeFactory->expects($this->once())->method('create')->with(['list' => $specificationMapping])->will($this->returnValue($expectedComposite)); $this->assertEquals($expectedComposite, $modelFactory->create([self::SPECIFICATION_KEY])); }
/** * 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 = []; $specification = $this->methodSpecificationFactory->create([AbstractMethod::CHECK_ZERO_TOTAL]); foreach ($this->_paymentHelper->getStoreMethods($store, $quote) as $method) { if ($this->_canUseMethod($method) && $specification->isApplicable($method, $this->getQuote())) { $this->_assignMethod($method); $methods[] = $method; } } $this->setData('methods', $methods); } return $methods; }
/** * Import data array to payment method object, * Method calls quote totals collect because payment method availability * can be related to quote totals * * @param array $data * @return $this * @throws \Magento\Framework\Model\Exception */ public function importData(array $data) { $data = new \Magento\Framework\Object($data); $this->_eventManager->dispatch($this->_eventPrefix . '_import_data_before', array($this->_eventObject => $this, 'input' => $data)); $this->setMethod($data->getMethod()); $method = $this->getMethodInstance(); /** * Payment availability related with quote totals. * We have to recollect quote totals before checking */ $this->getQuote()->collectTotals(); if (!$method->isAvailable($this->getQuote()) || !$this->methodSpecificationFactory->create($data->getChecks())->isApplicable($method, $this->getQuote())) { throw new \Magento\Framework\Model\Exception(__('The requested Payment Method is not available.')); } $method->assignData($data); /* * validating the payment data */ $method->validate(); return $this; }
/** * Check payment method model * * @param \Magento\Payment\Model\MethodInterface $method * @param \Magento\Sales\Model\Quote $quote * @return bool */ protected function _canUseMethod($method, \Magento\Sales\Model\Quote $quote) { return $this->methodSpecificationFactory->create(array(AbstractMethod::CHECK_USE_FOR_COUNTRY, AbstractMethod::CHECK_USE_FOR_CURRENCY, AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX))->isApplicable($method, $quote); }
/** * Check payment method model * * @param \Magento\Payment\Model\MethodInterface $method * @param \Magento\Quote\Api\Data\CartInterface $quote * @return bool */ protected function _canUseMethod($method, \Magento\Quote\Api\Data\CartInterface $quote) { return $this->methodSpecificationFactory->create([AbstractMethod::CHECK_USE_FOR_COUNTRY, AbstractMethod::CHECK_USE_FOR_CURRENCY, AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX])->isApplicable($method, $quote); }