Completed
Pull Request — master (#41)
by
unknown
02:25
created

Payment::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
c 6
b 2
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Moip\Resource;
4
5
use Moip\Http\HTTPRequest;
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, HTTPRequest::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 $this
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->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
138
        return $payment;
139
    }
140
141
    /**
142
     * Refunds.
143
     *
144
     * @return Refund
145
     */
146
    public function refunds()
147
    {
148
        $refund = new Refund($this->moip);
149
        $refund->setPayment($this);
150
151
        return $refund;
152
    }
153
154
    /**
155
     * Set means of payment.
156
     *
157
     * @param \stdClass $fundingInstrument
158
     *
159
     * @return $this
160
     */
161
    public function setFundingInstrument(stdClass $fundingInstrument)
162
    {
163
        $this->data->fundingInstrument = $fundingInstrument;
164
165
        return $this;
166
    }
167
168
    /**
169
     * Set billet.
170
     *
171
     * @param \DateTime|string $expirationDate   Expiration date of a billet.
172
     * @param string           $logoUri          Logo of billet.
173
     * @param array            $instructionLines Instructions billet.
174
     *
175
     * @return $this
176
     */
177
    public function setBoleto($expirationDate, $logoUri, array $instructionLines = [])
178
    {
179
        $keys = ['first', 'second', 'third'];
180
181
        if (empty($instructionLines)) {
182
            //Avoid warning in array_combine
183
            $instructionLines = ['', '', ''];
184
        }
185
186
        if ($expirationDate instanceof \DateTime) {
187
            $expirationDate = $expirationDate->format('Y-m-d');
188
        }
189
190
        $this->data->fundingInstrument->method = self::METHOD_BOLETO;
191
        $this->data->fundingInstrument->boleto = new stdClass();
192
        $this->data->fundingInstrument->boleto->expirationDate = $expirationDate;
193
        $this->data->fundingInstrument->boleto->instructionLines = array_combine($keys, $instructionLines);
194
        $this->data->fundingInstrument->boleto->logoUri = $logoUri;
195
196
        return $this;
197
    }
198
199
    /**
200
     * Set credit card holder.
201
     *
202
     * @param \Moip\Resource\Customer $holder
203
     */
204
    private function setCreditCardHolder(Customer $holder)
205
    {
206
        $birthdate = $holder->getBirthDate();
207
        if ($birthdate instanceof \DateTime) {
208
            $birthdate = $birthdate->format('Y-m-d');
209
        }
210
        $this->data->fundingInstrument->creditCard->holder = new stdClass();
211
        $this->data->fundingInstrument->creditCard->holder->fullname = $holder->getFullname();
212
        $this->data->fundingInstrument->creditCard->holder->birthdate = $birthdate;
213
        $this->data->fundingInstrument->creditCard->holder->taxDocument = new stdClass();
214
        $this->data->fundingInstrument->creditCard->holder->taxDocument->type = $holder->getTaxDocumentType();
215
        $this->data->fundingInstrument->creditCard->holder->taxDocument->number = $holder->getTaxDocumentNumber();
216
        $this->data->fundingInstrument->creditCard->holder->phone = new stdClass();
217
        $this->data->fundingInstrument->creditCard->holder->phone->countryCode = $holder->getPhoneCountryCode();
218
        $this->data->fundingInstrument->creditCard->holder->phone->areaCode = $holder->getPhoneAreaCode();
219
        $this->data->fundingInstrument->creditCard->holder->phone->number = $holder->getPhoneNumber();
220
    }
221
222
    /**
223
     * Set credit cardHash.
224
     *
225
     * @param string                  $hash   Credit card hash encripted using Moip.js
226
     * @param \Moip\Resource\Customer $holder
227
     *
228
     * @return $this
229
     */
230
    public function setCreditCardHash($hash, Customer $holder)
231
    {
232
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
233
        $this->data->fundingInstrument->creditCard = new stdClass();
234
        $this->data->fundingInstrument->creditCard->hash = $hash;
235
        $this->setCreditCardHolder($holder);
236
237
        return $this;
238
    }
239
240
    /**
241
     * Set credit card
242
     * Credit card used in a payment.
243
     * The card when returned within a parent resource is presented in its minimum representation.
244
     *
245
     * @param int                     $expirationMonth Card expiration month
246
     * @param int                     $expirationYear  Year of card expiration.
247
     * @param int                     $number          Card number.
248
     * @param int                     $cvc             Card Security Code.
249
     * @param \Moip\Resource\Customer $holder
250
     *
251
     * @return $this
252
     */
253
    public function setCreditCard($expirationMonth, $expirationYear, $number, $cvc, Customer $holder)
254
    {
255
        $this->data->fundingInstrument->method = self::METHOD_CREDIT_CARD;
256
        $this->data->fundingInstrument->creditCard = new stdClass();
257
        $this->data->fundingInstrument->creditCard->expirationMonth = $expirationMonth;
258
        $this->data->fundingInstrument->creditCard->expirationYear = $expirationYear;
259
        $this->data->fundingInstrument->creditCard->number = $number;
260
        $this->data->fundingInstrument->creditCard->cvc = $cvc;
261
        $this->setCreditCardHolder($holder);
262
263
        return $this;
264
    }
265
266
    /**
267
     * Set installment count.
268
     *
269
     * @param int $installmentCount
270
     *
271
     * @return $this
272
     */
273
    public function setInstallmentCount($installmentCount)
274
    {
275
        $this->data->installmentCount = $installmentCount;
276
277
        return $this;
278
    }
279
280
    /**
281
     * Set payment means made available by banks.
282
     *
283
     * @param string           $bankNumber     Bank number. Possible values: 001, 237, 341, 041.
284
     * @param \DateTime|string $expirationDate Date of expiration debit.
285
     * @param string           $returnUri      Return Uri.
286
     *
287
     * @return $this
288
     */
289
    public function setOnlineBankDebit($bankNumber, $expirationDate, $returnUri)
290
    {
291
        if ($expirationDate instanceof \DateTime) {
292
            $expirationDate = $expirationDate->format('Y-m-d');
293
        }
294
        $this->data->fundingInstrument->method = self::METHOD_ONLINE_BANK_DEBIT;
295
        $this->data->fundingInstrument->onlineBankDebit = new stdClass();
296
        $this->data->fundingInstrument->onlineBankDebit->bankNumber = $bankNumber;
297
        $this->data->fundingInstrument->onlineBankDebit->expirationDate = $expirationDate;
298
        $this->data->fundingInstrument->onlineBankDebit->returnUri = $returnUri;
299
300
        return $this;
301
    }
302
303
    /**
304
     * Set Multiorders.
305
     *
306
     * @param \Moip\Resource\Multiorders $multiorder
307
     */
308
    public function setMultiorder(Multiorders $multiorder)
309
    {
310
        $this->multiorder = $multiorder;
311
    }
312
313
    /**
314
     * Set order.
315
     *
316
     * @param \Moip\Resource\Orders $order
317
     *
318
     * @return $this
319
     */
320
    public function setOrder(Orders $order)
321
    {
322
        $this->order = $order;
323
324
        return $this;
325
    }
326
}
327