1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Quote\Model;
use Magento\Framework\Exception\State\InvalidTransitionException;
/**
* Class PaymentMethodManagement
*/
class PaymentMethodManagement implements \Magento\Quote\Api\PaymentMethodManagementInterface
{
/**
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;
/**
* @var \Magento\Payment\Model\Checks\ZeroTotal
*/
protected $zeroTotalValidator;
/**
* @var \Magento\Payment\Model\MethodList
*/
protected $methodList;
/**
* Constructor
*
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
* @param \Magento\Payment\Model\Checks\ZeroTotal $zeroTotalValidator
* @param \Magento\Payment\Model\MethodList $methodList
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
\Magento\Payment\Model\Checks\ZeroTotal $zeroTotalValidator,
\Magento\Payment\Model\MethodList $methodList
) {
$this->quoteRepository = $quoteRepository;
$this->zeroTotalValidator = $zeroTotalValidator;
$this->methodList = $methodList;
}
/**
* {@inheritdoc}
*/
public function set($cartId, \Magento\Quote\Api\Data\PaymentInterface $method)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->get($cartId);
$quote->setTotalsCollectedFlag(false);
$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,
]);
if ($quote->isVirtual()) {
$address = $quote->getBillingAddress();
} else {
$address = $quote->getShippingAddress();
// check if shipping address is set
if ($address->getCountryId() === null) {
throw new InvalidTransitionException(
__('The shipping address is missing. Set the address and try again.')
);
}
$address->setCollectShippingRates(true);
}
$paymentData = $method->getData();
$payment = $quote->getPayment();
$payment->importData($paymentData);
$address->setPaymentMethod($payment->getMethod());
if (!$this->zeroTotalValidator->isApplicable($payment->getMethodInstance(), $quote)) {
throw new InvalidTransitionException(__('The requested Payment Method is not available.'));
}
$quote->save();
return $quote->getPayment()->getId();
}
/**
* {@inheritdoc}
*/
public function get($cartId)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->get($cartId);
$payment = $quote->getPayment();
if (!$payment->getId()) {
return null;
}
return $payment;
}
/**
* {@inheritdoc}
*/
public function getList($cartId)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->get($cartId);
return $this->methodList->getAvailableMethods($quote);
}
}