1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Order item. |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2019 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\OmniKassa2 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2; |
12
|
|
|
|
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Order item. |
17
|
|
|
* |
18
|
|
|
* @author Reüel van der Steege |
19
|
|
|
* @version 2.1.0 |
20
|
|
|
* @since 2.0.3 |
21
|
|
|
*/ |
22
|
|
|
class OrderItem { |
23
|
|
|
/** |
24
|
|
|
* Item id. |
25
|
|
|
* |
26
|
|
|
* @var string|null |
27
|
|
|
*/ |
28
|
|
|
private $id; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Item name (required). |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $name; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Description. |
39
|
|
|
* |
40
|
|
|
* @var string|null |
41
|
|
|
*/ |
42
|
|
|
private $description; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Quantity (required). |
46
|
|
|
* |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
private $quantity; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Amount (required). |
53
|
|
|
* |
54
|
|
|
* @var Money |
55
|
|
|
*/ |
56
|
|
|
private $amount; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Tax. |
60
|
|
|
* |
61
|
|
|
* @var Money|null |
62
|
|
|
*/ |
63
|
|
|
private $tax; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Category; physical or digital (required). |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
private $category; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* VAT category. |
74
|
|
|
* |
75
|
|
|
* @var int|null |
76
|
|
|
*/ |
77
|
|
|
private $vat_category; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Construct order result. |
81
|
|
|
* |
82
|
|
|
* @param string $name Name. |
83
|
|
|
* @param int $quantity Quantity. |
84
|
|
|
* @param Money $amount Amount. |
85
|
|
|
* @param string $category Category. |
86
|
|
|
* @throws InvalidArgumentException Throws invalid argument exception when arguments are invalid. |
87
|
|
|
*/ |
88
|
2 |
|
public function __construct( $name, $quantity, Money $amount, $category ) { |
89
|
2 |
|
$this->set_name( $name ); |
90
|
2 |
|
$this->quantity = $quantity; |
91
|
2 |
|
$this->amount = $amount; |
92
|
2 |
|
$this->set_category( $category ); |
93
|
2 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get item ID. |
97
|
|
|
* |
98
|
|
|
* @return string|null |
99
|
|
|
*/ |
100
|
|
|
public function get_id() { |
101
|
|
|
return $this->id; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set item ID. |
106
|
|
|
* |
107
|
|
|
* @param string|null $id ID. |
108
|
|
|
*/ |
109
|
1 |
|
public function set_id( $id = null ) { |
110
|
1 |
|
$this->id = $id; |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get item name. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
1 |
|
public function get_name() { |
119
|
1 |
|
return $this->name; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set item name. |
124
|
|
|
* |
125
|
|
|
* @param string $name Name. |
126
|
|
|
* @throws InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 50`. |
127
|
|
|
*/ |
128
|
2 |
|
public function set_name( $name ) { |
129
|
2 |
|
DataHelper::validate_an( $name, 50 ); |
130
|
|
|
|
131
|
2 |
|
$this->name = $name; |
132
|
2 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get item description. |
136
|
|
|
* |
137
|
|
|
* @return string|null |
138
|
|
|
*/ |
139
|
|
|
public function get_description() { |
140
|
|
|
return $this->description; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set item description. |
145
|
|
|
* |
146
|
|
|
* @param string|null $description Description. |
147
|
|
|
* @throws InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 100`. |
148
|
|
|
*/ |
149
|
1 |
|
public function set_description( $description ) { |
150
|
1 |
|
if ( null !== $description ) { |
151
|
1 |
|
DataHelper::validate_an( $description, 100 ); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
$this->description = $description; |
155
|
1 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get quantity. |
159
|
|
|
* |
160
|
|
|
* @return int |
161
|
|
|
*/ |
162
|
|
|
public function get_quantity() { |
163
|
|
|
return $this->quantity; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get amount. |
168
|
|
|
* |
169
|
|
|
* @return Money |
170
|
|
|
*/ |
171
|
|
|
public function get_amount() { |
172
|
|
|
return $this->amount; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get tax. |
177
|
|
|
* |
178
|
|
|
* @return Money|null |
179
|
|
|
*/ |
180
|
|
|
public function get_tax() { |
181
|
|
|
return $this->tax; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Set tax. |
186
|
|
|
* |
187
|
|
|
* @param Money|null $tax Tax. |
188
|
|
|
*/ |
189
|
1 |
|
public function set_tax( Money $tax = null ) { |
190
|
1 |
|
$this->tax = $tax; |
191
|
1 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get category. |
195
|
|
|
* |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function get_category() { |
199
|
|
|
return $this->category; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Set category. |
204
|
|
|
* |
205
|
|
|
* @param string $category Product category: PHYSICAL or DIGITAL. |
206
|
|
|
* @throws InvalidArgumentException Throws invalid argument exception when value does not apply to format `AN..max 8`. |
207
|
|
|
*/ |
208
|
2 |
|
public function set_category( $category ) { |
209
|
2 |
|
DataHelper::validate_an( $category, 8 ); |
210
|
|
|
|
211
|
2 |
|
$this->category = $category; |
212
|
2 |
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get VAT category. |
216
|
|
|
* |
217
|
|
|
* @return int|null |
218
|
|
|
*/ |
219
|
|
|
public function get_vat_category() { |
220
|
|
|
return $this->vat_category; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Set VAT category. |
225
|
|
|
* |
226
|
|
|
* @param int|null $vat_category VAT category. |
227
|
|
|
*/ |
228
|
1 |
|
public function set_vat_category( $vat_category ) { |
229
|
1 |
|
$this->vat_category = $vat_category; |
230
|
1 |
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get JSON. |
234
|
|
|
* |
235
|
|
|
* @return object |
236
|
|
|
*/ |
237
|
|
|
public function get_json() { |
238
|
|
|
$object = (object) array(); |
239
|
|
|
|
240
|
|
|
if ( null !== $this->id ) { |
241
|
|
|
$object->id = $this->id; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$object->name = $this->name; |
245
|
|
|
|
246
|
|
|
if ( null !== $this->description ) { |
247
|
|
|
$object->description = $this->description; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$object->quantity = $this->quantity; |
251
|
|
|
$object->amount = $this->amount->get_json(); |
252
|
|
|
|
253
|
|
|
if ( null !== $this->tax ) { |
254
|
|
|
$object->tax = $this->tax->get_json(); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$object->category = $this->category; |
258
|
|
|
|
259
|
|
|
if ( null !== $this->vat_category ) { |
260
|
|
|
$object->vatCategory = $this->vat_category; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $object; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get signature fields. |
268
|
|
|
* |
269
|
|
|
* @param array $fields Fields. |
270
|
|
|
* @return array |
271
|
|
|
*/ |
272
|
1 |
|
public function get_signature_fields( $fields = array() ) { |
273
|
1 |
|
if ( null !== $this->id ) { |
274
|
1 |
|
$fields[] = $this->id; |
275
|
|
|
} |
276
|
|
|
|
277
|
1 |
|
$fields[] = $this->name; |
278
|
1 |
|
$fields[] = $this->description; |
279
|
1 |
|
$fields[] = strval( $this->quantity ); |
280
|
|
|
|
281
|
1 |
|
$fields = $this->amount->get_signature_fields( $fields ); |
282
|
|
|
|
283
|
1 |
|
if ( null === $this->tax ) { |
284
|
|
|
$fields[] = null; |
285
|
|
|
} else { |
286
|
1 |
|
$fields = $this->tax->get_signature_fields( $fields ); |
287
|
|
|
} |
288
|
|
|
|
289
|
1 |
|
$fields[] = $this->category; |
290
|
|
|
|
291
|
1 |
|
if ( null !== $this->vat_category ) { |
292
|
1 |
|
$fields[] = $this->vat_category; |
293
|
|
|
} |
294
|
|
|
|
295
|
1 |
|
return $fields; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|