1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TddWizard\Fixtures\Checkout; |
5
|
|
|
|
6
|
|
|
use Magento\Checkout\Model\Cart; |
7
|
|
|
use Magento\Customer\Api\AddressRepositoryInterface; |
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
|
|
|
/** |
23
|
|
|
* @var CartRepositoryInterface |
24
|
|
|
*/ |
25
|
|
|
private $quoteRepository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var QuoteManagement |
29
|
|
|
*/ |
30
|
|
|
private $quoteManagement; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var PaymentConfig |
34
|
|
|
*/ |
35
|
|
|
private $paymentConfig; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Cart |
39
|
|
|
*/ |
40
|
|
|
private $cart; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int|null |
44
|
|
|
*/ |
45
|
|
|
private $shippingAddressId; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int|null |
49
|
|
|
*/ |
50
|
|
|
private $billingAddressId; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string|null |
54
|
|
|
*/ |
55
|
|
|
private $shippingMethodCode; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string|null |
59
|
|
|
*/ |
60
|
|
|
private $paymentMethodCode; |
61
|
|
|
|
62
|
|
|
final public function __construct( |
63
|
|
|
AddressRepositoryInterface $addressRepository, |
64
|
|
|
CartRepositoryInterface $quoteRepository, |
65
|
|
|
QuoteManagement $quoteManagement, |
66
|
|
|
PaymentConfig $paymentConfig, |
67
|
|
|
Cart $cart, |
68
|
|
|
int $shippingAddressId = null, |
69
|
|
|
int $billingAddressId = null, |
70
|
|
|
string $shippingMethodCode = null, |
71
|
|
|
string $paymentMethodCode = null |
72
|
|
|
) { |
73
|
|
|
|
74
|
|
|
$this->addressRepository = $addressRepository; |
75
|
|
|
$this->quoteRepository = $quoteRepository; |
76
|
|
|
$this->quoteManagement = $quoteManagement; |
77
|
|
|
$this->paymentConfig = $paymentConfig; |
78
|
|
|
$this->cart = $cart; |
79
|
|
|
$this->shippingAddressId = $shippingAddressId; |
80
|
|
|
$this->billingAddressId = $billingAddressId; |
81
|
|
|
$this->shippingMethodCode = $shippingMethodCode; |
82
|
|
|
$this->paymentMethodCode = $paymentMethodCode; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function fromCart(Cart $cart): CustomerCheckout |
86
|
|
|
{ |
87
|
|
|
$objectManager = Bootstrap::getObjectManager(); |
88
|
|
|
return new static( |
89
|
|
|
$objectManager->create(AddressRepositoryInterface::class), |
90
|
|
|
$objectManager->create(CartRepositoryInterface::class), |
91
|
|
|
$objectManager->create(QuoteManagement::class), |
92
|
|
|
$objectManager->create(PaymentConfig::class), |
93
|
|
|
$cart |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function withCustomerBillingAddressId(int $addressId): CustomerCheckout |
98
|
|
|
{ |
99
|
|
|
$checkout = clone $this; |
100
|
|
|
$checkout->billingAddressId = $addressId; |
101
|
|
|
return $checkout; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function withCustomerShippingAddressId(int $addressId): CustomerCheckout |
105
|
|
|
{ |
106
|
|
|
$checkout = clone $this; |
107
|
|
|
$checkout->shippingAddressId = $addressId; |
108
|
|
|
return $checkout; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function withShippingMethodCode(string $code): CustomerCheckout |
112
|
|
|
{ |
113
|
|
|
$checkout = clone $this; |
114
|
|
|
$checkout->shippingMethodCode = $code; |
115
|
|
|
return $checkout; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function withPaymentMethodCode(string $code): CustomerCheckout |
119
|
|
|
{ |
120
|
|
|
$checkout = clone $this; |
121
|
|
|
$checkout->paymentMethodCode = $code; |
122
|
|
|
return $checkout; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return int Customer shipping address as configured or try default shipping address |
127
|
|
|
*/ |
128
|
|
|
private function getCustomerShippingAddressId(): int |
129
|
|
|
{ |
130
|
|
|
return $this->shippingAddressId |
131
|
|
|
?? (int) $this->cart->getCustomerSession()->getCustomer()->getDefaultShippingAddress()->getId(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return int Customer billing address as configured or try default billing address |
136
|
|
|
*/ |
137
|
|
|
private function getCustomerBillingAddressId(): int |
138
|
|
|
{ |
139
|
|
|
return $this->billingAddressId |
140
|
|
|
?? (int) $this->cart->getCustomerSession()->getCustomer()->getDefaultBillingAddress()->getId(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string Shipping method code as configured, or try first available rate |
145
|
|
|
*/ |
146
|
|
|
private function getShippingMethodCode(): string |
147
|
|
|
{ |
148
|
|
|
return $this->shippingMethodCode |
149
|
|
|
?? $this->cart->getQuote()->getShippingAddress()->getAllShippingRates()[0]->getCode(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string Payment method code as configured, or try first available method |
154
|
|
|
*/ |
155
|
|
|
private function getPaymentMethodCode(): string |
156
|
|
|
{ |
157
|
|
|
return $this->paymentMethodCode ?? array_values($this->paymentConfig->getActiveMethods())[0]->getCode(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return OrderInterface |
162
|
|
|
* @throws \Exception |
163
|
|
|
*/ |
164
|
|
|
public function placeOrder(): OrderInterface |
165
|
|
|
{ |
166
|
|
|
$this->saveBilling(); |
167
|
|
|
$this->saveShipping(); |
168
|
|
|
$this->savePayment(); |
169
|
|
|
/** @var Quote $reloadedQuote */ |
170
|
|
|
$reloadedQuote = $this->quoteRepository->get($this->cart->getQuote()->getId()); |
171
|
|
|
// Collect missing totals, like shipping |
172
|
|
|
$reloadedQuote->collectTotals(); |
173
|
|
|
$order = $this->quoteManagement->submit($reloadedQuote); |
174
|
|
|
$this->cart->getCheckoutSession()->clearQuote(); |
175
|
|
|
return $order; |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @throws \Exception |
180
|
|
|
*/ |
181
|
|
|
private function saveBilling(): void |
182
|
|
|
{ |
183
|
|
|
$billingAddress = $this->cart->getQuote()->getBillingAddress(); |
184
|
|
|
$billingAddress->importCustomerAddressData( |
185
|
|
|
$this->addressRepository->getById($this->getCustomerBillingAddressId()) |
186
|
|
|
); |
187
|
|
|
$billingAddress->save(); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @throws \Exception |
192
|
|
|
*/ |
193
|
|
|
private function saveShipping(): void |
194
|
|
|
{ |
195
|
|
|
$shippingAddress = $this->cart->getQuote()->getShippingAddress(); |
196
|
|
|
$shippingAddress->importCustomerAddressData( |
197
|
|
|
$this->addressRepository->getById($this->getCustomerShippingAddressId()) |
198
|
|
|
); |
199
|
|
|
$shippingAddress->setCollectShippingRates(true); |
200
|
|
|
$shippingAddress->collectShippingRates(); |
201
|
|
|
$shippingAddress->setShippingMethod($this->getShippingMethodCode()); |
202
|
|
|
$shippingAddress->save(); |
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @throws \Exception |
207
|
|
|
*/ |
208
|
|
|
private function savePayment(): void |
209
|
|
|
{ |
210
|
|
|
$payment = $this->cart->getQuote()->getPayment(); |
211
|
|
|
$payment->setMethod($this->getPaymentMethodCode()); |
212
|
|
|
$payment->save(); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
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