Completed
Pull Request — master (#247)
by
unknown
14:42
created

Orders::getShippingAddressStreet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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