Completed
Push — master ( 913cd3...6925fd )
by Jean C.
10s
created

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