PluginProbe
imoje / 4.2.0
imoje v4.2.0
4.16.1 4.16.0 trunk 3.1.1 3.2.0 3.2.1 3.2.3 3.2.4 3.2.6 3.2.7 3.2.8 3.3.0 3.3.1 3.3.2 3.3.3 4.0.0 4.1.0 4.1.1 4.10.1 4.11.0 4.12.0 4.14.0 4.15.0 4.15.1 4.15.2 All 39 releases
imoje / includes / libs / payment-core / src / CartData.php

CartData.php in imoje 4.2.0, at includes/libs/payment-core/src/CartData.php

326 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Imoje\Payment;
4
5 /**
6 * Class CartData
7 *
8 * @package Imoje\Payment
9 */
10 class CartData
11 {
12
13 const TYPE_SCHEMA_ITEMS = 'cartDataItems';
14 const TYPE_SCHEMA_ADDRESS = 'cartDataAddress';
15 const TYPE_SCHEMA_DISCOUNT = 'cartDataDiscount';
16 const TYPE_SCHEMA_SHIPPING = 'cartDataShipping';
17 const MAX_PREVIOUS_ORDERS = 3;
18
19 /**
20 * @var array
21 */
22 private $items;
23
24 /**
25 * @var array
26 */
27 private $addressBilling;
28
29 /**
30 * @var int
31 */
32 private $createdAt;
33
34 /**
35 * @var int
36 */
37 private $amount;
38
39 /**
40 * @var array
41 */
42 private $addressDelivery;
43
44 /**
45 * @var array
46 */
47 private $shipping = [];
48
49 /**
50 * @var array
51 */
52 private $discount = [];
53
54 /**
55 * @var CartData[]
56 */
57 private $previous = [];
58
59 /**
60 * @param mixed $id
61 * @param mixed $vat
62 * @param mixed $name
63 * @param mixed $amount
64 * @param mixed $quantity
65 * @param mixed $isUnitPrice
66 *
67 * @return void
68 */
69 public function addItem($id, $vat, $name, $amount, $quantity, $isUnitPrice)
70 {
71
72 if($isUnitPrice) {
73
74 $this->items[] = [
75 'id' => $id,
76 'vat' => $vat,
77 'name' => $name,
78 'amount' => $amount,
79 'quantity' => (int) $quantity,
80 ];
81
82 return;
83 }
84
85 $amountCalc = floor($amount / $quantity);
86
87 if((float) $amount !== ($amountCalc * $quantity)) {
88
89 $quantityCalc = $amount % $quantity;
90
91 $quantity = $quantity - $quantityCalc;
92
93 $this->items[] = [
94 'id' => $id,
95 'vat' => $vat,
96 'name' => $name,
97 'amount' => $amountCalc + 1,
98 'quantity' => $quantityCalc,
99 ];
100 }
101
102 $this->items[] = [
103 'id' => $id,
104 'vat' => $vat,
105 'name' => $name,
106 'amount' => $amountCalc,
107 'quantity' => (int) $quantity,
108 ];
109 }
110
111 /**
112 * @param int $vat
113 * @param string $name
114 * @param int $amount
115 *
116 * @return void
117 */
118 public function setDiscount($vat, $name, $amount)
119 {
120
121 $this->discount = [
122 'vat' => $vat,
123 'name' => $name,
124 'amount' => $amount,
125 ];
126 }
127
128 /**
129 * @param CartData $previousOrder
130 *
131 * @return void
132 */
133 public function addPrevious($previousOrder)
134 {
135
136 if(count($this->previous) === self::MAX_PREVIOUS_ORDERS) {
137 return;
138 }
139
140 $this->previous[] = $previousOrder;
141 }
142
143 /**
144 * @param int $vat
145 * @param string $name
146 * @param int $amount
147 *
148 * @return void
149 */
150 public function setShipping($vat, $name, $amount)
151 {
152
153 $this->shipping = [
154 'vat' => $vat,
155 'name' => $name,
156 'amount' => $amount,
157 ];
158 }
159
160 /**
161 * @param int $amount
162 *
163 * @return void
164 */
165 public function setAmount($amount)
166 {
167
168 $this->amount = $amount;
169 }
170
171 /**
172 * @param int $createdAt
173 *
174 * @return void
175 */
176 public function setCreatedAt($createdAt)
177 {
178
179 $this->createdAt = $createdAt;
180 }
181
182 /**
183 * @param string $city
184 * @param string $name
185 * @param string $phone
186 * @param string $street
187 * @param string $country
188 * @param string $postalCode
189 * @param string $vatNumber
190 *
191 * @return void
192 */
193 public function setAddressBilling($city, $name, $phone, $street, $country, $postalCode, $vatNumber = '')
194 {
195
196 $this->addressBilling = $this->commonPrepareAddress(
197 $city,
198 $name,
199 $phone,
200 $street,
201 $country,
202 $postalCode,
203 $vatNumber
204 );
205 }
206
207 /**
208 * @param string $city
209 * @param string $name
210 * @param string $phone
211 * @param string $street
212 * @param string $country
213 * @param string $postalCode
214 * @param string $vatNumber
215 *
216 * @return array
217 */
218 private function commonPrepareAddress($city, $name, $phone, $street, $country, $postalCode, $vatNumber = '')
219 {
220 $array = [
221 'city' => $city,
222 'name' => $name,
223 'phone' => (string) $phone,
224 'street' => $street,
225 'country' => $country,
226 'postalCode' => (string) $postalCode,
227 ];
228
229 if($vatNumber) {
230 $array['vatNumber'] = $vatNumber;
231 }
232
233 return $array;
234 }
235
236 /**
237 * @param string $city
238 * @param string $name
239 * @param string $phone
240 * @param string $street
241 * @param string $country
242 * @param string $postalCode
243 * @param string $vatNumber
244 *
245 * @return void
246 */
247 public function setAddressDelivery($city, $name, $phone, $street, $country, $postalCode, $vatNumber = '')
248 {
249 $this->addressDelivery = $this->commonPrepareAddress(
250 $city,
251 $name,
252 $phone,
253 $street,
254 $country,
255 $postalCode,
256 $vatNumber
257 );
258 }
259
260 /**
261 * @return array
262 */
263 public function prepareCartDataArray()
264 {
265
266 $data = [
267 'address' => [
268 'billing' => $this->addressBilling,
269 'delivery' => $this->addressDelivery,
270 ],
271 'items' => $this->items,
272 ];
273
274 if(!empty($this->discount)) {
275 $data['discount'] = $this->discount;
276 }
277
278 if(!empty($this->shipping)) {
279 $data['shipping'] = $this->shipping;
280 }
281
282 if(!empty($this->createdAt)) {
283 $data['createdAt'] = $this->createdAt;
284 }
285
286 if(!empty($this->amount)) {
287 $data['amount'] = $this->amount;
288 }
289
290 return $data;
291 }
292
293 /**
294 * @return string
295 */
296 public function prepareCartData()
297 {
298
299 $cartData = $this->prepareCartDataArray();
300
301 //if(!empty($this->previous)) {
302 // $cartData['previous'] = $this->preparePrevious($this->previous);
303 //}
304
305 return base64_encode(gzencode(json_encode($cartData), 5));
306 }
307
308 /**
309 * @param CartData[] $cartDataList
310 *
311 * @return array
312 */
313 private function preparePrevious($cartDataList)
314 {
315
316 $data = [];
317
318 foreach($cartDataList as $cartData) {
319
320 $data[] = $cartData->prepareCartDataArray();
321 }
322
323 return $data;
324 }
325 }
326