Completed
Pull Request — master (#133)
by
unknown
01:40
created

Payment::setInstallmentCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 6
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Moip\Resource;
4
5
use Requests;
6
use stdClass;
7
8
/**
9
 * Class Payment.
10
 */
11 View Code Duplication
class Payment extends MoipResource
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Comprehensibility Best Practice introduced by
The type Moip\Resource\Payment has been defined more than once; this definition is ignored, only the first definition in src/Resource/Payment.php (L11-604) is considered.

This check looks for classes that have been defined more than once.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
12
{
13
    /**
14
     * @const string
15
     */
16
    const PATH = 'payments';
17
18
    /**
19
     * @const string
20
     */
21
    const MULTI_PAYMENTS_PATH = 'multipayments';
22
23
    /**
24
     * @const string
25
     */
26
    const SIMULATOR_PATH = 'simulador';
27
28
    /**
29
     * Payment means.
30
     *
31
     * @const string
32
     */
33
    const METHOD_CREDIT_CARD = 'CREDIT_CARD';
34
35
    /**
36
     * Payment means.
37
     *
38
     * @const string
39
     */
40
    const METHOD_BOLETO = 'BOLETO';
41
42
    /**
43
     * Payment means.
44
     *
45
     * @const string
46
     */
47
    const METHOD_ONLINE_DEBIT = 'ONLINE_DEBIT';
48
49
    /**
50
     * Payment means.
51
     *
52
     * @const string
53
     */
54
    const METHOD_WALLET = 'WALLET';
55
56
    /**
57
     * Payment means.
58
     *
59
     * @const string
60
     */
61
    const METHOD_ONLINE_BANK_DEBIT = 'ONLINE_BANK_DEBIT';
62
63
    /**
64
     * @var \Moip\Resource\Orders
65
     */
66
    private $order;
67
68
    /**
69
     * Just created, but not initialized yet.
70
     */
71
    const STATUS_CREATED = 'CREATED';
72
73
    /**
74
     * Waiting for the payment.
75
     */
76
    const STATUS_WAITING = 'WAITING';
77
78
    /**
79
     * On risk analysis, it may be automatic or manual.
80
     */
81
    const STATUS_IN_ANALYSIS = 'IN_ANALYSIS';
82
83
    /**
84
     * The amount was reserved on client credit card, it may be caught or discarded until 5 days.
85
     */
86
    const STATUS_PRE_AUTHORIZED = 'PRE_AUTHORIZED';
87
88
    /**
89
     * Payment confirmed by the bank institution.
90
     */
91
    const STATUS_AUTHORIZED = 'AUTHORIZED';
92
93
    /**
94
     * Payment cancelled.
95
     */
96
    const STATUS_CANCELLED = 'CANCELLED';
97
98
    /**
99
     * Payment refunded.
100
     */
101
    const STATUS_REFUNDED = 'REFUNDED';
102
103
    /**
104
     * Paymend reversed (it means that the payment may was not recognized by the client).
105
     */
106
    const STATUS_REVERSED = 'REVERSED';
107
108
    /**
109
     * Payment finalized, the amout is on your account.
110
     */
111
    const STATUS_SETTLED = 'SETTLED';
112
113
    /**
114
     * @var \Moip\Resource\Multiorders
115
     */
116
    private $multiorder;
117
118
    /**
119
     * Initializes new instances.
120
     */
121
    protected function initialize()
122
    {
123
        $this->data = new stdClass();
124
        $this->data->installmentCount = 1;
125
        $this->data->fundingInstrument = new stdClass();
126
    }
127
128
    /**
129
     * Create a new payment in api MoIP.
130
     *
131
     * @return $this
132
     */
133
    public function execute()
134
    {
135
        if ($this->order !== null) {
136
            $path = sprintf('/%s/%s/%s/%s', MoipResource::VERSION, Orders::PATH, $this->order->getId(), self::PATH);
137
        } else {
138
            $path = sprintf('/%s/%s/%s/%s', MoipResource::VERSION, Multiorders::PATH, $this->multiorder->getId(),
139
                self::MULTI_PAYMENTS_PATH);
140
        }
141
142
        $response = $this->httpRequest($path, Requests::POST, $this);
143
144
        return $this->populate($response);
145
    }
146
147
    /**
148
     * Get an payment and multipayment in MoIP.
149
     *
150
     * @param string $id_moip Id MoIP payment
151
     *
152
     * @return stdClass
153
     */
154
    public function get($id_moip)
155
    {
156
        if ($this->order !== null) {
157
            return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $id_moip));
158
        }
159
160
        return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::MULTI_PAYMENTS_PATH, $id_moip));
161
    }
162
163
    /**
164
     * Get id MoIP payment.
165
     *
166
     *
167
     * @return \Moip\Resource\Payment
168
     */
169
    public function getId()
170
    {
171
        return $this->getIfSet('id');
172
    }
173
174
    /**
175
     * Mount payment structure.
176
     *
177
     * @param \stdClass $response
178
     *
179
     * @return Payment
180
     */
181
    protected function populate(stdClass $response)
182
    {
183
        $payment = clone $this;
184
185
        $payment->data->id = $this->getIfSet('id', $response);
186
        $payment->data->status = $this->getIfSet('status', $response);
187
        $payment->data->delayCapture = $this->getIfSet('delayCapture', $response);
188
        $payment->data->amount = new stdClass();
189
        $payment->data->amount->total = $this->getIfSet('total', $response->amount);
190
        $payment->data->amount->currency = $this->getIfSet('currency', $response->amount);
191
        $payment->data->installmentCount = $this->getIfSet('installmentCount', $response);
192
        $payment->data->fundingInstrument = $this->getIfSet('fundingInstrument', $response);
193
        $payment->data->escrows = $this->getIfSet('escrows', $response);
194
        $payment->data->fees = $this->getIfSet('fees', $response);
195
        $payment->data->refunds = $this->getIfSet('refunds', $response);
196
        $payment->data->_links = $this->getIfSet('_links', $response);
197
        $payment->data->createdAt = $this->getIfSetDateTime('createdAt', $response);
198
        $payment->data->updatedAt = $this->getIfSetDateTime('updatedAt', $response);
199
200
        return $payment;
201
    }
202
203
    /**
204
     * Refunds.
205
     *
206
     * @return Refund
207
     */
208
    public function refunds()
209
    {
210
        $refund = new Refund($this->moip);
211
        $refund->setPayment($this);
212
213
        return $refund;
214
    }
215
    
216
    /**
217
     * Escrows.
218
     *
219
     * @return Escrow
220
     */
221
    public function escrows()
222
    {
223
        $escrow = new Escrow($this->moip);
224
        $escrow->setId($this->getEscrow()->id);
225
226
        return $escrow;
227
    }
228
229
    /**
230
     * Get payment status.
231
     *
232
     * @return string Payment status. Possible values CREATED, WAITING, IN_ANALYSIS, PRE_AUTHORIZED, AUTHORIZED, CANCELLED, REFUNDED, REVERSED, SETTLED
233
     */
234
    public function getStatus()
235
    {
236
        return $this->getIfSet('status');
237
    }
238
239
    /**
240
     * get creation time.
241
     *
242
     * @return \DateTime
243
     */
244
    public function getCreatedAt()
245
    {
246
        return $this->data->createdAt;
247
    }
248
249
    /**
250
     * Returns when the last update occurred.
251
     *
252
     * @return \DateTime
253
     */
254
    public function getUpdatedAt()
255
    {
256
        return $this->data->updatedAt;
257
    }
258
259
    /**
260
     * Returns the funding instrument.
261
     *
262
     * @return stdClass
263
     */
264
    public function getFundingInstrument()
265
    {
266
        //todo: return a funding instrument object
267
        return $this->data->fundingInstrument;
268
    }
269
270
    /**
271
     * Get href to Boleto
272
     **.
273
     *
274
     * @return stdClass
275
     */
276
    public function getHrefBoleto()
277
    {
278
        return $this->getIfSet('_links')->payBoleto->redirectHref;
279
    }
280
281
    /**
282
     * Returns payment amount.
283
     *
284
     * @return stdClass
285
     */
286
    public function getAmount()
287
    {
288
        return $this->data->amount;
289
    }
290
    
291
    /**
292
     * Returns escrow.
293
     *
294
     * @return stdClass
295
     */
296
    public function getEscrow()
297
    {
298
        return reset($this->data->escrows);
299
    }
300
301
    /**
302
     * Set means of payment.
303
     *
304
     * @param \stdClass $fundingInstrument
305
     *
306
     * @return $this
307
     */
308
    public function setFundingInstrument(stdClass $fundingInstrument)
309
    {
310
        $this->data->fundingInstrument = $fundingInstrument;
311
312
        return $this;
313
    }
314
315
    /**
316
     * Set billet.
317
     *
318
     * @param \DateTime|string $expirationDate   Expiration date of a billet.
319
     * @param string           $logoUri          Logo of billet.
320
     * @param array            $instructionLines Instructions billet.
321
     *
322
     * @return $this
323
     */
324
    public function setBoleto($expirationDate, $logoUri, array $instructionLines = [])
325
    {
326
        $keys = ['first', 'second', 'third'];
327
328
        if (empty($instructionLines)) {
329
            //Avoid warning in array_combine
330
            $instructionLines = ['', '', ''];
331
        }
332
333
        if ($expirationDate instanceof \DateTime) {
334
            $expirationDate = $expirationDate->format('Y-m-d');
335
        }
336
337
        $this->data->fundingInstrument->method = self::METHOD_BOLETO;
338
        $this->data->fundingInstrument->boleto = new stdClass();
339
        $this->data->fundingInstrument->boleto->expirationDate = $expirationDate;
340
        $this->data->fundingInstrument->boleto->instructionLines = array_combine($keys, $instructionLines);
341
        $this->data->fundingInstrument->boleto->logoUri = $logoUri;
342
343
        return $this;
344
    }
345
346
    /**
347
     * Set credit card holder.
348
     *
349
     * @param \Moip\Resource\Customer $holder
350
     */
351
    private function setCreditCardHolder(Customer $holder)
352
    {
353
        $birthdate = $holder->getBirthDate();
354
        if ($birthdate instanceof \DateTime) {
355
            $birthdate = $birthdate->format('Y-m-d');
356
        }
357
        $this->data->fundingInstrument->creditCard->holder = new stdClass();
358
        $this->data->fundingInstrument->creditCard->holder->fullname = $holder->getFullname();
359
        $this->data->fundingInstrument->creditCard->holder->birthdate = $birthdate;
360
        $this->data->fundingInstrument->creditCard->holder->taxDocument = new stdClass();
361
        $this->data->fundingInstrument->creditCard->holder->taxDocument->type = $holder->getTaxDocumentType();
362
        $this->data->fundingInstrument->creditCard->holder->taxDocument->number = $holder->getTaxDocumentNumber();
363
        $this->data->fundingInstrument->creditCard->holder->phone = new stdClass();
364
        $this->data->fundingInstrument->creditCard->holder->phone->countryCode = $holder->getPhoneCountryCode();
365
        $this->data->fundingInstrument->creditCard->holder->phone->areaCode = $holder->getPhoneAreaCode();
366
        $this->data->fundingInstrument->creditCard->holder->phone->number = $holder->getPhoneNumber();
367
        $this->data->fundingInstrument->creditCard->holder->billingAddress = $holder->getBillingAddress();
368
    }
369
370
    /**
371
     * Set credit cardHash.
372
     *
373
     * @param string                  $hash   Credit card hash encripted using Moip.js
374
     * @param \Moip\Resource\Customer $holder
375
     * @param bool                    $store  Flag to know if credit card should be saved.
376
     *
377
     * @return $this
378
     */
379
    public function setCreditCardHash($hash, Customer $holder, $store = true)
380
    {
381
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
382
        $this->data->fundingInstrument->creditCard = new stdClass();
383
        $this->data->fundingInstrument->creditCard->hash = $hash;
384
        $this->data->fundingInstrument->creditCard->store = $store;
385
        $this->setCreditCardHolder($holder);
386
387
        return $this;
388
    }
389
390
    /**
391
     * Set credit card
392
     * Credit card used in a payment.
393
     * The card when returned within a parent resource is presented in its minimum representation.
394
     *
395
     * @param int                     $expirationMonth Card expiration month
396
     * @param int                     $expirationYear  Year of card expiration.
397
     * @param string                  $number          Card number.
398
     * @param int                     $cvc             Card Security Code.
399
     * @param \Moip\Resource\Customer $holder
400
     * @param bool                    $store           Flag to know if credit card should be saved.
401
     *
402
     * @return $this
403
     */
404
    public function setCreditCard($expirationMonth, $expirationYear, $number, $cvc, Customer $holder, $store = true)
405
    {
406
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
407
        $this->data->fundingInstrument->creditCard = new stdClass();
408
        $this->data->fundingInstrument->creditCard->expirationMonth = $expirationMonth;
409
        $this->data->fundingInstrument->creditCard->expirationYear = $expirationYear;
410
        $this->data->fundingInstrument->creditCard->number = $number;
411
        $this->data->fundingInstrument->creditCard->cvc = $cvc;
412
        $this->data->fundingInstrument->creditCard->store = $store;
413
        $this->setCreditCardHolder($holder);
414
415
        return $this;
416
    }
417
418
    /**
419
     * Sets data from a previously saved credit card
420
     * Credit card used in a payment.
421
     * Used when the credit card was saved with the customer and the payment made in a future date.
422
     *
423
     * @param string $creditCardId MoIP's Credit Card Id.
424
     * @param int    $cvc          Card Security Code.
425
     *
426
     * @return $this
427
     */
428
    public function setCreditCardSaved($creditCardId, $cvc)
429
    {
430
        $this->data->fundingInstrument = new stdClass();
431
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
432
        $this->data->fundingInstrument->creditCard = new stdClass();
433
        $this->data->fundingInstrument->creditCard->id = $creditCardId;
434
        $this->data->fundingInstrument->creditCard->cvc = $cvc;
435
436
        return $this;
437
    }
438
439
    /**
440
     * Set installment count.
441
     *
442
     * @param int $installmentCount
443
     *
444
     * @return $this
445
     */
446
    public function setInstallmentCount($installmentCount)
447
    {
448
        $this->data->installmentCount = $installmentCount;
449
450
        return $this;
451
    }
452
453
    /**
454
     * Set statement descriptor.
455
     *
456
     * @param string $statementDescriptor
457
     *
458
     * @return $this
459
     */
460
    public function setStatementDescriptor($statementDescriptor)
461
    {
462
        $this->data->statementDescriptor = $statementDescriptor;
463
464
        return $this;
465
    }
466
467
    /**
468
     * Set payment means made available by banks.
469
     *
470
     * @param string           $bankNumber     Bank number. Possible values: 001, 237, 341, 041.
471
     * @param \DateTime|string $expirationDate Date of expiration debit.
472
     * @param string           $returnUri      Return Uri.
473
     *
474
     * @return $this
475
     */
476
    public function setOnlineBankDebit($bankNumber, $expirationDate, $returnUri)
477
    {
478
        if ($expirationDate instanceof \DateTime) {
479
            $expirationDate = $expirationDate->format('Y-m-d');
480
        }
481
        $this->data->fundingInstrument->method = self::METHOD_ONLINE_BANK_DEBIT;
482
        $this->data->fundingInstrument->onlineBankDebit = new stdClass();
483
        $this->data->fundingInstrument->onlineBankDebit->bankNumber = $bankNumber;
484
        $this->data->fundingInstrument->onlineBankDebit->expirationDate = $expirationDate;
485
        $this->data->fundingInstrument->onlineBankDebit->returnUri = $returnUri;
486
487
        return $this;
488
    }
489
490
    /**
491
     * Set Multiorders.
492
     *
493
     * @param \Moip\Resource\Multiorders $multiorder
494
     *
495
     * @return $this
496
     */
497
    public function setMultiorder(Multiorders $multiorder)
498
    {
499
        $this->multiorder = $multiorder;
500
501
        return $this;
502
    }
503
504
    /**
505
     * Set order.
506
     *
507
     * @param \Moip\Resource\Orders $order
508
     *
509
     * @return $this
510
     */
511
    public function setOrder(Orders $order)
512
    {
513
        $this->order = $order;
514
515
        return $this;
516
    }
517
518
    /**
519
     * Turns on a delay on credit card payment capture (pre-authorization).
520
     *
521
     * @return $this
522
     */
523
    public function setDelayCapture()
524
    {
525
        $this->data->delayCapture = true;
526
527
        return $this;
528
    }
529
    
530
    /**
531
     * Set escrow to a payment
532
     *
533
     * @param string $description
534
     * 
535
     * @return $this
536
     */
537
    public function setEscrow($description) 
538
    {
539
        $this->data->escrow = new stdClass();
540
        $this->data->escrow->description = $description;
541
542
        return $this;
543
    }
544
545
    /**
546
     * Capture a pre-authorized amount on a credit card payment.
547
     *
548
     * @throws \Exception
549
     *
550
     * @return Payment
551
     */
552
    public function capture()
553
    {
554
        if ($this->order !== null) {
555
            $path = sprintf('/%s/%s/%s/%s', MoipResource::VERSION, self::PATH, $this->getId(), 'capture');
556
        } else {
557
            throw new \Exception('Sorry, multipayment capture is not available on this version');
558
        }
559
560
        $response = $this->httpRequest($path, Requests::POST, $this);
561
562
        return $this->populate($response);
563
    }
564
565
    /**
566
     * Avoid a pre-authorized amount on a credit card payment.
567
     *
568
     * @throws \Exception
569
     *
570
     * @return Payment
571
     */
572
    public function avoid()
573
    {
574
        if ($this->order !== null) {
575
            $path = sprintf('/%s/%s/%s/%s', MoipResource::VERSION, self::PATH, $this->getId(), 'void');
576
        } else {
577
            throw new \Exception('Sorry, multipayment capture is not available on this version');
578
        }
579
580
        $response = $this->httpRequest($path, Requests::POST, $this);
581
582
        return $this->populate($response);
583
    }
584
585
    /**
586
     * Authorize a payment (Available only in sandbox to credit card payment with status IN_ANALYSIS and billet payment with status WAITING).
587
     *
588
     * @return bool
589
     */
590
    public function authorize($amount = null)
591
    {
592
        if (is_null($amount)) {
593
            $amount = $this->getAmount()->total;
594
        }
595
        $path = sprintf('/%s/%s?payment_id=%s&amount=%s', self::SIMULATOR_PATH, 'authorize', $this->getId(), $amount);
596
        $response = $this->httpRequest($path, Requests::GET);
597
598
        if (empty($response)) {
599
            return true;
600
        }
601
602
        return false;
603
    }
604
}
605