Completed
Push — master ( 1b5410...64fdd0 )
by Jean C.
28s
created

Payment::setEscrow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 7
loc 7
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-601) 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(), self::MULTI_PAYMENTS_PATH);
139
        }
140
141
        $response = $this->httpRequest($path, Requests::POST, $this);
142
143
        return $this->populate($response);
144
    }
145
146
    /**
147
     * Get an payment and multipayment in MoIP.
148
     *
149
     * @param string $id_moip Id MoIP payment
150
     *
151
     * @return stdClass
152
     */
153
    public function get($id_moip)
154
    {
155
        if ($this->order !== null) {
156
            return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $id_moip));
157
        }
158
159
        return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::MULTI_PAYMENTS_PATH, $id_moip));
160
    }
161
162
    /**
163
     * Get id MoIP payment.
164
     *
165
     *
166
     * @return \Moip\Resource\Payment
167
     */
168
    public function getId()
169
    {
170
        return $this->getIfSet('id');
171
    }
172
173
    /**
174
     * Mount payment structure.
175
     *
176
     * @param \stdClass $response
177
     *
178
     * @return Payment
179
     */
180
    protected function populate(stdClass $response)
181
    {
182
        $payment = clone $this;
183
184
        $payment->data->id = $this->getIfSet('id', $response);
185
        $payment->data->status = $this->getIfSet('status', $response);
186
        $payment->data->delayCapture = $this->getIfSet('delayCapture', $response);
187
        $payment->data->amount = new stdClass();
188
        $payment->data->amount->total = $this->getIfSet('total', $response->amount);
189
        $payment->data->amount->currency = $this->getIfSet('currency', $response->amount);
190
        $payment->data->installmentCount = $this->getIfSet('installmentCount', $response);
191
        $payment->data->fundingInstrument = $this->getIfSet('fundingInstrument', $response);
192
        $payment->data->escrows = $this->getIfSet('escrows', $response);
193
        $payment->data->fees = $this->getIfSet('fees', $response);
194
        $payment->data->refunds = $this->getIfSet('refunds', $response);
195
        $payment->data->_links = $this->getIfSet('_links', $response);
196
        $payment->data->createdAt = $this->getIfSetDateTime('createdAt', $response);
197
        $payment->data->updatedAt = $this->getIfSetDateTime('updatedAt', $response);
198
199
        return $payment;
200
    }
201
202
    /**
203
     * Refunds.
204
     *
205
     * @return Refund
206
     */
207
    public function refunds()
208
    {
209
        $refund = new Refund($this->moip);
210
        $refund->setPayment($this);
211
212
        return $refund;
213
    }
214
215
    /**
216
     * Escrows.
217
     *
218
     * @return Escrow
219
     */
220
    public function escrows()
221
    {
222
        $escrow = new Escrow($this->moip);
223
        $escrow->setId($this->getEscrow()->id);
224
225
        return $escrow;
226
    }
227
228
    /**
229
     * Get payment status.
230
     *
231
     * @return string Payment status. Possible values CREATED, WAITING, IN_ANALYSIS, PRE_AUTHORIZED, AUTHORIZED, CANCELLED, REFUNDED, REVERSED, SETTLED
232
     */
233
    public function getStatus()
234
    {
235
        return $this->getIfSet('status');
236
    }
237
238
    /**
239
     * get creation time.
240
     *
241
     * @return \DateTime
242
     */
243
    public function getCreatedAt()
244
    {
245
        return $this->data->createdAt;
246
    }
247
248
    /**
249
     * Returns when the last update occurred.
250
     *
251
     * @return \DateTime
252
     */
253
    public function getUpdatedAt()
254
    {
255
        return $this->data->updatedAt;
256
    }
257
258
    /**
259
     * Returns the funding instrument.
260
     *
261
     * @return stdClass
262
     */
263
    public function getFundingInstrument()
264
    {
265
        //todo: return a funding instrument object
266
        return $this->data->fundingInstrument;
267
    }
268
269
    /**
270
     * Get href to Boleto
271
     * *.
272
     *
273
     * @return stdClass
274
     */
275
    public function getHrefBoleto()
276
    {
277
        return $this->getIfSet('_links')->payBoleto->redirectHref;
278
    }
279
280
    /**
281
     * Returns payment amount.
282
     *
283
     * @return stdClass
284
     */
285
    public function getAmount()
286
    {
287
        return $this->data->amount;
288
    }
289
290
    /**
291
     * Returns escrow.
292
     *
293
     * @return stdClass
294
     */
295
    public function getEscrow()
296
    {
297
        return reset($this->data->escrows);
298
    }
299
300
    /**
301
     * Set means of payment.
302
     *
303
     * @param \stdClass $fundingInstrument
304
     *
305
     * @return $this
306
     */
307
    public function setFundingInstrument(stdClass $fundingInstrument)
308
    {
309
        $this->data->fundingInstrument = $fundingInstrument;
310
311
        return $this;
312
    }
313
314
    /**
315
     * Set billet.
316
     *
317
     * @param \DateTime|string $expirationDate   Expiration date of a billet.
318
     * @param string           $logoUri          Logo of billet.
319
     * @param array            $instructionLines Instructions billet.
320
     *
321
     * @return $this
322
     */
323
    public function setBoleto($expirationDate, $logoUri, array $instructionLines = [])
324
    {
325
        $keys = ['first', 'second', 'third'];
326
327
        if (empty($instructionLines)) {
328
            //Avoid warning in array_combine
329
            $instructionLines = ['', '', ''];
330
        }
331
332
        if ($expirationDate instanceof \DateTime) {
333
            $expirationDate = $expirationDate->format('Y-m-d');
334
        }
335
336
        $this->data->fundingInstrument->method = self::METHOD_BOLETO;
337
        $this->data->fundingInstrument->boleto = new stdClass();
338
        $this->data->fundingInstrument->boleto->expirationDate = $expirationDate;
339
        $this->data->fundingInstrument->boleto->instructionLines = array_combine($keys, $instructionLines);
340
        $this->data->fundingInstrument->boleto->logoUri = $logoUri;
341
342
        return $this;
343
    }
344
345
    /**
346
     * Set credit card holder.
347
     *
348
     * @param \Moip\Resource\Customer $holder
349
     */
350
    private function setCreditCardHolder(Customer $holder)
351
    {
352
        $birthdate = $holder->getBirthDate();
353
        if ($birthdate instanceof \DateTime) {
354
            $birthdate = $birthdate->format('Y-m-d');
355
        }
356
        $this->data->fundingInstrument->creditCard->holder = new stdClass();
357
        $this->data->fundingInstrument->creditCard->holder->fullname = $holder->getFullname();
358
        $this->data->fundingInstrument->creditCard->holder->birthdate = $birthdate;
359
        $this->data->fundingInstrument->creditCard->holder->taxDocument = new stdClass();
360
        $this->data->fundingInstrument->creditCard->holder->taxDocument->type = $holder->getTaxDocumentType();
361
        $this->data->fundingInstrument->creditCard->holder->taxDocument->number = $holder->getTaxDocumentNumber();
362
        $this->data->fundingInstrument->creditCard->holder->phone = new stdClass();
363
        $this->data->fundingInstrument->creditCard->holder->phone->countryCode = $holder->getPhoneCountryCode();
364
        $this->data->fundingInstrument->creditCard->holder->phone->areaCode = $holder->getPhoneAreaCode();
365
        $this->data->fundingInstrument->creditCard->holder->phone->number = $holder->getPhoneNumber();
366
        $this->data->fundingInstrument->creditCard->holder->billingAddress = $holder->getBillingAddress();
367
    }
368
369
    /**
370
     * Set credit cardHash.
371
     *
372
     * @param string                  $hash   Credit card hash encripted using Moip.js
373
     * @param \Moip\Resource\Customer $holder
374
     * @param bool                    $store  Flag to know if credit card should be saved.
375
     *
376
     * @return $this
377
     */
378
    public function setCreditCardHash($hash, Customer $holder, $store = true)
379
    {
380
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
381
        $this->data->fundingInstrument->creditCard = new stdClass();
382
        $this->data->fundingInstrument->creditCard->hash = $hash;
383
        $this->data->fundingInstrument->creditCard->store = $store;
384
        $this->setCreditCardHolder($holder);
385
386
        return $this;
387
    }
388
389
    /**
390
     * Set credit card
391
     * Credit card used in a payment.
392
     * The card when returned within a parent resource is presented in its minimum representation.
393
     *
394
     * @param int                     $expirationMonth Card expiration month
395
     * @param int                     $expirationYear  Year of card expiration.
396
     * @param string                  $number          Card number.
397
     * @param int                     $cvc             Card Security Code.
398
     * @param \Moip\Resource\Customer $holder
399
     * @param bool                    $store           Flag to know if credit card should be saved.
400
     *
401
     * @return $this
402
     */
403
    public function setCreditCard($expirationMonth, $expirationYear, $number, $cvc, Customer $holder, $store = true)
404
    {
405
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
406
        $this->data->fundingInstrument->creditCard = new stdClass();
407
        $this->data->fundingInstrument->creditCard->expirationMonth = $expirationMonth;
408
        $this->data->fundingInstrument->creditCard->expirationYear = $expirationYear;
409
        $this->data->fundingInstrument->creditCard->number = $number;
410
        $this->data->fundingInstrument->creditCard->cvc = $cvc;
411
        $this->data->fundingInstrument->creditCard->store = $store;
412
        $this->setCreditCardHolder($holder);
413
414
        return $this;
415
    }
416
417
    /**
418
     * Sets data from a previously saved credit card
419
     * Credit card used in a payment.
420
     * Used when the credit card was saved with the customer and the payment made in a future date.
421
     *
422
     * @param string $creditCardId MoIP's Credit Card Id.
423
     * @param int    $cvc          Card Security Code.
424
     *
425
     * @return $this
426
     */
427
    public function setCreditCardSaved($creditCardId, $cvc)
428
    {
429
        $this->data->fundingInstrument = new stdClass();
430
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
431
        $this->data->fundingInstrument->creditCard = new stdClass();
432
        $this->data->fundingInstrument->creditCard->id = $creditCardId;
433
        $this->data->fundingInstrument->creditCard->cvc = $cvc;
434
435
        return $this;
436
    }
437
438
    /**
439
     * Set installment count.
440
     *
441
     * @param int $installmentCount
442
     *
443
     * @return $this
444
     */
445
    public function setInstallmentCount($installmentCount)
446
    {
447
        $this->data->installmentCount = $installmentCount;
448
449
        return $this;
450
    }
451
452
    /**
453
     * Set statement descriptor.
454
     *
455
     * @param string $statementDescriptor
456
     *
457
     * @return $this
458
     */
459
    public function setStatementDescriptor($statementDescriptor)
460
    {
461
        $this->data->statementDescriptor = $statementDescriptor;
462
463
        return $this;
464
    }
465
466
    /**
467
     * Set payment means made available by banks.
468
     *
469
     * @param string           $bankNumber     Bank number. Possible values: 001, 237, 341, 041.
470
     * @param \DateTime|string $expirationDate Date of expiration debit.
471
     * @param string           $returnUri      Return Uri.
472
     *
473
     * @return $this
474
     */
475
    public function setOnlineBankDebit($bankNumber, $expirationDate, $returnUri)
476
    {
477
        if ($expirationDate instanceof \DateTime) {
478
            $expirationDate = $expirationDate->format('Y-m-d');
479
        }
480
        $this->data->fundingInstrument->method = self::METHOD_ONLINE_BANK_DEBIT;
481
        $this->data->fundingInstrument->onlineBankDebit = new stdClass();
482
        $this->data->fundingInstrument->onlineBankDebit->bankNumber = $bankNumber;
483
        $this->data->fundingInstrument->onlineBankDebit->expirationDate = $expirationDate;
484
        $this->data->fundingInstrument->onlineBankDebit->returnUri = $returnUri;
485
486
        return $this;
487
    }
488
489
    /**
490
     * Set Multiorders.
491
     *
492
     * @param \Moip\Resource\Multiorders $multiorder
493
     *
494
     * @return $this
495
     */
496
    public function setMultiorder(Multiorders $multiorder)
497
    {
498
        $this->multiorder = $multiorder;
499
500
        return $this;
501
    }
502
503
    /**
504
     * Set order.
505
     *
506
     * @param \Moip\Resource\Orders $order
507
     *
508
     * @return $this
509
     */
510
    public function setOrder(Orders $order)
511
    {
512
        $this->order = $order;
513
514
        return $this;
515
    }
516
517
    /**
518
     * Turns on a delay on credit card payment capture (pre-authorization).
519
     *
520
     * @return $this
521
     */
522
    public function setDelayCapture()
523
    {
524
        $this->data->delayCapture = true;
525
526
        return $this;
527
    }
528
529
    /**
530
     * Set escrow to a payment.
531
     *
532
     * @param string $description
533
     *
534
     * @return $this
535
     */
536
    public function setEscrow($description)
537
    {
538
        $this->data->escrow = new stdClass();
539
        $this->data->escrow->description = $description;
540
541
        return $this;
542
    }
543
544
    /**
545
     * Capture a pre-authorized amount on a credit card payment.
546
     *
547
     * @throws \Exception
548
     *
549
     * @return Payment
550
     */
551
    public function capture()
552
    {
553
        if (is_null($this->order)) {
554
            throw new \Exception('Sorry, multipayment capture is not available on this version');
555
        }
556
        $path = sprintf('/%s/%s/%s/%s', MoipResource::VERSION, self::PATH, $this->getId(), 'capture');
557
558
        $response = $this->httpRequest($path, Requests::POST, $this);
559
560
        return $this->populate($response);
561
    }
562
563
    /**
564
     * Avoid a pre-authorized amount on a credit card payment.
565
     *
566
     * @throws \Exception
567
     *
568
     * @return Payment
569
     */
570
    public function avoid()
571
    {
572
        if (is_null($this->order)) {
573
            throw new \Exception('Sorry, multipayment capture is not available on this version');
574
        }
575
        $path = sprintf('/%s/%s/%s/%s', MoipResource::VERSION, self::PATH, $this->getId(), 'void');
576
577
        $response = $this->httpRequest($path, Requests::POST, $this);
578
579
        return $this->populate($response);
580
    }
581
582
    /**
583
     * Authorize a payment (Available only in sandbox to credit card payment with status IN_ANALYSIS and billet payment with status WAITING).
584
     *
585
     * @return bool
586
     */
587
    public function authorize($amount = null)
588
    {
589
        if (is_null($amount)) {
590
            $amount = $this->getAmount()->total;
591
        }
592
        $path = sprintf('/%s/%s?payment_id=%s&amount=%s', self::SIMULATOR_PATH, 'authorize', $this->getId(), $amount);
593
        $response = $this->httpRequest($path, Requests::GET);
594
595
        if (empty($response)) {
596
            return true;
597
        }
598
599
        return false;
600
    }
601
}
602