Completed
Push — master ( 2fdc09...6b4c1a )
by Jean C.
10s
created

Payment::getUpdatedAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Moip\Resource;
4
5
use Requests;
6
use stdClass;
7
8
/**
9
 * Class Payment.
10
 */
11
class Payment extends MoipResource
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->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
     * Get payment status.
217
     *
218
     * @return string Payment status. Possible values CREATED, WAITING, IN_ANALYSIS, PRE_AUTHORIZED, AUTHORIZED, CANCELLED, REFUNDED, REVERSED, SETTLED
219
     */
220
    public function getStatus()
221
    {
222
        return $this->getIfSet('status');
223
    }
224
225
    /**
226
     * get creation time.
227
     *
228
     * @return \DateTime
229
     */
230
    public function getCreatedAt()
231
    {
232
        return $this->data->createdAt;
233
    }
234
235
    /**
236
     * Returns when the last update occurred.
237
     *
238
     * @return \DateTime
239
     */
240
    public function getUpdatedAt()
241
    {
242
        return $this->data->updatedAt;
243
    }
244
245
    /**
246
     * Returns the funding instrument.
247
     *
248
     * @return stdClass
249
     */
250
    public function getFundingInstrument()
251
    {
252
        //todo: return a funding instrument object
253
        return $this->data->fundingInstrument;
254
    }
255
256
    /**
257
     * Get href to Boleto
258
     **.
259
     *
260
     * @return stdClass
261
     */
262
    public function getHrefBoleto()
263
    {
264
        return $this->getIfSet('_links')->payBoleto->redirectHref;
265
    }
266
    
267
    /**
268
     * Returns payment amount.
269
     *
270
     * @return stdClass
271
     */
272
    public function getAmount()
273
    {
274
        return $this->data->amount;
275
    }
276
277
    /**
278
     * Set means of payment.
279
     *
280
     * @param \stdClass $fundingInstrument
281
     *
282
     * @return $this
283
     */
284
    public function setFundingInstrument(stdClass $fundingInstrument)
285
    {
286
        $this->data->fundingInstrument = $fundingInstrument;
287
288
        return $this;
289
    }
290
291
    /**
292
     * Set billet.
293
     *
294
     * @param \DateTime|string $expirationDate   Expiration date of a billet.
295
     * @param string           $logoUri          Logo of billet.
296
     * @param array            $instructionLines Instructions billet.
297
     *
298
     * @return $this
299
     */
300
    public function setBoleto($expirationDate, $logoUri, array $instructionLines = [])
301
    {
302
        $keys = ['first', 'second', 'third'];
303
304
        if (empty($instructionLines)) {
305
            //Avoid warning in array_combine
306
            $instructionLines = ['', '', ''];
307
        }
308
309
        if ($expirationDate instanceof \DateTime) {
310
            $expirationDate = $expirationDate->format('Y-m-d');
311
        }
312
313
        $this->data->fundingInstrument->method = self::METHOD_BOLETO;
314
        $this->data->fundingInstrument->boleto = new stdClass();
315
        $this->data->fundingInstrument->boleto->expirationDate = $expirationDate;
316
        $this->data->fundingInstrument->boleto->instructionLines = array_combine($keys, $instructionLines);
317
        $this->data->fundingInstrument->boleto->logoUri = $logoUri;
318
319
        return $this;
320
    }
321
322
    /**
323
     * Set credit card holder.
324
     *
325
     * @param \Moip\Resource\Customer $holder
326
     */
327
    private function setCreditCardHolder(Customer $holder)
328
    {
329
        $birthdate = $holder->getBirthDate();
330
        if ($birthdate instanceof \DateTime) {
331
            $birthdate = $birthdate->format('Y-m-d');
332
        }
333
        $this->data->fundingInstrument->creditCard->holder = new stdClass();
334
        $this->data->fundingInstrument->creditCard->holder->fullname = $holder->getFullname();
335
        $this->data->fundingInstrument->creditCard->holder->birthdate = $birthdate;
336
        $this->data->fundingInstrument->creditCard->holder->taxDocument = new stdClass();
337
        $this->data->fundingInstrument->creditCard->holder->taxDocument->type = $holder->getTaxDocumentType();
338
        $this->data->fundingInstrument->creditCard->holder->taxDocument->number = $holder->getTaxDocumentNumber();
339
        $this->data->fundingInstrument->creditCard->holder->phone = new stdClass();
340
        $this->data->fundingInstrument->creditCard->holder->phone->countryCode = $holder->getPhoneCountryCode();
341
        $this->data->fundingInstrument->creditCard->holder->phone->areaCode = $holder->getPhoneAreaCode();
342
        $this->data->fundingInstrument->creditCard->holder->phone->number = $holder->getPhoneNumber();
343
        $this->data->fundingInstrument->creditCard->holder->billingAddress = $holder->getBillingAddress();
344
    }
345
346
    /**
347
     * Set credit cardHash.
348
     *
349
     * @param string                  $hash   Credit card hash encripted using Moip.js
350
     * @param \Moip\Resource\Customer $holder
351
     * @param bool                    $store  Flag to know if credit card should be saved.
352
     *
353
     * @return $this
354
     */
355
    public function setCreditCardHash($hash, Customer $holder, $store = true)
356
    {
357
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
358
        $this->data->fundingInstrument->creditCard = new stdClass();
359
        $this->data->fundingInstrument->creditCard->hash = $hash;
360
        $this->data->fundingInstrument->creditCard->store = $store;
361
        $this->setCreditCardHolder($holder);
362
363
        return $this;
364
    }
365
366
    /**
367
     * Set credit card
368
     * Credit card used in a payment.
369
     * The card when returned within a parent resource is presented in its minimum representation.
370
     *
371
     * @param int                     $expirationMonth Card expiration month
372
     * @param int                     $expirationYear  Year of card expiration.
373
     * @param string                  $number          Card number.
374
     * @param int                     $cvc             Card Security Code.
375
     * @param \Moip\Resource\Customer $holder
376
     * @param bool                    $store           Flag to know if credit card should be saved.
377
     *
378
     * @return $this
379
     */
380
    public function setCreditCard($expirationMonth, $expirationYear, $number, $cvc, Customer $holder, $store = true)
381
    {
382
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
383
        $this->data->fundingInstrument->creditCard = new stdClass();
384
        $this->data->fundingInstrument->creditCard->expirationMonth = $expirationMonth;
385
        $this->data->fundingInstrument->creditCard->expirationYear = $expirationYear;
386
        $this->data->fundingInstrument->creditCard->number = $number;
387
        $this->data->fundingInstrument->creditCard->cvc = $cvc;
388
        $this->data->fundingInstrument->creditCard->store = $store;
389
        $this->setCreditCardHolder($holder);
390
391
        return $this;
392
    }
393
394
    /**
395
     * Sets data from a previously saved credit card
396
     * Credit card used in a payment.
397
     * Used when the credit card was saved with the customer and the payment made in a future date.
398
     *
399
     * @param string $creditCardId MoIP's Credit Card Id.
400
     * @param int    $cvc          Card Security Code.
401
     *
402
     * @return $this
403
     */
404
    public function setCreditCardSaved($creditCardId, $cvc)
405
    {
406
        $this->data->fundingInstrument = new stdClass();
407
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
408
        $this->data->fundingInstrument->creditCard = new stdClass();
409
        $this->data->fundingInstrument->creditCard->id = $creditCardId;
410
        $this->data->fundingInstrument->creditCard->cvc = $cvc;
411
412
        return $this;
413
    }
414
415
    /**
416
     * Set installment count.
417
     *
418
     * @param int $installmentCount
419
     *
420
     * @return $this
421
     */
422
    public function setInstallmentCount($installmentCount)
423
    {
424
        $this->data->installmentCount = $installmentCount;
425
426
        return $this;
427
    }
428
429
    /**
430
     * Set statement descriptor.
431
     *
432
     * @param string $statementDescriptor
433
     *
434
     * @return $this
435
     */
436
    public function setStatementDescriptor($statementDescriptor)
437
    {
438
        $this->data->statementDescriptor = $statementDescriptor;
439
440
        return $this;
441
    }
442
443
    /**
444
     * Set payment means made available by banks.
445
     *
446
     * @param string           $bankNumber     Bank number. Possible values: 001, 237, 341, 041.
447
     * @param \DateTime|string $expirationDate Date of expiration debit.
448
     * @param string           $returnUri      Return Uri.
449
     *
450
     * @return $this
451
     */
452
    public function setOnlineBankDebit($bankNumber, $expirationDate, $returnUri)
453
    {
454
        if ($expirationDate instanceof \DateTime) {
455
            $expirationDate = $expirationDate->format('Y-m-d');
456
        }
457
        $this->data->fundingInstrument->method = self::METHOD_ONLINE_BANK_DEBIT;
458
        $this->data->fundingInstrument->onlineBankDebit = new stdClass();
459
        $this->data->fundingInstrument->onlineBankDebit->bankNumber = $bankNumber;
460
        $this->data->fundingInstrument->onlineBankDebit->expirationDate = $expirationDate;
461
        $this->data->fundingInstrument->onlineBankDebit->returnUri = $returnUri;
462
463
        return $this;
464
    }
465
466
    /**
467
     * Set Multiorders.
468
     *
469
     * @param \Moip\Resource\Multiorders $multiorder
470
     *
471
     * @return $this
472
     */
473
    public function setMultiorder(Multiorders $multiorder)
474
    {
475
        $this->multiorder = $multiorder;
476
477
        return $this;
478
    }
479
480
    /**
481
     * Set order.
482
     *
483
     * @param \Moip\Resource\Orders $order
484
     *
485
     * @return $this
486
     */
487
    public function setOrder(Orders $order)
488
    {
489
        $this->order = $order;
490
491
        return $this;
492
    }
493
494
    /**
495
     * Turns on a delay on credit card payment capture (pre-authorization).
496
     *
497
     * @return $this
498
     */
499
    public function setDelayCapture()
500
    {
501
        $this->data->delayCapture = true;
502
503
        return $this;
504
    }
505
506
    /**
507
     * Capture a pre-authorized amount on a credit card payment.
508
     *
509
     * @throws \Exception
510
     *
511
     * @return Payment
512
     */
513 View Code Duplication
    public function capture()
0 ignored issues
show
Duplication introduced by
This method 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...
514
    {
515
        if ($this->order !== null) {
516
            $path = sprintf('/%s/%s/%s/%s', MoipResource::VERSION, self::PATH, $this->getId(), 'capture');
517
        } else {
518
            throw new \Exception('Sorry, multipayment capture is not available on this version');
519
        }
520
521
        $response = $this->httpRequest($path, Requests::POST, $this);
522
523
        return $this->populate($response);
524
    }
525
526
    /**
527
     * Avoid a pre-authorized amount on a credit card payment.
528
     *
529
     * @throws \Exception
530
     *
531
     * @return Payment
532
     */
533 View Code Duplication
    public function avoid()
0 ignored issues
show
Duplication introduced by
This method 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...
534
    {
535
        if ($this->order !== null) {
536
            $path = sprintf('/%s/%s/%s/%s', MoipResource::VERSION, self::PATH, $this->getId(), 'void');
537
        } else {
538
            throw new \Exception('Sorry, multipayment capture is not available on this version');
539
        }
540
541
        $response = $this->httpRequest($path, Requests::POST, $this);
542
543
        return $this->populate($response);
544
    }
545
    
546
    /**
547
     * Authorize a payment (Available only in sandbox to credit card payment with status IN_ANALYSIS and billet payment with status WAITING)
548
     *
549
     * @return bool
550
     */
551
    public function authorize($amount = null)
552
    {
553
        if (is_null($amount)) {
554
            $amount = $this->getAmount()->total;
555
        }
556
        $path = sprintf('/%s/%s?payment_id=%s&amount=%s', self::SIMULATOR_PATH, 'authorize', $this->getId(), $amount);
557
        $response = $this->httpRequest($path, Requests::GET);
558
559
        if (empty($response)) {
560
            return true;
561
        }
562
563
        return false;
564
    }
565
}
566