public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null) { if ($quote && ($quote->getBaseGrandTotal() < $this->_minAmount || $this->_maxAmount && $quote->getBaseGrandTotal() > $this->_maxAmount)) { return false; } return parent::isAvailable($quote); }
/** * @param bool $result * * @dataProvider dataProviderForTestIsAvailable */ public function testIsAvailable($result) { $storeId = 15; $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($storeId); $this->scopeConfigMock->expects($this->once())->method('getValue')->with('payment/' . Stub::STUB_CODE . '/active', ScopeInterface::SCOPE_STORE, $storeId)->willReturn($result); $this->eventManagerMock->expects($this->once())->method('dispatch')->with($this->equalTo('payment_method_is_active'), $this->countOf(3)); $this->assertEquals($result, $this->payment->isAvailable($this->quoteMock)); }
public function testGetShippingAddress() { /** @var AddressAdapterInterface $addressAdapterMock */ $addressAdapterMock = $this->getMockBuilder('Magento\\Payment\\Gateway\\Data\\AddressAdapterInterface')->getMockForAbstractClass(); /** @var \Magento\Quote\Api\Data\AddressInterface $quoteAddressMock */ $quoteAddressMock = $this->getMockBuilder('Magento\\Quote\\Api\\Data\\AddressInterface')->getMockForAbstractClass(); $this->addressAdapterFactoryMock->expects($this->once())->method('create')->with(['address' => $quoteAddressMock])->willReturn($addressAdapterMock); $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($quoteAddressMock); $this->assertSame($addressAdapterMock, $this->model->getShippingAddress()); }
/** * @param ShippingAssignmentInterface $shippingAssignment * @param CartInterface $quote * @return void * @throws InputException */ public function save(CartInterface $quote, ShippingAssignmentInterface $shippingAssignment) { /** @var \Magento\Quote\Model\Quote $quote */ foreach ($shippingAssignment->getItems() as $item) { if (!$quote->getItemById($item->getItemId())) { $this->cartItemPersister->save($quote, $item); } } $this->shippingProcessor->save($shippingAssignment->getShipping(), $quote); }
/** * Check whether payment method can be used * * @param CartInterface|null $quote * @return bool */ public function isAvailable(CartInterface $quote = null) { if ($quote !== null) { $accessCheck = $this->accessCheckFactory->create(array("storeId" => $this->getStore())); if ($accessCheck->check($quote->getCustomer()->getGroupId()) === false) { return false; } } return parent::isAvailable($quote); }
/** * @param ShippingInterface $shipping * @param CartInterface $quote * @return void */ public function save(ShippingInterface $shipping, CartInterface $quote) { $this->shippingAddressManagement->assign($quote->getId(), $shipping->getAddress()); if (!empty($shipping->getMethod()) && $quote->getItemsCount() > 0) { $nameComponents = explode('_', $shipping->getMethod()); $carrierCode = array_shift($nameComponents); // carrier method code can contains more one name component $methodCode = implode('_', $nameComponents); $this->shippingMethodManagement->apply($quote->getId(), $carrierCode, $methodCode); } }
/** * @param \Magento\Quote\Api\Data\CartInterface $quote * @return \Magento\Payment\Model\MethodInterface[] * @api */ public function getAvailableMethods(\Magento\Quote\Api\Data\CartInterface $quote = null) { $store = $quote ? $quote->getStoreId() : null; $methods = []; foreach ($this->paymentHelper->getStoreMethods($store, $quote) as $method) { if ($this->_canUseMethod($method, $quote)) { $method->setInfoInstance($quote->getPayment()); $methods[] = $method; } } return $methods; }
/** * @param CartInterface $quote * @param CartItemInterface $item * @return CartItemInterface * @throws CouldNotSaveException * @throws InputException * @throws LocalizedException * @throws NoSuchEntityException * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function save(CartInterface $quote, CartItemInterface $item) { /** @var \Magento\Quote\Model\Quote $quote */ $qty = $item->getQty(); if (!is_numeric($qty) || $qty <= 0) { throw InputException::invalidFieldValue('qty', $qty); } $cartId = $item->getQuoteId(); $itemId = $item->getItemId(); try { /** Update existing item */ if (isset($itemId)) { $currentItem = $quote->getItemById($itemId); if (!$currentItem) { throw new NoSuchEntityException(__('Cart %1 does not contain item %2', $cartId, $itemId)); } $productType = $currentItem->getProduct()->getTypeId(); $buyRequestData = $this->cartItemOptionProcessor->getBuyRequest($productType, $item); if (is_object($buyRequestData)) { /** Update item product options */ $item = $quote->updateItem($itemId, $buyRequestData); } else { if ($item->getQty() !== $currentItem->getQty()) { $currentItem->setQty($qty); } } } else { /** add new item to shopping cart */ $product = $this->productRepository->get($item->getSku()); $productType = $product->getTypeId(); $item = $quote->addProduct($product, $this->cartItemOptionProcessor->getBuyRequest($productType, $item)); if (is_string($item)) { throw new LocalizedException(__($item)); } } } catch (NoSuchEntityException $e) { throw $e; } catch (LocalizedException $e) { throw $e; } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not save quote')); } $itemId = $item->getId(); foreach ($quote->getAllItems() as $quoteItem) { /** @var \Magento\Quote\Model\Quote\Item $quoteItem */ if ($itemId == $quoteItem->getId()) { $item = $this->cartItemOptionProcessor->addProductOptions($productType, $quoteItem); return $this->cartItemOptionProcessor->applyCustomOptions($item); } } throw new CouldNotSaveException(__('Could not save quote')); }
/** * @param \Magento\Quote\Api\Data\CartInterface $quote * @return \Magento\Payment\Model\MethodInterface[] * @api */ public function getAvailableMethods(\Magento\Quote\Api\Data\CartInterface $quote = null) { $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, $quote) && $specification->isApplicable($method, $quote)) { $method->setInfoInstance($quote->getPayment()); $methods[] = $method; } } return $methods; }
/** * @param CartInterface $quote * @param AddressInterface $address * @param bool $useForShipping * @return void * @throws NoSuchEntityException * @throws InputException */ public function save(CartInterface $quote, AddressInterface $address, $useForShipping = false) { /** @var \Magento\Quote\Model\Quote $quote */ $this->addressValidator->validate($address); $customerAddressId = $address->getCustomerAddressId(); $shippingAddress = null; $addressData = []; if ($useForShipping) { $shippingAddress = $address; } $saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0; if ($customerAddressId) { try { $addressData = $this->addressRepository->getById($customerAddressId); } catch (NoSuchEntityException $e) { // do nothing if customer is not found by id } $address = $quote->getBillingAddress()->importCustomerAddressData($addressData); if ($useForShipping) { $shippingAddress = $quote->getShippingAddress()->importCustomerAddressData($addressData); $shippingAddress->setSaveInAddressBook($saveInAddressBook); } } elseif ($quote->getCustomerId()) { $address->setEmail($quote->getCustomerEmail()); } $address->setSaveInAddressBook($saveInAddressBook); $quote->setBillingAddress($address); if ($useForShipping) { $shippingAddress->setSameAsBilling(1); $shippingAddress->setCollectShippingRates(true); $quote->setShippingAddress($shippingAddress); } }
public function testSave() { $this->quoteRepositoryMock->expects($this->any())->method('get')->willReturn($this->quoteMock); $this->quoteItemMock->expects($this->once())->method('getItemId')->willReturn($this->itemId); $this->quoteItemMock->expects($this->once())->method('getProductType')->willReturn(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE); $customerMock = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false); $this->quoteMock->expects($this->once())->method('getCustomer')->willReturn($customerMock); $this->messageMock->expects($this->any())->method('setCustomerId'); $this->messageMock->expects($this->any())->method('setGiftMessageId'); $this->cacheMock->expects($this->once())->method('save'); $this->assertTrue($this->itemRepository->save($this->cartId, $this->messageMock, $this->itemId)); }
public function testSave() { $this->quoteRepositoryMock->expects($this->any())->method('get')->willReturn($this->quoteMock); $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(1); $this->quoteMock->expects($this->once())->method('getIsVirtual')->willReturn(false); $customerMock = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false); $this->quoteMock->expects($this->once())->method('getCustomer')->willReturn($customerMock); $this->messageMock->expects($this->any())->method('setCustomerId'); $this->messageMock->expects($this->any())->method('setGiftMessageId'); $this->cacheMock->expects($this->once())->method('save'); $this->assertTrue($this->cartRepository->save($this->cartId, $this->messageMock)); }
/** * @param CartInterface $quote * @return CartInterface * * @throws InputException * @throws \Magento\Framework\Exception\CouldNotSaveException * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function save(CartInterface $quote) { /** @var \Magento\Quote\Model\Quote $quote */ // Quote Item processing $items = $quote->getItems(); if ($items) { foreach ($items as $item) { /** @var \Magento\Quote\Model\Quote\Item $item */ if (!$item->isDeleted()) { $quote->setLastAddedItem($this->cartItemPersister->save($quote, $item)); } } } // Billing Address processing $billingAddress = $quote->getBillingAddress(); if ($billingAddress) { $this->billingAddressPersister->save($quote, $billingAddress); } $this->processShippingAssignment($quote); $this->quoteResourceModel->save($quote->collectTotals()); return $quote; }
/** * Returns false if paytypes are disabled in checkout or there is no method for paytypes in current API. * Returns array of paytypes otherwise. * * @param \Magento\Quote\Api\Data\CartInterface $quote * @return array|false */ public function getAllForQuote(\Magento\Quote\Api\Data\CartInterface $quote) { /** * @var $client \Orba\Payupl\Model\Client */ if (!$this->scopeConfig->isSetFlag(\Orba\Payupl\Model\Payupl::XML_PATH_PAYTYPES_IN_CHECKOUT, 'store')) { return false; } $client = $this->clientFactory->create(); $paytypes = $client->getPaytypes(); if ($paytypes === false) { return false; } $total = $quote->getGrandTotal(); foreach ($paytypes as $key => $paytype) { if (!$paytype['enable'] || $total < $paytype['min'] || $total > $paytype['max']) { unset($paytypes[$key]); } else { $paytypes[$key]['id'] = 'orba-payupl-paytype-' . $paytype['type']; } } return $paytypes; }
/** * @param \Magento\Quote\Api\Data\CartInterface $quote * @return \Magento\Payment\Model\MethodInterface[] * @api */ public function getAvailableMethods(\Magento\Quote\Api\Data\CartInterface $quote = null) { $store = $quote ? $quote->getStoreId() : null; $methods = []; $isFreeAdded = false; foreach ($this->paymentHelper->getStoreMethods($store, $quote) as $method) { if ($this->_canUseMethod($method, $quote)) { $method->setInfoInstance($quote->getPayment()); $methods[] = $method; if ($method->getCode() == Free::PAYMENT_METHOD_FREE_CODE) { $isFreeAdded = true; } } } if (!$isFreeAdded) { /** @var \Magento\Payment\Model\Method\Free $freeMethod */ $freeMethod = $this->paymentHelper->getMethodInstance(Free::PAYMENT_METHOD_FREE_CODE); if ($freeMethod->isAvailableInConfig()) { $freeMethod->setInfoInstance($quote->getPayment()); $methods[] = $freeMethod; } } return $methods; }
/** * @param CartInterface $quote * @return CartInterface */ public function load(CartInterface $quote) { if (!$quote->getIsActive()) { return $quote; } /** @var \Magento\Quote\Model\Quote $quote */ $quote->setItems($quote->getAllVisibleItems()); $shippingAssignments = []; if (!$quote->isVirtual() && $quote->getItemsQty() > 0) { $shippingAssignments[] = $this->shippingAssignmentProcessor->create($quote); } $cartExtension = $quote->getExtensionAttributes(); if ($cartExtension === null) { $cartExtension = $this->cartExtensionFactory->create(); } $cartExtension->setShippingAssignments($shippingAssignments); $quote->setExtensionAttributes($cartExtension); return $quote; }
/** * Check whether there are CC types set in configuration * * @param \Magento\Quote\Api\Data\CartInterface|null $quote * @return bool */ public function isAvailable($quote = null) { return $this->getConfigData('cctypes', $quote ? $quote->getStoreId() : null) && parent::isAvailable($quote); }
/** * {@inheritdoc} */ public function delete(\Magento\Quote\Api\Data\CartInterface $quote) { $quoteId = $quote->getId(); $customerId = $quote->getCustomerId(); $quote->delete(); unset($this->quotesById[$quoteId]); unset($this->quotesByCustomerId[$customerId]); }
/** * Check whether there are CC types set in configuration * * @param \Magento\Quote\Api\Data\CartInterface|null $quote * @return bool */ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null) { return $this->getConfigData('cctypes', $quote ? $quote->getStoreId() : null) && parent::isAvailable($quote); }
/** * Check whether payment method can be used with the quote * * @param \Magento\Quote\Api\Data\CartInterface|null $quote * @return bool */ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null) { return $this->isActive($quote ? $quote->getStoreId() : null); }
/** * Returns shipping address * * @return AddressAdapterInterface */ public function getShippingAddress() { return $this->addressAdapterFactory->create(['address' => $this->quote->getShippingAddress()]); }
/** * @param CartInterface $quote * @param ShippingAssignmentInterface $shippingAssignment * @return void */ public function save(CartInterface $quote, ShippingAssignmentInterface $shippingAssignment) { if ($quote->getIsActive()) { $this->shippingAssignmentProcessor->save($quote, $shippingAssignment); } }
/** * Check whether payment method can be used * * @param \Magento\Quote\Api\Data\CartInterface|null $quote * @return bool */ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null) { if (!$this->isActive($quote ? $quote->getStoreId() : null)) { return false; } $checkResult = new DataObject(); $checkResult->setData('is_available', true); // for future use in observers $this->_eventManager->dispatch('payment_method_is_active', ['result' => $checkResult, 'method_instance' => $this, 'quote' => $quote]); return $checkResult->getData('is_available'); }
/** * Delete partial payment registry data before quote deletion. * * @param $subject * @param \Magento\Quote\Api\Data\CartInterface $quote * @return array * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeDelete($subject, \Magento\Quote\Api\Data\CartInterface $quote) { $quoteId = $quote->getId(); $this->_repoPartialQuote->deleteById($quoteId); return [$quote]; }
/** * Returns order id * * @return int */ public function getId() { return $this->quote->getId(); }
/** * 2016-02-15 * @override * How is a payment method's isAvailable() used? https://mage2.pro/t/721 * * @see \Magento\Payment\Model\MethodInterface::isAvailable() * https://github.com/magento/magento2/blob/6ce74b2/app/code/Magento/Payment/Model/MethodInterface.php#L343-L350 * @see \Magento\Payment\Model\Method\AbstractMethod::isAvailable() * https://github.com/magento/magento2/blob/6ce74b2/app/code/Magento/Payment/Model/Method/AbstractMethod.php#L805-L825 * * @param CartInterface|Q $quote [optional] * @return bool */ public function isAvailable(CartInterface $quote = null) { /** @var bool $result */ $result = ($this->availableInBackend() || !df_is_backend()) && $this->isActive($quote ? $quote->getStoreId() : null); if ($result) { /** @var DataObject $checkResult */ $checkResult = new DataObject(['is_available' => true]); df_dispatch('payment_method_is_active', ['result' => $checkResult, 'method_instance' => $this, 'quote' => $quote]); $result = $checkResult['is_available']; } if ($result && $quote) { /** @var float $amount */ $amount = $this->s()->cFromBase($quote->getBaseGrandTotal(), $quote); /** @var int|float $min */ /** @var int|float $max */ list($min, $max) = dfa($this->amountLimits(), $this->s()->currencyC($quote), [null, null]); $result = (is_null($min) || $amount >= $min) && (is_null($max) || $amount <= $max); } return $result; }
/** * @param CartInterface $quote * @param AddressInterface $address * @param string $method * @return CartInterface */ private function prepareShippingAssignment(CartInterface $quote, AddressInterface $address, $method) { $cartExtension = $quote->getExtensionAttributes(); if ($cartExtension === null) { $cartExtension = $this->getCartExtensionFactory()->create(); } $shippingAssignment = $cartExtension->getShippingAssignments()[0]; if ($cartExtension->getShippingAssignments() === null) { $shippingAssignment = $this->getShippingAssignmentFactory()->create(); } $shipping = $shippingAssignment->getShipping(); if ($shipping === null) { $shipping = $this->getShippingFactory()->create(); } $shipping->setAddress($address); $shipping->setMethod($method); $shippingAssignment->setShipping($shipping); $cartExtension->setShippingAssignments([$shippingAssignment]); return $quote->setExtensionAttributes($cartExtension); }
/** * @param CartInterface $quote * @return void * @throws \InvalidArgumentException */ protected function validateQuote($quote) { if (!$quote || !$quote->getItemsCount()) { throw new \InvalidArgumentException(__('We can\'t initialize checkout.')); } }
/** * {inheritdoc} */ public function isAvailable(CartInterface $quote = null) { if (!$this->isActive($quote ? $quote->getStoreId() : null)) { return false; } $checkResult = new DataObject(); $checkResult->setData('is_available', true); try { $validator = $this->validatorPool->get('availability'); $result = $validator->validate(['payment' => $this->paymentDataObjectFactory->create($this->getInfoInstance())]); $checkResult->setData('is_available', $result->isValid()); } catch (NotFoundException $e) { // pass } // for future use in observers $this->eventManager->dispatch('payment_method_is_active', ['result' => $checkResult, 'method_instance' => $this, 'quote' => $quote]); return $checkResult->getData('is_available'); }
/** * Check whether payment method can be used with the quote * * @param \Magento\Quote\Api\Data\CartInterface|null $quote * @return bool */ public function isAvailable($quote = null) { return $this->isActive($quote ? $quote->getStoreId() : null); }