Completed
Push — master ( 096a12...e3dac2 )
by Jean C.
10s
created

Payment   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 401
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 26
lcom 2
cbo 5
dl 0
loc 401
rs 10
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 6 1
A execute() 0 12 2
A get() 0 4 1
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 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 setOnlineBankDebit() 0 13 2
A setDelayCapture() 0 6 1
A setMultiorder() 0 6 1
A setOrder() 0 6 1
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
     * @var \Moip\Resource\Multiorders
62
     */
63
    private $multiorder;
64
65
    /**
66
     * Initializes new instances.
67
     */
68
    protected function initialize()
69
    {
70
        $this->data = new stdClass();
71
        $this->data->installmentCount = 1;
72
        $this->data->fundingInstrument = new stdClass();
73
    }
74
75
    /**
76
     * Create a new payment in api MoIP.
77
     *
78
     * @return $this
79
     */
80
    public function execute()
81
    {
82
        if ($this->order !== null) {
83
            $path = sprintf('/%s/%s/%s/%s', MoipResource::VERSION, Orders::PATH, $this->order->getId(), self::PATH);
84
        } else {
85
            $path = sprintf('/%s/%s/%s/%s', MoipResource::VERSION, Multiorders::PATH, $this->multiorder->getId(),
86
                self::MULTI_PAYMENTS_PATH);
87
        }
88
        $response = $this->httpRequest($path, Requests::POST, $this);
89
90
        return $this->populate($response);
91
    }
92
93
    /**
94
     * Get an payment in MoIP.
95
     *
96
     * @param string $id_moip Id MoIP payment
97
     *
98
     * @return stdClass
99
     */
100
    public function get($id_moip)
101
    {
102
        return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $id_moip));
103
    }
104
105
    /**
106
     * Get id MoIP payment.
107
     *
108
     *
109
     * @return \Moip\Resource\Payment
110
     */
111
    public function getId()
112
    {
113
        return $this->getIfSet('id');
114
    }
115
116
    /**
117
     * Mount payment structure.
118
     *
119
     * @param \stdClass $response
120
     *
121
     * @return Payment
122
     */
123
    protected function populate(stdClass $response)
124
    {
125
        $payment = clone $this;
126
127
        $payment->data->id = $this->getIfSet('id', $response);
128
        $payment->data->status = $this->getIfSet('status', $response);
129
        $payment->data->delayCapture = $this->getIfSet('delayCapture', $response);
130
        $payment->data->amount = new stdClass();
131
        $payment->data->amount->total = $this->getIfSet('total', $response->amount);
132
        $payment->data->amount->currency = $this->getIfSet('currency', $response->amount);
133
        $payment->data->installmentCount = $this->getIfSet('installmentCount', $response);
134
        $payment->data->fundingInstrument = $this->getIfSet('fundingInstrument', $response);
135
        $payment->data->fees = $this->getIfSet('fees', $response);
136
        $payment->data->refunds = $this->getIfSet('refunds', $response);
137
        $payment->data->_links = $this->getIfSet('_links', $response);
138
        $payment->data->createdAt = $this->getIfSetDateTime('createdAt', $response);
139
        $payment->data->updatedAt = $this->getIfSetDateTime('updatedAt', $response);
140
141
        return $payment;
142
    }
143
144
    /**
145
     * Refunds.
146
     *
147
     * @return Refund
148
     */
149
    public function refunds()
150
    {
151
        $refund = new Refund($this->moip);
152
        $refund->setPayment($this);
153
154
        return $refund;
155
    }
156
	
157
	/**
158
	 * Get payment status.
159
	 *
160
	 * @return string Payment status. Possible values CREATED, WAITING, IN_ANALYSIS, PRE_AUTHORIZED, AUTHORIZED, CANCELLED, REFUNDED, REVERSED, SETTLED
161
	 */
162
	public function getStatus()
163
	{
164
		return $this->getIfSet('status');
165
	}
166
167
    /**
168
     * get creation time.
169
     *
170
     * @return \DateTime
171
     */
172
    public function getCreatedAt()
173
    {
174
        return $this->data->createdAt;
175
    }
176
177
    /**
178
     * Returns when the last update occurred.
179
     *
180
     * @return \DateTime
181
     */
182
    public function getUpdatedAt()
183
    {
184
        return $this->data->updatedAt;
185
    }
186
187
    /**
188
     * Returns the funding instrument.
189
     *
190
     * @return stdClass
191
     */
192
    public function getFundingInstrument()
193
    {
194
        //todo: return a funding instrument object
195
        return $this->data->fundingInstrument;
196
    }
197
198
    /**
199
     * Set means of payment.
200
     *
201
     * @param \stdClass $fundingInstrument
202
     *
203
     * @return $this
204
     */
205
    public function setFundingInstrument(stdClass $fundingInstrument)
206
    {
207
        $this->data->fundingInstrument = $fundingInstrument;
208
209
        return $this;
210
    }
211
212
    /**
213
     * Set billet.
214
     *
215
     * @param \DateTime|string $expirationDate   Expiration date of a billet.
216
     * @param string           $logoUri          Logo of billet.
217
     * @param array            $instructionLines Instructions billet.
218
     *
219
     * @return $this
220
     */
221
    public function setBoleto($expirationDate, $logoUri, array $instructionLines = [])
222
    {
223
        $keys = ['first', 'second', 'third'];
224
225
        if (empty($instructionLines)) {
226
            //Avoid warning in array_combine
227
            $instructionLines = ['', '', ''];
228
        }
229
230
        if ($expirationDate instanceof \DateTime) {
231
            $expirationDate = $expirationDate->format('Y-m-d');
232
        }
233
234
        $this->data->fundingInstrument->method = self::METHOD_BOLETO;
235
        $this->data->fundingInstrument->boleto = new stdClass();
236
        $this->data->fundingInstrument->boleto->expirationDate = $expirationDate;
237
        $this->data->fundingInstrument->boleto->instructionLines = array_combine($keys, $instructionLines);
238
        $this->data->fundingInstrument->boleto->logoUri = $logoUri;
239
240
        return $this;
241
    }
242
243
    /**
244
     * Set credit card holder.
245
     *
246
     * @param \Moip\Resource\Customer $holder
247
     */
248
    private function setCreditCardHolder(Customer $holder)
249
    {
250
        $birthdate = $holder->getBirthDate();
251
        if ($birthdate instanceof \DateTime) {
252
            $birthdate = $birthdate->format('Y-m-d');
253
        }
254
        $this->data->fundingInstrument->creditCard->holder = new stdClass();
255
        $this->data->fundingInstrument->creditCard->holder->fullname = $holder->getFullname();
256
        $this->data->fundingInstrument->creditCard->holder->birthdate = $birthdate;
257
        $this->data->fundingInstrument->creditCard->holder->taxDocument = new stdClass();
258
        $this->data->fundingInstrument->creditCard->holder->taxDocument->type = $holder->getTaxDocumentType();
259
        $this->data->fundingInstrument->creditCard->holder->taxDocument->number = $holder->getTaxDocumentNumber();
260
        $this->data->fundingInstrument->creditCard->holder->phone = new stdClass();
261
        $this->data->fundingInstrument->creditCard->holder->phone->countryCode = $holder->getPhoneCountryCode();
262
        $this->data->fundingInstrument->creditCard->holder->phone->areaCode = $holder->getPhoneAreaCode();
263
        $this->data->fundingInstrument->creditCard->holder->phone->number = $holder->getPhoneNumber();
264
	$this->data->fundingInstrument->creditCard->holder->billingAddress = $holder->getBillingAddress();
265
    }
266
267
    /**
268
     * Set credit cardHash.
269
     *
270
     * @param string                  $hash   Credit card hash encripted using Moip.js
271
     * @param \Moip\Resource\Customer $holder
272
     *
273
     * @return $this
274
     */
275
    public function setCreditCardHash($hash, Customer $holder)
276
    {
277
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
278
        $this->data->fundingInstrument->creditCard = new stdClass();
279
        $this->data->fundingInstrument->creditCard->hash = $hash;
280
        $this->setCreditCardHolder($holder);
281
282
        return $this;
283
    }
284
285
    /**
286
     * Set credit card
287
     * Credit card used in a payment.
288
     * The card when returned within a parent resource is presented in its minimum representation.
289
     *
290
     * @param int                     $expirationMonth Card expiration month
291
     * @param int                     $expirationYear  Year of card expiration.
292
     * @param string                  $number          Card number.
293
     * @param int                     $cvc             Card Security Code.
294
     * @param \Moip\Resource\Customer $holder
295
     *
296
     * @return $this
297
     */
298
    public function setCreditCard($expirationMonth, $expirationYear, $number, $cvc, Customer $holder)
299
    {
300
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
301
        $this->data->fundingInstrument->creditCard = new stdClass();
302
        $this->data->fundingInstrument->creditCard->expirationMonth = $expirationMonth;
303
        $this->data->fundingInstrument->creditCard->expirationYear = $expirationYear;
304
        $this->data->fundingInstrument->creditCard->number = $number;
305
        $this->data->fundingInstrument->creditCard->cvc = $cvc;
306
        $this->setCreditCardHolder($holder);
307
308
        return $this;
309
    }
310
311
    /**
312
     * Sets data from a previously saved credit card
313
     * Credit card used in a payment.
314
     * Used when the credit card was saved with the customer and the payment made in a future date.
315
     *
316
     * @param string $creditCardId MoIP's Credit Card Id.
317
     * @param int    $cvc          Card Security Code.
318
     *
319
     * @return $this
320
     */
321
    public function setCreditCardSaved($creditCardId, $cvc)
322
    {
323
        $this->data->fundingInstrument = new stdClass;
324
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
325
        $this->data->fundingInstrument->creditCard = new stdClass;
326
        $this->data->fundingInstrument->creditCard->id = $creditCardId;
327
        $this->data->fundingInstrument->creditCard->cvc = $cvc;
328
329
        return $this;
330
    }
331
332
    /**
333
     * Set installment count.
334
     *
335
     * @param int $installmentCount
336
     *
337
     * @return $this
338
     */
339
    public function setInstallmentCount($installmentCount)
340
    {
341
        $this->data->installmentCount = $installmentCount;
342
343
        return $this;
344
    }
345
346
    /**
347
     * Set payment means made available by banks.
348
     *
349
     * @param string           $bankNumber     Bank number. Possible values: 001, 237, 341, 041.
350
     * @param \DateTime|string $expirationDate Date of expiration debit.
351
     * @param string           $returnUri      Return Uri.
352
     *
353
     * @return $this
354
     */
355
    public function setOnlineBankDebit($bankNumber, $expirationDate, $returnUri)
356
    {
357
        if ($expirationDate instanceof \DateTime) {
358
            $expirationDate = $expirationDate->format('Y-m-d');
359
        }
360
        $this->data->fundingInstrument->method = self::METHOD_ONLINE_BANK_DEBIT;
361
        $this->data->fundingInstrument->onlineBankDebit = new stdClass();
362
        $this->data->fundingInstrument->onlineBankDebit->bankNumber = $bankNumber;
363
        $this->data->fundingInstrument->onlineBankDebit->expirationDate = $expirationDate;
364
        $this->data->fundingInstrument->onlineBankDebit->returnUri = $returnUri;
365
366
        return $this;
367
    }
368
369
    /**
370
     * Set delay capture.
371
     *
372
     * @return $this
373
     */
374
    public function setDelayCapture()
375
    {
376
        $this->data->delayCapture = true;
377
378
        return $this;
379
    }
380
381
    /**
382
     * Set Multiorders.
383
     *
384
     * @param \Moip\Resource\Multiorders $multiorder
385
     *
386
     * @return $this
387
     */
388
    public function setMultiorder(Multiorders $multiorder)
389
    {
390
        $this->multiorder = $multiorder;
391
392
        return $this;
393
    }
394
395
    /**
396
     * Set order.
397
     *
398
     * @param \Moip\Resource\Orders $order
399
     *
400
     * @return $this
401
     */
402
    public function setOrder(Orders $order)
403
    {
404
        $this->order = $order;
405
406
        return $this;
407
    }
408
}
409