1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TddWizard\Fixtures\Checkout; |
4
|
|
|
|
5
|
|
|
use Magento\Checkout\Model\Cart; |
6
|
|
|
use Magento\Customer\Api\AddressRepositoryInterface; |
7
|
|
|
use Magento\Framework\ObjectManagerInterface; |
8
|
|
|
use Magento\Payment\Model\Config as PaymentConfig; |
9
|
|
|
use Magento\Quote\Api\CartRepositoryInterface; |
10
|
|
|
use Magento\Quote\Model\Quote; |
11
|
|
|
use Magento\Quote\Model\QuoteManagement; |
12
|
|
|
use Magento\Sales\Api\Data\OrderInterface; |
13
|
|
|
use Magento\TestFramework\Helper\Bootstrap; |
|
|
|
|
14
|
|
|
|
15
|
|
|
class CustomerCheckout |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var AddressRepositoryInterface |
19
|
|
|
*/ |
20
|
|
|
private $addressRepository; |
21
|
|
|
/** |
22
|
|
|
* @var CartRepositoryInterface |
23
|
|
|
*/ |
24
|
|
|
private $quoteRepository; |
25
|
|
|
/** |
26
|
|
|
* @var QuoteManagement |
27
|
|
|
*/ |
28
|
|
|
private $quoteManagement; |
29
|
|
|
/** |
30
|
|
|
* @var PaymentConfig |
31
|
|
|
*/ |
32
|
|
|
private $paymentConfig; |
33
|
|
|
/** |
34
|
|
|
* @var Cart |
35
|
|
|
*/ |
36
|
|
|
private $cart; |
37
|
|
|
/** |
38
|
|
|
* @var int|null |
39
|
|
|
*/ |
40
|
|
|
private $customerShippingAddressId; |
41
|
|
|
/** |
42
|
|
|
* @var int|null |
43
|
|
|
*/ |
44
|
|
|
private $customerBillingAddressId; |
45
|
|
|
/** |
46
|
|
|
* @var string|null |
47
|
|
|
*/ |
48
|
|
|
private $shippingMethodCode; |
49
|
|
|
/** |
50
|
|
|
* @var string|null |
51
|
|
|
*/ |
52
|
|
|
private $paymentMethodCode; |
53
|
|
|
|
54
|
|
|
public function __construct( |
55
|
|
|
AddressRepositoryInterface $addressRepository, |
56
|
|
|
CartRepositoryInterface $quoteRepository, |
57
|
|
|
QuoteManagement $quoteManagement, |
58
|
|
|
PaymentConfig $paymentConfig, |
59
|
|
|
Cart $cart, |
60
|
|
|
$customerShippingAddressId = null, |
61
|
|
|
$customerBillingAddressId = null, |
62
|
|
|
$shippingMethodCode = null, |
63
|
|
|
$paymentMethodCode = null |
64
|
|
|
) { |
65
|
|
|
|
66
|
|
|
$this->addressRepository = $addressRepository; |
67
|
|
|
$this->quoteRepository = $quoteRepository; |
68
|
|
|
$this->quoteManagement = $quoteManagement; |
69
|
|
|
$this->paymentConfig = $paymentConfig; |
70
|
|
|
$this->cart = $cart; |
71
|
|
|
$this->customerShippingAddressId = $customerShippingAddressId; |
72
|
|
|
$this->customerBillingAddressId = $customerBillingAddressId; |
73
|
|
|
$this->shippingMethodCode = $shippingMethodCode; |
74
|
|
|
$this->paymentMethodCode = $paymentMethodCode; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function fromCart(Cart $cart, ObjectManagerInterface $objectManager = null) : CustomerCheckout |
78
|
|
|
{ |
79
|
|
|
if ($objectManager === null) { |
80
|
|
|
$objectManager = Bootstrap::getObjectManager(); |
81
|
|
|
} |
82
|
|
|
return new static( |
83
|
|
|
$objectManager->create(AddressRepositoryInterface::class), |
84
|
|
|
$objectManager->create(CartRepositoryInterface::class), |
85
|
|
|
$objectManager->create(QuoteManagement::class), |
86
|
|
|
$objectManager->create(PaymentConfig::class), |
87
|
|
|
$cart |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function withCustomerBillingAddressId(int $addressId) : CustomerCheckout |
92
|
|
|
{ |
93
|
|
|
$checkout = clone $this; |
94
|
|
|
$checkout->customerBillingAddressId = $addressId; |
95
|
|
|
return $checkout; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function withCustomerShippingAddressId(int $addressId) : CustomerCheckout |
99
|
|
|
{ |
100
|
|
|
$checkout = clone $this; |
101
|
|
|
$checkout->customerShippingAddressId = $addressId; |
102
|
|
|
return $checkout; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function withShippingMethodCode(string $code) : CustomerCheckout |
106
|
|
|
{ |
107
|
|
|
$checkout = clone $this; |
108
|
|
|
$checkout->shippingMethodCode = $code; |
109
|
|
|
return $checkout; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function withPaymentMethodCode(string $code) : CustomerCheckout |
113
|
|
|
{ |
114
|
|
|
$checkout = clone $this; |
115
|
|
|
$checkout->paymentMethodCode = $code; |
116
|
|
|
return $checkout; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return int Customer shipping address as configured or try default shipping address |
121
|
|
|
*/ |
122
|
|
|
private function getCustomerShippingAddressId() : int |
123
|
|
|
{ |
124
|
|
|
return $this->customerShippingAddressId |
|
|
|
|
125
|
|
|
?? $this->cart->getCustomerSession()->getCustomer()->getDefaultShippingAddress()->getId(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return int Customer billing address as configured or try default billing address |
131
|
|
|
*/ |
132
|
|
|
private function getCustomerBillingAddressId() : int |
133
|
|
|
{ |
134
|
|
|
return $this->customerBillingAddressId |
|
|
|
|
135
|
|
|
?? $this->cart->getCustomerSession()->getCustomer()->getDefaultBillingAddress()->getId(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return string Shipping method code as configured, or try first available rate |
140
|
|
|
*/ |
141
|
|
|
private function getShippingMethodCode() : string |
142
|
|
|
{ |
143
|
|
|
return $this->shippingMethodCode |
144
|
|
|
?? $this->cart->getQuote()->getShippingAddress()->getAllShippingRates()[0]->getCode(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string Payment method code as configured, or try first available method |
149
|
|
|
*/ |
150
|
|
|
private function getPaymentMethodCode() : string |
151
|
|
|
{ |
152
|
|
|
return $this->paymentMethodCode ?? array_values($this->paymentConfig->getActiveMethods())[0]->getCode(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function placeOrder() : OrderInterface |
156
|
|
|
{ |
157
|
|
|
$this->saveBilling(); |
158
|
|
|
$this->saveShipping(); |
159
|
|
|
$this->savePayment(); |
160
|
|
|
/** @var Quote $reloadedQuote */ |
161
|
|
|
$reloadedQuote = $this->quoteRepository->get($this->cart->getQuote()->getId()); |
162
|
|
|
// Collect missing totals, like shipping |
163
|
|
|
$reloadedQuote->collectTotals(); |
164
|
|
|
return $this->quoteManagement->submit($reloadedQuote); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
private function saveBilling() |
168
|
|
|
{ |
169
|
|
|
$billingAddress = $this->cart->getQuote()->getBillingAddress(); |
170
|
|
|
$billingAddress->importCustomerAddressData( |
171
|
|
|
$this->addressRepository->getById($this->getCustomerShippingAddressId()) |
172
|
|
|
); |
173
|
|
|
$billingAddress->save(); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
private function saveShipping() |
177
|
|
|
{ |
178
|
|
|
$shippingAddress = $this->cart->getQuote()->getShippingAddress(); |
179
|
|
|
$shippingAddress->importCustomerAddressData( |
180
|
|
|
$this->addressRepository->getById($this->getCustomerBillingAddressId()) |
181
|
|
|
); |
182
|
|
|
$shippingAddress->setCollectShippingRates(true); |
|
|
|
|
183
|
|
|
$shippingAddress->collectShippingRates(); |
184
|
|
|
$shippingAddress->setShippingMethod($this->getShippingMethodCode()); |
185
|
|
|
$shippingAddress->save(); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
private function savePayment() |
189
|
|
|
{ |
190
|
|
|
$payment = $this->cart->getQuote()->getPayment(); |
191
|
|
|
$payment->setMethod($this->getPaymentMethodCode()); |
192
|
|
|
$payment->save(); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths