Completed
Push — master ( 4fb3b2...b5a0c8 )
by Jean C.
10s
created

Orders::getEventIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Moip\Resource;
4
5
use ArrayIterator;
6
use stdClass;
7
8
/**
9
 * Class Orders.
10
 */
11
class Orders extends MoipResource
12
{
13
    /**
14
     * @const string
15
     */
16
    const PATH = 'orders';
17
18
    /**
19
     * Defines what kind of payee as pripmary.
20
     *
21
     * @const string
22
     */
23
    const RECEIVER_TYPE_PRIMARY = 'PRIMARY';
24
25
    /**
26
     * Defines what kind of payee as secundary.
27
     *
28
     * @const string
29
     */
30
    const RECEIVER_TYPE_SECONDARY = 'SECONDARY';
31
32
    /**
33
     * Currency used in the application.
34
     *
35
     * @const string
36
     */
37
    const AMOUNT_CURRENCY = 'BRL';
38
39
    /**
40
     * @var \Moip\Resource\Orders
41
     **/
42
    private $orders;
43
44
    /**
45
     * Adds a new item to order.
46
     *
47
     * @param string $product  Name of the product.
48
     * @param int    $quantity Product Quantity.
49
     * @param string $detail   Additional product description.
50
     * @param int    $price    Initial value of the item.
51
     *
52
     * @return $this
53
     */
54
    public function addItem($product, $quantity, $detail, $price)
55
    {
56
        if (!is_int($price)) {
57
            throw new \UnexpectedValueException('Informe o valor do item como inteiro');
58
        }
59
60
        if (!is_int($quantity) || $quantity < 1) {
61
            throw new \UnexpectedValueException('A quantidade do item deve ser um valor inteiro maior que 0');
62
        }
63
64
        $item = new stdClass();
65
        $item->product = $product;
66
        $item->quantity = $quantity;
67
        $item->detail = $detail;
68
        $item->price = $price;
69
70
        $this->data->items[] = $item;
71
72
        return $this;
73
    }
74
75
    /**
76
     *  Adds a new receiver to order.
77
     *
78
     * @param string $moipAccount Id MoIP MoIP account that will receive payment values.
79
     * @param string $type        Define qual o tipo de recebedor do pagamento, valores possíveis: PRIMARY, SECONDARY.
80
     * @param int    $fixed       Value that the receiver will receive.
81
     * @param int    $percentual  Percentual value that the receiver will receive. Possible values: 0 - 100
82
     * @param bool   $feePayor    Flag to know if receiver is the payer of Moip tax.
83
     *
84
     * @return $this
85
     */
86
    public function addReceiver($moipAccount, $type, $fixed = NULL, $percentual = NULL, $feePayor = false)
87
    {
88
        $receiver = new stdClass();
89
        $receiver->moipAccount = new stdClass();
90
        $receiver->moipAccount->id = $moipAccount;
91
        if (!empty($fixed)) {
92
            $receiver->amount = new stdClass();
93
            $receiver->amount->fixed = $fixed;
94
        }
95
        if (!empty($percentual)) {
96
            $receiver->amount = new stdClass();
97
            $receiver->amount->percentual = $percentual;
98
        }
99
        $receiver->feePayor = $feePayor;
100
        $receiver->type = $type;
101
102
        $this->data->receivers[] = $receiver;
103
104
        return $this;
105
    }
106
107
    /**
108
     * Initialize necessary used in some functions.
109
     */
110
    protected function initialize()
111
    {
112
        $this->data = new stdClass();
113
        $this->data->ownId = null;
114
        $this->data->amount = new stdClass();
115
        $this->data->amount->currency = self::AMOUNT_CURRENCY;
116
        $this->data->amount->subtotals = new stdClass();
117
        $this->data->items = [];
118
        $this->data->receivers = [];
119
        $this->data->checkoutPreferences = new stdClass();
120
        $this->data->checkoutPreferences->redirectUrls = new stdClass();
121
    }
122
123
    /**
124
     * Initialize necessary used in some functions.
125
     */
126
    private function initializeSubtotals()
127
    {
128
        if (!isset($this->data->subtotals)) {
129
            $this->data->subtotals = new stdClass();
130
        }
131
    }
132
133
    /**
134
     * Mount the structure of order.
135
     *
136
     * @param \stdClass $response
137
     *
138
     * @return Orders Response order.
139
     */
140
    protected function populate(stdClass $response)
141
    {
142
        $this->orders = clone $this;
143
        $this->orders->data->id = $response->id;
144
        $this->orders->data->ownId = $response->ownId;
145
        $this->orders->data->amount->total = $response->amount->total;
146
        $this->orders->data->amount->fees = $response->amount->fees;
147
        $this->orders->data->amount->refunds = $response->amount->refunds;
148
        $this->orders->data->amount->liquid = $response->amount->liquid;
149
        $this->orders->data->amount->otherReceivers = $response->amount->otherReceivers;
150
        $this->orders->data->amount->subtotals = $response->amount->subtotals;
151
152
        $customer = new Customer($this->moip);
153
        $this->orders->data->customer = $customer->populate($response->customer);
154
155
        $this->orders->data->payments = $this->structure($response, Payment::PATH, Payment::class);
156
        $this->orders->data->refunds = $this->structure($response, Refund::PATH, Refund::class);
157
        $this->orders->data->entries = $this->structure($response, Entry::PATH, Entry::class);
158
        $this->orders->data->events = $this->structure($response, Event::PATH, Event::class);
159
160
        $this->orders->data->items = $response->items;
161
        $this->orders->data->receivers = $response->receivers;
162
        $this->orders->data->createdAt = $response->createdAt;
163
        $this->orders->data->status = $response->status;
164
        $this->orders->data->_links = $response->_links;
165
166
        return $this->orders;
167
    }
168
169
    /**
170
     * Structure resource.
171
     *
172
     * @param stdClass                                                                               $response
173
     * @param string                                                                                 $resource
174
     * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class
175
     *
176
     * @return array
177
     */
178
    private function structure(stdClass $response, $resource, $class)
179
    {
180
        $structures = [];
181
182
        foreach ($response->$resource as $responseResource) {
183
            $structure = new $class($this->orders->moip);
184
            $structure->populate($responseResource);
185
186
            $structures[] = $structure;
187
        }
188
189
        return $structures;
190
    }
191
192
    /**
193
     * Create a new order in MoIP.
194
     *
195
     * @return \Moip\Resource\Orders|stdClass
196
     */
197
    public function create()
198
    {
199
        return $this->createResource(sprintf('/%s/%s', MoipResource::VERSION, self::PATH));
200
    }
201
202
    /**
203
     * Get an order in MoIP.
204
     *
205
     * @param string $id_moip Id MoIP order id
206
     *
207
     * @return stdClass
208
     */
209
    public function get($id_moip)
210
    {
211
        return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $id_moip));
212
    }
213
214
    /**
215
     * Get MoIP order id.
216
     *
217
     * @return string
218
     */
219
    public function getId()
220
    {
221
        return $this->getIfSet('id');
222
    }
223
224
    /**
225
     * Get own request id. external reference.
226
     *
227
     * @return string
228
     */
229
    public function getOwnId()
230
    {
231
        return $this->getIfSet('ownId');
232
    }
233
234
    /**
235
     * Get total value of order.
236
     *
237
     * @return int|float
238
     */
239
    public function getAmountTotal()
240
    {
241
        return $this->getIfSet('total', $this->data->amount);
242
    }
243
244
    /**
245
     * Get total value of MoIP rate.
246
     *
247
     * @return int|float
248
     */
249
    public function getAmountFees()
250
    {
251
        return $this->getIfSet('fees', $this->data->amount);
252
    }
253
254
    /**
255
     * Get total amount of refunds.
256
     *
257
     * @return int|float
258
     */
259
    public function getAmountRefunds()
260
    {
261
        return $this->getIfSet('refunds', $this->data->amount);
262
    }
263
264
    /**
265
     * Get net total value.
266
     *
267
     * @return int|float
268
     */
269
    public function getAmountLiquid()
270
    {
271
        return $this->getIfSet('liquid', $this->data->amount);
272
    }
273
274
    /**
275
     * Get sum of amounts received by other recipients. Used in Marketplaces.
276
     *
277
     * @return int|float
278
     */
279
    public function getAmountOtherReceivers()
280
    {
281
        return $this->getIfSet('otherReceivers', $this->data->amount);
282
    }
283
284
    /**
285
     * Get currency used in the application. Possible values: BRL.
286
     *
287
     * @return string
288
     */
289
    public function getCurrenty()
290
    {
291
        return $this->getIfSet('currency', $this->data->amount);
292
    }
293
294
    /**
295
     * Get greight value of the item will be added to the value of the items.
296
     *
297
     * @return int|float
298
     */
299
    public function getSubtotalShipping()
300
    {
301
        $this->initializeSubtotals();
302
303
        return $this->getIfSet('shipping', $this->data->amount->subtotals);
304
    }
305
306
    /**
307
     * Get Additional value to the item will be added to the value of the items.
308
     *
309
     * @return int|float
310
     */
311
    public function getSubtotalAddition()
312
    {
313
        $this->initializeSubtotals();
314
315
        return $this->getIfSet('addition', $this->data->amount->subtotals);
316
    }
317
318
    /**
319
     * Get discounted value of the item will be subtracted from the total value of the items.
320
     *
321
     * @return int|float
322
     */
323
    public function getSubtotalDiscount()
324
    {
325
        $this->initializeSubtotals();
326
327
        return $this->getIfSet('discount', $this->data->amount->subtotals);
328
    }
329
330
    /**
331
     * Get summing the values of all items.
332
     *
333
     * @return int|float
334
     */
335
    public function getSubtotalItems()
336
    {
337
        $this->initializeSubtotals();
338
339
        return $this->getIfSet('items', $this->data->amount->subtotals);
340
    }
341
342
    /**
343
     * Ger structure item information request.
344
     *
345
     * @return \ArrayIterator
346
     */
347
    public function getItemIterator()
348
    {
349
        return new ArrayIterator($this->data->items);
350
    }
351
352
    /**
353
     * Get Customer associated with the request.
354
     *
355
     * @return \Moip\Resource\Customer
356
     */
357
    public function getCustomer()
358
    {
359
        return $this->data->customer;
360
    }
361
362
    /**
363
     * Get payments associated with the request.
364
     *
365
     * @return ArrayIterator
366
     */
367
    public function getPaymentIterator()
368
    {
369
        return new ArrayIterator($this->data->payments);
370
    }
371
372
    /**
373
     * Get recipient structure of payments.
374
     *
375
     * @return ArrayIterator
376
     */
377
    public function getReceiverIterator()
378
    {
379
        return new ArrayIterator($this->data->receivers);
380
    }
381
382
    /**
383
     * Get releases associated with the request.
384
     *
385
     * @return ArrayIterator
386
     */
387
    public function getEventIterator()
388
    {
389
        return new ArrayIterator($this->data->events);
390
    }
391
392
    /**
393
     * Get repayments associated with the request.
394
     *
395
     * @return ArrayIterator
396
     */
397
    public function getRefundIterator()
398
    {
399
        return new ArrayIterator($this->data->refunds);
400
    }
401
402
    /**
403
     * Get order status.
404
     * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED.
405
     *
406
     * @return string
407
     */
408
    public function getStatus()
409
    {
410
        return $this->getIfSet('status');
411
    }
412
413
    /**
414
     * Get date of resource creation.
415
     *
416
     * @return \DateTime
417
     */
418
    public function getCreatedAt()
419
    {
420
        return $this->getIfSetDateTime('createdAt');
421
    }
422
423
    /**
424
     * Get updated resource.
425
     *
426
     * @return \DateTime
427
     */
428
    public function getUpdatedAt()
429
    {
430
        return $this->getIfSetDateTime('updatedAt');
431
    }
432
433
    /**
434
     * Get checkout preferences of the order.
435
     *
436
     * @return string
437
     */
438
    public function getCheckoutPreferences()
439
    {
440
        return $this->getIfSet('checkoutPreferences');
441
    }
442
443
    /**
444
     * Structure of payment.
445
     *
446
     * @return \Moip\Resource\Payment
447
     */
448
    public function payments()
449
    {
450
        $payment = new Payment($this->moip);
451
        $payment->setOrder($this);
452
453
        return $payment;
454
    }
455
456
    /**
457
     * Structure of refund.
458
     *
459
     * @return \Moip\Resource\Refund
460
     */
461
    public function refunds()
462
    {
463
        $refund = new Refund($this->moip);
464
        $refund->setOrder($this);
465
466
        return $refund;
467
    }
468
469
    /**
470
     * Set additional value to the item will be added to the value of the items.
471
     *
472
     * @param int|float $value additional value to the item.
473
     *
474
     * @return $this
475
     */
476
    public function setAddition($value)
477
    {
478
        if (!isset($this->data->amount->subtotals)) {
479
            $this->data->amount->subtotals = new stdClass();
480
        }
481
        $this->data->amount->subtotals->addition = (float) $value;
482
483
        return $this;
484
    }
485
486
    /**
487
     * Set customer associated with the order.
488
     *
489
     * @param \Moip\Resource\Customer $customer customer associated with the request.
490
     *
491
     * @return $this
492
     */
493
    public function setCustomer(Customer $customer)
494
    {
495
        $this->data->customer = $customer;
496
497
        return $this;
498
    }
499
500
    /**
501
     * Set customer id associated with the order.
502
     *
503
     * @param string $id Customer's id.
504
     *
505
     * @return $this
506
     */
507
    public function setCustomerId($id)
508
    {
509
        if (!isset($this->data->customer)) {
510
            $this->data->customer = new stdClass();
511
        }
512
        $this->data->customer->id = $id;
513
514
        return $this;
515
    }
516
517
    /**
518
     * Set discounted value of the item will be subtracted from the total value of the items.
519
     *
520
     * @param int|float $value discounted value.
521
     *
522
     * @return $this
523
     */
524
    public function setDiscount($value)
525
    {
526
        $this->data->amount->subtotals->discount = (float) $value;
527
528
        return $this;
529
    }
530
531
    /**
532
     * Set discounted value of the item will be subtracted from the total value of the items.
533
     *
534
     * @deprecated
535
     *
536
     * @param int|float $value discounted value.
537
     *
538
     * @return $this
539
     */
540
    public function setDiscont($value)
541
    {
542
        $this->setDiscount($value);
543
544
        return $this;
545
    }
546
547
    /**
548
     * Set own request id. external reference.
549
     *
550
     * @param string $ownId external reference.
551
     *
552
     * @return $this
553
     */
554
    public function setOwnId($ownId)
555
    {
556
        $this->data->ownId = $ownId;
557
558
        return $this;
559
    }
560
561
    /**
562
     * Set shipping Amount.
563
     *
564
     * @param float $value shipping Amount.
565
     *
566
     * @return $this
567
     */
568
    public function setShippingAmount($value)
569
    {
570
        $this->data->amount->subtotals->shipping = (float) $value;
571
572
        return $this;
573
    }
574
575
    /**
576
     * Set URL for redirection in case of success.
577
     *
578
     * @param string $urlSuccess UrlSuccess.
579
     *
580
     * @return $this
581
     */
582
    public function setUrlSuccess($urlSuccess = '')
583
    {
584
        $this->data->checkoutPreferences->redirectUrls->urlSuccess = $urlSuccess;
585
586
        return $this;
587
    }
588
589
    /**
590
     * Set URL for redirection in case of failure.
591
     *
592
     * @param string $urlFailure UrlFailure.
593
     *
594
     * @return $this
595
     */
596
    public function setUrlFailure($urlFailure = '')
597
    {
598
        $this->data->checkoutPreferences->redirectUrls->urlFailure = $urlFailure;
599
600
        return $this;
601
    }
602
    
603
    /**
604
     * Set installment settings for checkout preferences
605
     *
606
     * @param array $quantity
607
     * @param int $discountValue
608
     * @param int $additionalValue
609
     *
610
     * @return $this
611
     */
612
    public function setInstallmentCheckoutPreferences($quantity, $discountValue = 0, $additionalValue = 0)
613
    {
614
        $this->data->checkoutPreferences->installments = new stdClass();
615
        $this->data->checkoutPreferences->installments->quantity = $quantity;
616
        $this->data->checkoutPreferences->installments->discount = $discountValue;
617
        $this->data->checkoutPreferences->installments->addition = $additionalValue;
618
619
        return $this;
620
    }
621
}
622