Completed
Push — master ( 4517c4...dc88ca )
by
unknown
05:54
created

Refund::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
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 ArrayIterator;
6
use Requests;
7
use stdClass;
8
9
/**
10
 * Class Refund.
11
 */
12
class Refund extends MoipResource
13
{
14
    /**
15
     * @const string
16
     */
17
    const PATH = 'refunds';
18
19
    /**
20
     * Refunds means.
21
     *
22
     * @const string
23
     */
24
    const METHOD_CREDIT_CARD = 'CREDIT_CARD';
25
26
    /**
27
     * Refunds means.
28
     *
29
     * @const string
30
     */
31
    const METHOD_BANK_ACCOUNT = 'BANK_ACCOUNT';
32
33
    /**
34
     * Refunds means.
35
     *
36
     * @const string
37
     */
38
    const METHOD_MONEY_ORDER = 'MONEY_ORDER';
39
40
    /**
41
     * @var \Moip\Resource\Orders
42
     */
43
    private $order;
44
45
    /**
46
     * @var \Moip\Resource\Payment
47
     */
48
    private $payment;
49
50
    /**
51
     * Initializes new instances.
52
     */
53
    public function initialize()
54
    {
55
        $this->data = new stdClass();
56
    }
57
58
    /**
59
     * Mount refund structure.
60
     *
61
     * @param \stdClass $response
62
     *
63
     * @return $this
64
     */
65
    protected function populate(stdClass $response)
66
    {
67
        $refund = clone $this;
68
69
        $refund->data->id = $this->getIfSet('id', $response);
70
71
        if (isset($response->amount)) {
72
            $refund->data->amount = new stdClass();
73
            $refund->data->amount->total = $this->getIfSet('total', $response->amount);
74
            $refund->data->amount->discounted = $this->getIfSet('discounted', $response->amount);
75
            $refund->data->amount->currency = $this->getIfSet('currency', $response->amount);
76
        }
77
78
        $refund->data->fee = $this->getIfSet('fee', $response);
79
        $refund->data->createdAt = $this->getIfSet('createdAt', $response);
80
81
        if (isset($response->refundingInstrument)) {
82
            $refund->data->refundingInstrument = new stdClass();
83
            $refund->data->refundingInstrument->method = $this->getIfSet('method', $response->refundingInstrument);
84
85
            if (isset($response->refundingInstrument->bankAccount)) {
86
                $refund->data->refundingInstrument->bankAccount = new stdClass();
87
                $refund->data->refundingInstrument->bankAccount->bankNumber = $this->getIfSet('bankNumber', $response->refundingInstrument->bankAccount);
88
                $refund->data->refundingInstrument->bankAccount->bankName = $this->getIfSet('bankName', $response->refundingInstrument->bankAccount);
89
                $refund->data->refundingInstrument->bankAccount->agencyNumber = $this->getIfSet('agencyNumber', $response->refundingInstrument->bankAccount);
90
                $refund->data->refundingInstrument->bankAccount->agencyCheckNumber = $this->getIfSet('agencyCheckNumber', $response->refundingInstrument->bankAccount);
91
                $refund->data->refundingInstrument->bankAccount->accountNumber = $this->getIfSet('accountNumber', $response->refundingInstrument->bankAccount);
92
                $refund->data->refundingInstrument->bankAccount->accountCheckNumber = $this->getIfSet('accountCheckNumber', $response->refundingInstrument->bankAccount);
93
                $refund->data->refundingInstrument->bankAccount->agencyCheckNumber = $this->getIfSet('agencyCheckNumber', $response->refundingInstrument->bankAccount);
94
                $refund->data->refundingInstrument->bankAccount->type = $this->getIfSet('type', $response->refundingInstrument->bankAccount);
95
            }
96
        }
97
98
        $refund->data->type = $this->getIfSet('type', $response);
99
        $refund->data->status = $this->getIfSet('status', $response);
100
        $refund->data->method = $this->getIfSet('method', $response);
101
        $refund->data->createdAt = $this->getIfSet('createdAt', $response);
102
        $refund->data->_links = $this->getIfSet('_links', $response);
103
104
        return $refund;
105
    }
106
107
    /**
108
     * Get id MoIP refund.
109
     *
110
     *
111
     * @return string
112
     */
113
    public function getId()
114
    {
115
        return $this->getIfSet('id');
116
    }
117
118
    /**
119
     * Get status from MoIP refund.
120
     *
121
     *
122
     * @return string
123
     */
124
    public function getStatus()
125
    {
126
        return $this->getIfSet('status');
127
    }
128
129
    /**
130
     * Get MoIP refund type.
131
     *
132
     *
133
     * @return string
134
     */
135
    public function getType()
136
    {
137
        return $this->getIfSet('type');
138
    }
139
140
    /**
141
     * Create a new refund in api MoIP.
142
     *
143
     * @param stdClass $data
144
     *
145
     * @return $this
146
     */
147
    private function execute(stdClass $data = null, $resourceId = null)
148
    {
149
        $body = empty($data) ? new stdClass() : $data;
150
        $response = $this->httpRequest($this->getPath($resourceId), Requests::POST, $body);
151
152
        return $this->populate($response);
153
    }
154
155
    /**
156
     * Checks path that will be the request.
157
     *
158
     * @return string
159
     */
160
    private function getPath($resourceId = null)
161
    {
162
        if (!is_null($resourceId)) {
163
            $endpoint = ($this->isOrder($resourceId) ? Orders::PATH : Payment::PATH);
164
165
            return sprintf('/%s/%s/%s/%s', MoipResource::VERSION, $endpoint, $resourceId, self::PATH);
166
        }
167
168
        if ($this->order !== null) {
169
            return sprintf('/%s/%s/%s/%s', MoipResource::VERSION, Orders::PATH, $this->order->getId(), self::PATH);
170
        }
171
172
        return sprintf('/%s/%s/%s/%s', MoipResource::VERSION, Payment::PATH, $this->payment->getId(), self::PATH);
173
    }
174
175
    /**
176
     * Bank account is the bank address of a particular vendor or a customer.
177
     *
178
     * @param string                  $type               Kind of bank account. possible values: CHECKING, SAVING.
179
     * @param string                  $bankNumber         Bank number. possible values: 001, 237, 341, 041.
180
     * @param int                     $agencyNumber       Branch number.
181
     * @param int                     $agencyCheckNumber  Checksum of the agency.
182
     * @param int                     $accountNumber      Account number.
183
     * @param int                     $accountCheckNumber Digit account checker.
184
     * @param \Moip\Resource\Customer $holder
185
     *
186
     * @return \stdClass
187
     */
188
    private function bankAccountDataCustomer($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, Customer $holder)
189
    {
190
        $data = new stdClass();
191
        $data->refundingInstrument = new stdClass();
192
        $data->refundingInstrument->method = self::METHOD_BANK_ACCOUNT;
193
        $data->refundingInstrument->bankAccount = new stdClass();
194
        $data->refundingInstrument->bankAccount->type = $type;
195
        $data->refundingInstrument->bankAccount->bankNumber = $bankNumber;
196
        $data->refundingInstrument->bankAccount->agencyNumber = $agencyNumber;
197
        $data->refundingInstrument->bankAccount->agencyCheckNumber = $agencyCheckNumber;
198
        $data->refundingInstrument->bankAccount->accountNumber = $accountNumber;
199
        $data->refundingInstrument->bankAccount->accountCheckNumber = $accountCheckNumber;
200
        $data->refundingInstrument->bankAccount->holder = new stdClass();
201
        $data->refundingInstrument->bankAccount->holder->fullname = $holder->getFullname();
202
        $data->refundingInstrument->bankAccount->holder->taxDocument = new stdClass();
203
        $data->refundingInstrument->bankAccount->holder->taxDocument->type = $holder->getTaxDocumentType();
204
        $data->refundingInstrument->bankAccount->holder->taxDocument->number = $holder->getTaxDocumentNumber();
205
206
        return $data;
207
    }
208
209
    /**
210
     * Bank account is the bank address of a particular vendor or a customer.
211
     *
212
     * @param \Moip\Resource\BankAccount $bankAccount
213
     *
214
     * @return \stdClass
215
     */
216
    private function bankAccountData(BankAccount $bankAccount)
217
    {
218
        $data = new stdClass();
219
        $data->refundingInstrument = new stdClass();
220
        $data->refundingInstrument->method = self::METHOD_BANK_ACCOUNT;
221
        $data->refundingInstrument->bankAccount = new stdClass();
222
        $data->refundingInstrument->bankAccount->type = $bankAccount->getType();
223
        $data->refundingInstrument->bankAccount->bankNumber = $bankAccount->getBankNumber();
224
        $data->refundingInstrument->bankAccount->agencyNumber = $bankAccount->getAgencyNumber();
225
        $data->refundingInstrument->bankAccount->agencyCheckNumber = $bankAccount->getAgencyCheckNumber();
226
        $data->refundingInstrument->bankAccount->accountNumber = $bankAccount->getAccountNumber();
227
        $data->refundingInstrument->bankAccount->accountCheckNumber = $bankAccount->getAccountCheckNumber();
228
        $data->refundingInstrument->bankAccount->holder = new stdClass();
229
        $data->refundingInstrument->bankAccount->holder->fullname = $bankAccount->getFullname();
230
        $data->refundingInstrument->bankAccount->holder->taxDocument = new stdClass();
231
        $data->refundingInstrument->bankAccount->holder->taxDocument->type = $bankAccount->getTaxDocumentType();
232
        $data->refundingInstrument->bankAccount->holder->taxDocument->number = $bankAccount->getTaxDocumentNumber();
233
234
        return $data;
235
    }
236
237
    /**
238
     * Making a full refund to the bank account.
239
     *
240
     * @param string                  $type               Kind of bank account. possible values: CHECKING, SAVING.
241
     * @param string                  $bankNumber         Bank number. possible values: 001, 237, 341, 041.
242
     * @param int                     $agencyNumber       Branch number.
243
     * @param int                     $agencyCheckNumber  Checksum of the agency.
244
     * @param int                     $accountNumber      Account number.
245
     * @param int                     $accountCheckNumber Digit account checker.
246
     * @param \Moip\Resource\Customer $holder
247
     *
248
     * @return Refund
249
     */
250
    public function bankAccountFull($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, Customer $holder)
251
    {
252
        $data = $this->bankAccountDataCustomer($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, $holder);
253
254
        return $this->execute($data);
255
    }
256
257
    /**
258
     * Making a partial refund in the bank account.
259
     *
260
     * @param string                  $type               Kind of bank account. possible values: CHECKING, SAVING.
261
     * @param string                  $bankNumber         Bank number. possible values: 001, 237, 341, 041.
262
     * @param int                     $agencyNumber       Branch number.
263
     * @param int                     $agencyCheckNumber  Checksum of the agency.
264
     * @param int                     $accountNumber      Account number.
265
     * @param int                     $accountCheckNumber Digit account checker.
266
     * @param \Moip\Resource\Customer $holder
267
     *
268
     * @return Refund
269
     */
270
    public function bankAccountPartial($amount, $type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, Customer $holder)
271
    {
272
        $data = $this->bankAccountDataCustomer($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, $holder);
273
        $data->amount = $amount;
274
275
        return $this->execute($data);
276
    }
277
278
    /**
279
     * Making a full refund in credit card.
280
     *
281
     * @return \Moip\Resource\Refund
282
     */
283
    public function creditCardFull()
284
    {
285
        return $this->execute();
286
    }
287
288
    /**
289
     * Making a partial refund in credit card.
290
     *
291
     * @param int|float $amount value of refund.
292
     *
293
     * @return \Moip\Resource\Refund
294
     */
295
    public function creditCardPartial($amount)
296
    {
297
        $data = new stdClass();
298
        $data->amount = $amount;
299
300
        return $this->execute($data);
301
    }
302
303
    /**
304
     * Get iterator.
305
     *
306
     * @return \ArrayIterator
307
     */
308
    public function getIterator()
309
    {
310
        $refunds = [];
311
        $response = $this->httpRequest($this->getPath(), Requests::GET);
312
        foreach ($response->refunds as $refund) {
313
            $refunds[] = $this->populate($refund);
314
        }
315
316
        return new ArrayIterator($refunds);
317
    }
318
319
    /**
320
     * Set order.
321
     *
322
     * @param \Moip\Resource\Orders $order
323
     */
324
    public function setOrder(Orders $order)
325
    {
326
        $this->order = $order;
327
    }
328
329
    /**
330
     * Set payment.
331
     *
332
     * @param \Moip\Resource\Payment $payment
333
     */
334
    public function setPayment(Payment $payment)
335
    {
336
        $this->payment = $payment;
337
    }
338
339 View Code Duplication
    public function bankAccount($resourceId, BankAccount $bankAccount, $amount = null)
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...
340
    {
341
        $data = $this->bankAccountData($bankAccount);
342
343
        if (!is_null($amount)) {
344
            $data->amount = $amount;
345
        }
346
347
        return $this->execute($data, $resourceId);
348
    }
349
350 View Code Duplication
    public function creditCard($resourceId, $amount = null)
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...
351
    {
352
        if (!is_null($amount)) {
353
            $data = new stdClass();
354
            $data->amount = $amount;
355
356
            return $this->execute($data, $resourceId);
357
        }
358
359
        return $this->execute(null, $resourceId);
360
    }
361
362
    private function isOrder($resourceId)
363
    {
364
        return 0 === strpos($resourceId, 'ORD');
365
    }
366
}
367