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