Completed
Pull Request — master (#156)
by
unknown
01:44
created

Orders::getList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 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
     *
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->amount->total = $response->amount->total;
149
        $this->orders->data->amount->fees = $response->amount->fees;
150
        $this->orders->data->amount->refunds = $response->amount->refunds;
151
        $this->orders->data->amount->liquid = $response->amount->liquid;
152
        $this->orders->data->amount->otherReceivers = $response->amount->otherReceivers;
153
        $this->orders->data->amount->subtotals = $response->amount->subtotals;
154
155
        $customer = new Customer($this->moip);
156
        $this->orders->data->customer = $customer->populate($response->customer);
157
158
        $this->orders->data->payments = $this->structure($response, Payment::PATH, Payment::class);
159
        $this->orders->data->refunds = $this->structure($response, Refund::PATH, Refund::class);
160
        $this->orders->data->entries = $this->structure($response, Entry::PATH, Entry::class);
161
        $this->orders->data->events = $this->structure($response, Event::PATH, Event::class);
162
163
        $this->orders->data->items = $response->items;
164
        $this->orders->data->receivers = $response->receivers;
165
        $this->orders->data->createdAt = $response->createdAt;
166
        $this->orders->data->status = $response->status;
167
        $this->orders->data->_links = $response->_links;
168
169
        return $this->orders;
170
    }
171
172
    /**
173
     * Structure resource.
174
     *
175
     * @param stdClass                                                                               $response
176
     * @param string                                                                                 $resource
177
     * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class
178
     *
179
     * @return array
180
     */
181
    private function structure(stdClass $response, $resource, $class)
182
    {
183
        $structures = [];
184
185
        foreach ($response->$resource as $responseResource) {
186
            $structure = new $class($this->orders->moip);
187
            $structure->populate($responseResource);
188
189
            $structures[] = $structure;
190
        }
191
192
        return $structures;
193
    }
194
195
    /**
196
     * Create a new order in MoIP.
197
     *
198
     * @return \Moip\Resource\Orders|stdClass
199
     */
200
    public function create()
201
    {
202
        return $this->createResource(sprintf('/%s/%s', MoipResource::VERSION, self::PATH));
203
    }
204
205
    /**
206
     * Get an order in MoIP.
207
     *
208
     * @param string $id_moip Id MoIP order id
209
     *
210
     * @return stdClass
211
     */
212
    public function get($id_moip)
213
    {
214
        return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $id_moip));
215
    }
216
217
    /**
218
     * Get MoIP order id.
219
     *
220
     * @return string
221
     */
222
    public function getId()
223
    {
224
        return $this->getIfSet('id');
225
    }
226
227
    /**
228
     * Get own request id. external reference.
229
     *
230
     * @return string
231
     */
232
    public function getOwnId()
233
    {
234
        return $this->getIfSet('ownId');
235
    }
236
237
    /**
238
     * Get total value of order.
239
     *
240
     * @return int|float
241
     */
242
    public function getAmountTotal()
243
    {
244
        return $this->getIfSet('total', $this->data->amount);
245
    }
246
247
    /**
248
     * Get total value of MoIP rate.
249
     *
250
     * @return int|float
251
     */
252
    public function getAmountFees()
253
    {
254
        return $this->getIfSet('fees', $this->data->amount);
255
    }
256
257
    /**
258
     * Get total amount of refunds.
259
     *
260
     * @return int|float
261
     */
262
    public function getAmountRefunds()
263
    {
264
        return $this->getIfSet('refunds', $this->data->amount);
265
    }
266
267
    /**
268
     * Get net total value.
269
     *
270
     * @return int|float
271
     */
272
    public function getAmountLiquid()
273
    {
274
        return $this->getIfSet('liquid', $this->data->amount);
275
    }
276
277
    /**
278
     * Get sum of amounts received by other recipients. Used in Marketplaces.
279
     *
280
     * @return int|float
281
     */
282
    public function getAmountOtherReceivers()
283
    {
284
        return $this->getIfSet('otherReceivers', $this->data->amount);
285
    }
286
287
    /**
288
     * Get currency used in the application. Possible values: BRL.
289
     *
290
     * @return string
291
     */
292
    public function getCurrenty()
293
    {
294
        return $this->getIfSet('currency', $this->data->amount);
295
    }
296
297
    /**
298
     * Get greight value of the item will be added to the value of the items.
299
     *
300
     * @return int|float
301
     */
302
    public function getSubtotalShipping()
303
    {
304
        $this->initializeSubtotals();
305
306
        return $this->getIfSet('shipping', $this->data->amount->subtotals);
307
    }
308
309
    /**
310
     * Get Additional value to the item will be added to the value of the items.
311
     *
312
     * @return int|float
313
     */
314
    public function getSubtotalAddition()
315
    {
316
        $this->initializeSubtotals();
317
318
        return $this->getIfSet('addition', $this->data->amount->subtotals);
319
    }
320
321
    /**
322
     * Get discounted value of the item will be subtracted from the total value of the items.
323
     *
324
     * @return int|float
325
     */
326
    public function getSubtotalDiscount()
327
    {
328
        $this->initializeSubtotals();
329
330
        return $this->getIfSet('discount', $this->data->amount->subtotals);
331
    }
332
333
    /**
334
     * Get summing the values of all items.
335
     *
336
     * @return int|float
337
     */
338
    public function getSubtotalItems()
339
    {
340
        $this->initializeSubtotals();
341
342
        return $this->getIfSet('items', $this->data->amount->subtotals);
343
    }
344
345
    /**
346
     * Ger structure item information request.
347
     *
348
     * @return \ArrayIterator
349
     */
350
    public function getItemIterator()
351
    {
352
        return new ArrayIterator($this->data->items);
353
    }
354
355
    /**
356
     * Get Customer associated with the request.
357
     *
358
     * @return \Moip\Resource\Customer
359
     */
360
    public function getCustomer()
361
    {
362
        return $this->data->customer;
363
    }
364
365
    /**
366
     * Get payments associated with the request.
367
     *
368
     * @return ArrayIterator
369
     */
370
    public function getPaymentIterator()
371
    {
372
        return new ArrayIterator($this->data->payments);
373
    }
374
375
    /**
376
     * Get recipient structure of payments.
377
     *
378
     * @return ArrayIterator
379
     */
380
    public function getReceiverIterator()
381
    {
382
        return new ArrayIterator($this->data->receivers);
383
    }
384
385
    /**
386
     * Get releases associated with the request.
387
     *
388
     * @return ArrayIterator
389
     */
390
    public function getEventIterator()
391
    {
392
        return new ArrayIterator($this->data->events);
393
    }
394
395
    /**
396
     * Get repayments associated with the request.
397
     *
398
     * @return ArrayIterator
399
     */
400
    public function getRefundIterator()
401
    {
402
        return new ArrayIterator($this->data->refunds);
403
    }
404
405
    /**
406
     * Get order status.
407
     * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED.
408
     *
409
     * @return string
410
     */
411
    public function getStatus()
412
    {
413
        return $this->getIfSet('status');
414
    }
415
416
    /**
417
     * Get date of resource creation.
418
     *
419
     * @return \DateTime
420
     */
421
    public function getCreatedAt()
422
    {
423
        return $this->getIfSetDateTime('createdAt');
424
    }
425
426
    /**
427
     * Get updated resource.
428
     *
429
     * @return \DateTime
430
     */
431
    public function getUpdatedAt()
432
    {
433
        return $this->getIfSetDateTime('updatedAt');
434
    }
435
436
    /**
437
     * Get checkout preferences of the order.
438
     *
439
     * @return string
440
     */
441
    public function getCheckoutPreferences()
442
    {
443
        return $this->getIfSet('checkoutPreferences');
444
    }
445
446
    /**
447
     * Create a new Orders list instance.
448
     *
449
     * @return \Moip\Resource\OrdersList
450
     */
451
    public function getList(Pagination $pagination = null, Filters $filters = null, $qParam = '')
452
    {
453
        $orderList = new OrdersList($this->moip);
454
455
        return $orderList->get($pagination, $filters, $qParam);
456
    }
457
458
    /**
459
     * Structure of payment.
460
     *
461
     * @return \Moip\Resource\Payment
462
     */
463
    public function payments()
464
    {
465
        $payment = new Payment($this->moip);
466
        $payment->setOrder($this);
467
468
        return $payment;
469
    }
470
471
    /**
472
     * Structure of refund.
473
     *
474
     * @return \Moip\Resource\Refund
475
     */
476
    public function refunds()
477
    {
478
        $refund = new Refund($this->moip);
479
        $refund->setOrder($this);
480
481
        return $refund;
482
    }
483
484
    /**
485
     * Set additional value to the item will be added to the value of the items.
486
     *
487
     * @param int|float $value additional value to the item.
488
     *
489
     * @return $this
490
     */
491
    public function setAddition($value)
492
    {
493
        if (!isset($this->data->amount->subtotals)) {
494
            $this->data->amount->subtotals = new stdClass();
495
        }
496
        $this->data->amount->subtotals->addition = (float) $value;
497
498
        return $this;
499
    }
500
501
    /**
502
     * Set customer associated with the order.
503
     *
504
     * @param \Moip\Resource\Customer $customer customer associated with the request.
505
     *
506
     * @return $this
507
     */
508
    public function setCustomer(Customer $customer)
509
    {
510
        $this->data->customer = $customer;
511
512
        return $this;
513
    }
514
515
    /**
516
     * Set customer id associated with the order.
517
     *
518
     * @param string $id Customer's id.
519
     *
520
     * @return $this
521
     */
522
    public function setCustomerId($id)
523
    {
524
        if (!isset($this->data->customer)) {
525
            $this->data->customer = new stdClass();
526
        }
527
        $this->data->customer->id = $id;
528
529
        return $this;
530
    }
531
532
    /**
533
     * Set discounted value of the item will be subtracted from the total value of the items.
534
     *
535
     * @param int|float $value discounted value.
536
     *
537
     * @return $this
538
     */
539
    public function setDiscount($value)
540
    {
541
        $this->data->amount->subtotals->discount = (float) $value;
542
543
        return $this;
544
    }
545
546
    /**
547
     * Set discounted value of the item will be subtracted from the total value of the items.
548
     *
549
     * @deprecated
550
     *
551
     * @param int|float $value discounted value.
552
     *
553
     * @return $this
554
     */
555
    public function setDiscont($value)
556
    {
557
        $this->setDiscount($value);
558
559
        return $this;
560
    }
561
562
    /**
563
     * Set own request id. external reference.
564
     *
565
     * @param string $ownId external reference.
566
     *
567
     * @return $this
568
     */
569
    public function setOwnId($ownId)
570
    {
571
        $this->data->ownId = $ownId;
572
573
        return $this;
574
    }
575
576
    /**
577
     * Set shipping Amount.
578
     *
579
     * @param float $value shipping Amount.
580
     *
581
     * @return $this
582
     */
583
    public function setShippingAmount($value)
584
    {
585
        $this->data->amount->subtotals->shipping = (float) $value;
586
587
        return $this;
588
    }
589
590
    /**
591
     * Set URL for redirection in case of success.
592
     *
593
     * @param string $urlSuccess UrlSuccess.
594
     *
595
     * @return $this
596
     */
597
    public function setUrlSuccess($urlSuccess = '')
598
    {
599
        $this->data->checkoutPreferences->redirectUrls->urlSuccess = $urlSuccess;
600
601
        return $this;
602
    }
603
604
    /**
605
     * Set URL for redirection in case of failure.
606
     *
607
     * @param string $urlFailure UrlFailure.
608
     *
609
     * @return $this
610
     */
611
    public function setUrlFailure($urlFailure = '')
612
    {
613
        $this->data->checkoutPreferences->redirectUrls->urlFailure = $urlFailure;
614
615
        return $this;
616
    }
617
618
    /**
619
     * Set installment settings for checkout preferences.
620
     *
621
     * @param array $quantity
622
     * @param int   $discountValue
623
     * @param int   $additionalValue
624
     *
625
     * @return $this
626
     */
627
    public function addInstallmentCheckoutPreferences($quantity, $discountValue = 0, $additionalValue = 0)
628
    {
629
        $installmentPreferences = new stdClass();
630
        $installmentPreferences->quantity = $quantity;
631
        $installmentPreferences->discount = $discountValue;
632
        $installmentPreferences->addition = $additionalValue;
633
634
        $this->data->checkoutPreferences->installments[] = $installmentPreferences;
635
636
        return $this;
637
    }
638
}
639