1
|
|
|
<?php |
2
|
|
|
namespace Loevgaard\AltaPay\Payload; |
3
|
|
|
|
4
|
|
|
use Assert\Assert; |
5
|
|
|
use Loevgaard\AltaPay; |
6
|
|
|
use Money\Money; |
7
|
|
|
|
8
|
|
|
class OrderLine extends Payload implements OrderLineInterface |
9
|
|
|
{ |
10
|
|
|
const GOODS_TYPE_SHIPMENT = 'shipment'; |
11
|
|
|
const GOODS_TYPE_HANDLING = 'handling'; |
12
|
|
|
const GOODS_TYPE_ITEM = 'item'; |
13
|
|
|
const GOODS_TYPE_REFUND = 'refund'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Used to create Money objects |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $currency; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $itemId; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var float |
34
|
|
|
*/ |
35
|
|
|
protected $quantity; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
protected $unitPrice; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var float |
44
|
|
|
*/ |
45
|
|
|
protected $taxPercent; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
protected $taxAmount; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $unitCode; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var int |
59
|
|
|
*/ |
60
|
|
|
protected $discount; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $goodsType; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $imageUrl; |
71
|
|
|
|
72
|
15 |
|
public function __construct(string $description, string $itemId, float $quantity, Money $unitPrice) |
73
|
|
|
{ |
74
|
15 |
|
$this->currency = $unitPrice->getCurrency()->getCode(); |
75
|
|
|
|
76
|
15 |
|
$this->setDescription($description); |
77
|
15 |
|
$this->setItemId($itemId); |
78
|
15 |
|
$this->setQuantity($quantity); |
79
|
15 |
|
$this->setUnitPrice($unitPrice); |
80
|
15 |
|
} |
81
|
|
|
|
82
|
3 |
|
public function getPayload() : array |
83
|
|
|
{ |
84
|
3 |
|
$this->validate(); |
85
|
|
|
|
86
|
|
|
$payload = [ |
87
|
3 |
|
'description' => $this->description, |
88
|
3 |
|
'itemId' => $this->itemId, |
89
|
3 |
|
'quantity' => $this->quantity, |
90
|
3 |
|
'unitPrice' => AltaPay\floatFromMoney($this->getUnitPrice()), |
91
|
3 |
|
'taxPercent' => $this->taxPercent, |
92
|
3 |
|
'taxAmount' => AltaPay\floatFromMoney($this->getTaxAmount()), |
93
|
3 |
|
'unitCode' => $this->unitCode, |
94
|
3 |
|
'discount' => AltaPay\floatFromMoney($this->getDiscount()), |
95
|
3 |
|
'goodsType' => $this->goodsType, |
96
|
3 |
|
'imageUrl' => $this->imageUrl, |
97
|
|
|
]; |
98
|
|
|
|
99
|
3 |
|
return static::simplePayload($payload); |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
public function validate() |
103
|
|
|
{ |
104
|
3 |
|
Assert::that($this->description)->string(); |
105
|
3 |
|
Assert::that($this->itemId)->string(); |
106
|
3 |
|
Assert::that($this->quantity)->float(); |
107
|
3 |
|
Assert::that($this->getUnitPrice())->isInstanceOf(Money::class); |
108
|
3 |
|
Assert::thatNullOr($this->taxPercent)->float(); |
109
|
3 |
|
Assert::thatNullOr($this->getTaxAmount())->isInstanceOf(Money::class); |
110
|
3 |
|
Assert::thatNullOr($this->unitCode)->string(); |
111
|
3 |
|
Assert::thatNullOr($this->getDiscount())->isInstanceOf(Money::class); |
112
|
3 |
|
Assert::thatNullOr($this->goodsType)->string()->inArray(static::getGoodsTypes()); |
113
|
3 |
|
Assert::thatNullOr($this->imageUrl)->string(); |
114
|
3 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
6 |
|
public static function getGoodsTypes() : array |
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
6 |
|
self::GOODS_TYPE_HANDLING, |
123
|
6 |
|
self::GOODS_TYPE_ITEM, |
124
|
6 |
|
self::GOODS_TYPE_REFUND, |
125
|
6 |
|
self::GOODS_TYPE_SHIPMENT, |
126
|
|
|
]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
3 |
|
public function getDescription() : string |
133
|
|
|
{ |
134
|
3 |
|
return $this->description; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $description |
139
|
|
|
* @return OrderLine |
140
|
|
|
*/ |
141
|
15 |
|
public function setDescription(string $description) : self |
142
|
|
|
{ |
143
|
15 |
|
$this->description = $description; |
144
|
15 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
3 |
|
public function getItemId() : string |
151
|
|
|
{ |
152
|
3 |
|
return $this->itemId; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $itemId |
157
|
|
|
* @return OrderLine |
158
|
|
|
*/ |
159
|
15 |
|
public function setItemId(string $itemId) : self |
160
|
|
|
{ |
161
|
15 |
|
$this->itemId = $itemId; |
162
|
15 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return float |
167
|
|
|
*/ |
168
|
3 |
|
public function getQuantity() : float |
169
|
|
|
{ |
170
|
3 |
|
return $this->quantity; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param float $quantity |
175
|
|
|
* @return OrderLine |
176
|
|
|
*/ |
177
|
15 |
|
public function setQuantity(float $quantity) : self |
178
|
|
|
{ |
179
|
15 |
|
$this->quantity = $quantity; |
180
|
15 |
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return Money |
185
|
|
|
*/ |
186
|
6 |
|
public function getUnitPrice() : ?Money |
187
|
|
|
{ |
188
|
6 |
|
return AltaPay\createMoney((string)$this->currency, (int)$this->unitPrice); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param Money $unitPrice |
193
|
|
|
* @return OrderLine |
194
|
|
|
*/ |
195
|
15 |
|
public function setUnitPrice(Money $unitPrice) : self |
196
|
|
|
{ |
197
|
15 |
|
if ($unitPrice->getCurrency()->getCode() !== $this->currency) { |
198
|
|
|
throw new \InvalidArgumentException('The $unitPrice does not have the same currency as this order line'); |
199
|
|
|
} |
200
|
|
|
|
201
|
15 |
|
$this->unitPrice = $unitPrice->getAmount(); |
|
|
|
|
202
|
15 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @return float |
207
|
|
|
*/ |
208
|
3 |
|
public function getTaxPercent() : ?float |
209
|
|
|
{ |
210
|
3 |
|
return $this->taxPercent; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param float $taxPercent |
215
|
|
|
* @return OrderLine |
216
|
|
|
*/ |
217
|
3 |
|
public function setTaxPercent(float $taxPercent) : self |
218
|
|
|
{ |
219
|
3 |
|
$this->taxPercent = $taxPercent; |
220
|
3 |
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return Money |
225
|
|
|
*/ |
226
|
6 |
|
public function getTaxAmount() : ?Money |
227
|
|
|
{ |
228
|
6 |
|
if (is_null($this->taxAmount)) { |
229
|
3 |
|
return null; |
230
|
|
|
} |
231
|
|
|
|
232
|
6 |
|
return AltaPay\createMoney((string)$this->currency, (int)$this->taxAmount); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param Money $taxAmount |
237
|
|
|
* @return OrderLine |
238
|
|
|
*/ |
239
|
6 |
|
public function setTaxAmount(Money $taxAmount) : self |
240
|
|
|
{ |
241
|
6 |
|
if ($taxAmount->getCurrency()->getCode() !== $this->currency) { |
242
|
|
|
throw new \InvalidArgumentException('The $taxAmount does not have the same currency as this order line'); |
243
|
|
|
} |
244
|
|
|
|
245
|
6 |
|
$this->taxAmount = $taxAmount->getAmount(); |
|
|
|
|
246
|
6 |
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return string |
251
|
|
|
*/ |
252
|
3 |
|
public function getUnitCode() : ?string |
253
|
|
|
{ |
254
|
3 |
|
return $this->unitCode; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param string $unitCode |
259
|
|
|
* @return OrderLine |
260
|
|
|
*/ |
261
|
6 |
|
public function setUnitCode(string $unitCode) : self |
262
|
|
|
{ |
263
|
6 |
|
$this->unitCode = $unitCode; |
264
|
6 |
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return Money |
269
|
|
|
*/ |
270
|
6 |
|
public function getDiscount() : ?Money |
271
|
|
|
{ |
272
|
6 |
|
if (is_null($this->discount)) { |
273
|
3 |
|
return null; |
274
|
|
|
} |
275
|
|
|
|
276
|
3 |
|
return AltaPay\createMoney((string)$this->currency, (int)$this->discount); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param Money $discount |
281
|
|
|
* @return OrderLine |
282
|
|
|
*/ |
283
|
3 |
|
public function setDiscount(Money $discount) : self |
284
|
|
|
{ |
285
|
3 |
|
if ($discount->getCurrency()->getCode() !== $this->currency) { |
286
|
|
|
throw new \InvalidArgumentException('The $discount does not have the same currency as this order line'); |
287
|
|
|
} |
288
|
|
|
|
289
|
3 |
|
$this->discount = $discount->getAmount(); |
|
|
|
|
290
|
3 |
|
return $this; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @return string |
295
|
|
|
*/ |
296
|
3 |
|
public function getGoodsType() : ?string |
297
|
|
|
{ |
298
|
3 |
|
return $this->goodsType; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param string $goodsType |
303
|
|
|
* @return OrderLine |
304
|
|
|
*/ |
305
|
6 |
|
public function setGoodsType(string $goodsType) : self |
306
|
|
|
{ |
307
|
6 |
|
$this->goodsType = $goodsType; |
308
|
6 |
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return string |
313
|
|
|
*/ |
314
|
3 |
|
public function getImageUrl() : ?string |
315
|
|
|
{ |
316
|
3 |
|
return $this->imageUrl; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param string $imageUrl |
321
|
|
|
* @return OrderLine |
322
|
|
|
*/ |
323
|
3 |
|
public function setImageUrl(string $imageUrl) : self |
324
|
|
|
{ |
325
|
3 |
|
$this->imageUrl = $imageUrl; |
326
|
3 |
|
return $this; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.