Completed
Push — master ( dcf0f2...da9d55 )
by Jean C.
12s
created

Payment   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 515
Duplicated Lines 4.66 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 24
loc 515
rs 9.3999
wmc 33
lcom 2
cbo 5

25 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 6 1
A execute() 0 13 2
A getId() 0 4 1
A populate() 0 20 1
A refunds() 0 7 1
A getStatus() 0 4 1
A getCreatedAt() 0 4 1
A getUpdatedAt() 0 4 1
A getFundingInstrument() 0 5 1
A get() 0 8 2
A getHrefBoleto() 0 4 1
A setFundingInstrument() 0 6 1
A setBoleto() 0 21 3
A setCreditCardHolder() 0 18 2
A setCreditCardHash() 0 9 1
A setCreditCard() 0 12 1
A setCreditCardSaved() 0 10 1
A setInstallmentCount() 0 6 1
A setStatementDescriptor() 0 6 1
A setOnlineBankDebit() 0 13 2
A setMultiorder() 0 6 1
A setOrder() 0 6 1
A setDelayCapture() 0 6 1
A capture() 12 12 2
A avoid() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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