Completed
Push — master ( 49a148...c1925a )
by Jean C.
02:07
created

Payment::setCreditCardHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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