Completed
Pull Request — master (#52)
by
unknown
04:33 queued 01:58
created

Payment::setDelayCapture()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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