/** * {@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(); }
/** * @expectedException \Exception * @expectedExceptionMessage The requested Payment Method is not available. */ public function testBuildPaymentObjectThrowsExceptionIfPaymentMethodNotAvailable() { $paymentData = ['method' => 'notAvailableMethod', 'payment_details' => 'paymentDetailsTest']; $paymentMethodMock = $this->getMock('\\Magento\\Checkout\\Service\\V1\\Data\\Cart\\PaymentMethod', [], [], '', false); $paymentMethodMock->expects($this->once())->method('__toArray')->will($this->returnValue($paymentData)); $paymentMethodMock->expects($this->once())->method('getPaymentDetails')->will($this->returnValue(['paymentDetailsTest'])); $paymentMock = $this->getMock('\\Magento\\Sales\\Model\\Quote\\Payment', [], [], '', false); $paymentMock->expects($this->once())->method('importData')->with($this->contains('notAvailableMethod'))->will($this->throwException(new \Magento\Framework\Exception\LocalizedException('The requested Payment Method is not available.'))); $quoteMock = $this->getMock('\\Magento\\Sales\\Model\\Quote', [], [], '', false); $quoteMock->expects($this->once())->method('getPayment')->will($this->returnValue($paymentMock)); $this->assertEquals($paymentMock, $this->builder->build($paymentMethodMock, $quoteMock)); }