Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PaymentMethodManagement.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
8 namespace Magento\Quote\Model;
9 
11 
16 {
20  protected $quoteRepository;
21 
26 
30  protected $methodList;
31 
39  public function __construct(
41  \Magento\Payment\Model\Checks\ZeroTotal $zeroTotalValidator,
42  \Magento\Payment\Model\MethodList $methodList
43  ) {
44  $this->quoteRepository = $quoteRepository;
45  $this->zeroTotalValidator = $zeroTotalValidator;
46  $this->methodList = $methodList;
47  }
48 
53  {
55  $quote = $this->quoteRepository->get($cartId);
56  $quote->setTotalsCollectedFlag(false);
57  $method->setChecks([
58  \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT,
59  \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY,
60  \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY,
61  \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
62  ]);
63 
64  if ($quote->isVirtual()) {
65  $address = $quote->getBillingAddress();
66  } else {
67  $address = $quote->getShippingAddress();
68  // check if shipping address is set
69  if ($address->getCountryId() === null) {
70  throw new InvalidTransitionException(
71  __('The shipping address is missing. Set the address and try again.')
72  );
73  }
74  $address->setCollectShippingRates(true);
75  }
76 
77  $paymentData = $method->getData();
78  $payment = $quote->getPayment();
79  $payment->importData($paymentData);
80  $address->setPaymentMethod($payment->getMethod());
81 
82  if (!$this->zeroTotalValidator->isApplicable($payment->getMethodInstance(), $quote)) {
83  throw new InvalidTransitionException(__('The requested Payment Method is not available.'));
84  }
85 
86  $quote->save();
87  return $quote->getPayment()->getId();
88  }
89 
93  public function get($cartId)
94  {
96  $quote = $this->quoteRepository->get($cartId);
97  $payment = $quote->getPayment();
98  if (!$payment->getId()) {
99  return null;
100  }
101  return $payment;
102  }
103 
107  public function getList($cartId)
108  {
110  $quote = $this->quoteRepository->get($cartId);
111  return $this->methodList->getAvailableMethods($quote);
112  }
113 }
$quote
__()
Definition: __.php:13
$address
Definition: customer.php:38
$payment
Definition: order.php:17
$cartId
Definition: quote.php:22
$method
Definition: info.phtml:13
__construct(\Magento\Quote\Api\CartRepositoryInterface $quoteRepository, \Magento\Payment\Model\Checks\ZeroTotal $zeroTotalValidator, \Magento\Payment\Model\MethodList $methodList)