Orders::getCurrenty()   A
last analyzed

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