1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FAPI\Sylius\Api; |
11
|
|
|
|
12
|
|
|
use FAPI\Sylius\Exception; |
13
|
|
|
use FAPI\Sylius\Exception\Domain as DomainExceptions; |
14
|
|
|
use FAPI\Sylius\Exception\InvalidArgumentException; |
15
|
|
|
use FAPI\Sylius\Model\Checkout\PaymentCollection; |
16
|
|
|
use FAPI\Sylius\Model\Checkout\ShipmentCollection; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Kasim Taskin <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class Checkout extends HttpApi |
22
|
|
|
{ |
23
|
|
|
const SHIPPING_ADDRESS_FIELDS = [ |
24
|
|
|
'firstName', |
25
|
|
|
'lastName', |
26
|
|
|
'city', |
27
|
|
|
'postcode', |
28
|
|
|
'street', |
29
|
|
|
'countryCode', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param int $id |
|
|
|
|
34
|
|
|
* |
35
|
|
|
* @throws Exception |
36
|
|
|
* |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
|
|
public function putAddress(int $cartId, array $shippingAddress, bool $differentBillingAddress = false, array $billingAddress = []): bool |
40
|
|
|
{ |
41
|
|
|
if (empty($cartId)) { |
42
|
|
|
throw new InvalidArgumentException('Cart id cannot be empty'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (empty($shippingAddress)) { |
46
|
|
|
throw new InvalidArgumentException('Shipping address cannot be empty'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
foreach (self::SHIPPING_ADDRESS_FIELDS as $field) { |
50
|
|
|
if (empty($shippingAddress[$field])) { |
51
|
|
|
throw new InvalidArgumentException("Field {$field} missing in shipping address"); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$params = [ |
56
|
|
|
'shippingAddress' => $shippingAddress, |
57
|
|
|
'differentBillingAddress' => $differentBillingAddress, |
58
|
|
|
'billingAddress' => $billingAddress, |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
$response = $this->httpPut("/api/v1/checkouts/addressing/{$cartId}", $params); |
62
|
|
|
|
63
|
|
|
// Use any valid status code here |
64
|
|
|
if (204 !== $response->getStatusCode()) { |
65
|
|
|
switch ($response->getStatusCode()) { |
66
|
|
|
case 400: |
67
|
|
|
throw new DomainExceptions\ValidationException(); |
68
|
|
|
break; |
|
|
|
|
69
|
|
|
default: |
70
|
|
|
$this->handleErrors($response); |
71
|
|
|
|
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $cartId |
81
|
|
|
* @param string $paymentMethodCode |
82
|
|
|
* |
83
|
|
|
* @throws Exception\DomainException |
84
|
|
|
* @throws Exception\Domain\ValidationException |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function putPaymentMethod(int $cartId, string $paymentMethodCode): bool |
89
|
|
|
{ |
90
|
|
|
if (empty($cartId)) { |
91
|
|
|
throw new InvalidArgumentException('Cart id cannot be empty'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (empty($paymentMethodCode)) { |
95
|
|
|
throw new InvalidArgumentException('Payment method code cannot be empty'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$params = [ |
99
|
|
|
'payments' => [ |
100
|
|
|
[ |
101
|
|
|
'method' => $paymentMethodCode, |
102
|
|
|
], |
103
|
|
|
], |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
$response = $this->httpPut("/api/v1/checkouts/select-payment/{$cartId}", $params); |
107
|
|
|
|
108
|
|
|
// Use any valid status code here |
109
|
|
|
if (204 !== $response->getStatusCode()) { |
110
|
|
|
switch ($response->getStatusCode()) { |
111
|
|
|
case 400: |
112
|
|
|
throw new DomainExceptions\ValidationException(); |
113
|
|
|
break; |
|
|
|
|
114
|
|
|
default: |
115
|
|
|
$this->handleErrors($response); |
116
|
|
|
|
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param int $cartId |
126
|
|
|
* |
127
|
|
|
* @throws Exception\DomainException |
128
|
|
|
* @throws Exception\Domain\ValidationException |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
public function complete(int $cartId) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
if (empty($cartId)) { |
135
|
|
|
throw new InvalidArgumentException('Cart id cannot be empty'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$response = $this->httpPut("/api/v1/checkouts/complete/{$cartId}"); |
139
|
|
|
|
140
|
|
|
// Use any valid status code here |
141
|
|
|
if (204 !== $response->getStatusCode()) { |
142
|
|
|
switch ($response->getStatusCode()) { |
143
|
|
|
case 400: |
144
|
|
|
throw new DomainExceptions\ValidationException(); |
145
|
|
|
break; |
|
|
|
|
146
|
|
|
default: |
147
|
|
|
$this->handleErrors($response); |
148
|
|
|
|
149
|
|
|
break; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param int $cartId |
158
|
|
|
* |
159
|
|
|
* @throws Exception\DomainException |
160
|
|
|
* @throws Exception\Domain\ValidationException |
161
|
|
|
* |
162
|
|
|
* @return ShipmentCollection |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
public function getShippingMethods(int $cartId): ShipmentCollection |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
if (empty($cartId)) { |
167
|
|
|
throw new InvalidArgumentException('Cart id cannot be empty'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$response = $this->httpGet("/api/v1/checkouts/select-shipping/{$cartId}"); |
171
|
|
|
|
172
|
|
|
// Use any valid status code here |
173
|
|
|
if (200 !== $response->getStatusCode()) { |
174
|
|
|
switch ($response->getStatusCode()) { |
175
|
|
|
case 400: |
176
|
|
|
throw new DomainExceptions\ValidationException(); |
177
|
|
|
break; |
|
|
|
|
178
|
|
|
default: |
179
|
|
|
$this->handleErrors($response); |
180
|
|
|
|
181
|
|
|
break; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $this->hydrator->hydrate($response, ShipmentCollection::class); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param int $cartId |
190
|
|
|
* |
191
|
|
|
* @throws Exception\DomainException |
192
|
|
|
* @throws Exception\Domain\ValidationException |
193
|
|
|
* |
194
|
|
|
* @return PaymentCollection |
195
|
|
|
*/ |
196
|
|
View Code Duplication |
public function getPaymentMethods(int $cartId): PaymentCollection |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
if (empty($cartId)) { |
199
|
|
|
throw new InvalidArgumentException('Cart id cannot be empty'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$response = $this->httpGet("/api/v1/checkouts/select-payment/{$cartId}"); |
203
|
|
|
|
204
|
|
|
// Use any valid status code here |
205
|
|
|
if (200 !== $response->getStatusCode()) { |
206
|
|
|
switch ($response->getStatusCode()) { |
207
|
|
|
case 400: |
208
|
|
|
throw new DomainExceptions\ValidationException(); |
209
|
|
|
break; |
|
|
|
|
210
|
|
|
default: |
211
|
|
|
$this->handleErrors($response); |
212
|
|
|
|
213
|
|
|
break; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $this->hydrator->hydrate($response, PaymentCollection::class); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.