Completed
Push — master ( 69f22c...1b5410 )
by Jean C.
07:51
created

Orders::addInstallmentCheckoutPreferences()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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