/** * {@inheritdoc} */ public function set($cartId, \Magento\Quote\Api\Data\PaymentInterface $method) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); $method->setChecks([\Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT, \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY, \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY, \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX]); $payment = $quote->getPayment(); $payment->importData($method->getData()); if ($quote->isVirtual()) { // check if billing address is set if ($quote->getBillingAddress()->getCountryId() === null) { throw new InvalidTransitionException(__('Billing address is not set')); } $quote->getBillingAddress()->setPaymentMethod($payment->getMethod()); } else { // check if shipping address is set if ($quote->getShippingAddress()->getCountryId() === null) { throw new InvalidTransitionException(__('Shipping address is not set')); } $quote->getShippingAddress()->setPaymentMethod($payment->getMethod()); } if (!$quote->isVirtual() && $quote->getShippingAddress()) { $quote->getShippingAddress()->setCollectShippingRates(true); } if (!$this->zeroTotalValidator->isApplicable($payment->getMethodInstance(), $quote)) { throw new InvalidTransitionException(__('The requested Payment Method is not available.')); } $quote->setTotalsCollectedFlag(false)->collectTotals()->save(); return $quote->getPayment()->getId(); }
/** * {@inheritdoc} */ public function set(\Magento\Checkout\Service\V1\Data\Cart\PaymentMethod $method, $cartId) { $quote = $this->quoteRepository->get($cartId); $payment = $this->paymentMethodBuilder->build($method, $quote); if ($quote->isVirtual()) { // check if billing address is set if (is_null($quote->getBillingAddress()->getCountryId())) { throw new InvalidTransitionException('Billing address is not set'); } $quote->getBillingAddress()->setPaymentMethod($payment->getMethod()); } else { // check if shipping address is set if (is_null($quote->getShippingAddress()->getCountryId())) { throw new InvalidTransitionException('Shipping address is not set'); } $quote->getShippingAddress()->setPaymentMethod($payment->getMethod()); } if (!$quote->isVirtual() && $quote->getShippingAddress()) { $quote->getShippingAddress()->setCollectShippingRates(true); } if (!$this->zeroTotalValidator->isApplicable($payment->getMethodInstance(), $quote)) { throw new InvalidTransitionException('The requested Payment Method is not available.'); } $quote->setTotalsCollectedFlag(false)->collectTotals()->save(); return $quote->getPayment()->getId(); }
/** * @dataProvider paymentMethodDataProvider * @param string $code * @param int $total * @param bool $expectation */ public function testIsApplicable($code, $total, $expectation) { $paymentMethod = $this->getMockBuilder('\\Magento\\Payment\\Model\\MethodInterface')->disableOriginalConstructor()->setMethods([])->getMock(); if (!$total) { $paymentMethod->expects($this->once())->method('getCode')->will($this->returnValue($code)); } $quote = $this->getMockBuilder('Magento\\Quote\\Model\\Quote')->disableOriginalConstructor()->setMethods(['getBaseGrandTotal', '__wakeup'])->getMock(); $quote->expects($this->once())->method('getBaseGrandTotal')->will($this->returnValue($total)); $model = new ZeroTotal(); $this->assertEquals($expectation, $model->isApplicable($paymentMethod, $quote)); }