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 a Refund. |
120
|
|
|
* |
121
|
|
|
* @param string $moip_id Refund id. |
122
|
|
|
* |
123
|
|
|
* @return stdClass |
124
|
|
|
*/ |
125
|
|
|
public function get($moip_id) |
126
|
|
|
{ |
127
|
|
|
return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $moip_id)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get status from MoIP refund. |
132
|
|
|
* |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getStatus() |
137
|
|
|
{ |
138
|
|
|
return $this->getIfSet('status'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get MoIP refund type. |
143
|
|
|
* |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getType() |
148
|
|
|
{ |
149
|
|
|
return $this->getIfSet('type'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Create a new refund in api MoIP. |
154
|
|
|
* |
155
|
|
|
* @param stdClass $data |
156
|
|
|
* |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
|
|
private function execute(stdClass $data = null, $resourceId = null) |
160
|
|
|
{ |
161
|
|
|
$body = empty($data) ? new stdClass() : $data; |
162
|
|
|
$response = $this->httpRequest($this->getPath($resourceId), Requests::POST, $body); |
163
|
|
|
|
164
|
|
|
return $this->populate($response); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Checks path that will be the request. |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
private function getPath($resourceId = null) |
173
|
|
|
{ |
174
|
|
|
if (!is_null($resourceId)) { |
175
|
|
|
$endpoint = ($this->isOrder($resourceId) ? Orders::PATH : Payment::PATH); |
176
|
|
|
|
177
|
|
|
return sprintf('/%s/%s/%s/%s', MoipResource::VERSION, $endpoint, $resourceId, self::PATH); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($this->order !== null) { |
181
|
|
|
return sprintf('/%s/%s/%s/%s', MoipResource::VERSION, Orders::PATH, $this->order->getId(), self::PATH); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return sprintf('/%s/%s/%s/%s', MoipResource::VERSION, Payment::PATH, $this->payment->getId(), self::PATH); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Bank account is the bank address of a particular vendor or a customer. |
189
|
|
|
* |
190
|
|
|
* @param string $type Kind of bank account. possible values: CHECKING, SAVING. |
191
|
|
|
* @param string $bankNumber Bank number. possible values: 001, 237, 341, 041. |
192
|
|
|
* @param int $agencyNumber Branch number. |
193
|
|
|
* @param int $agencyCheckNumber Checksum of the agency. |
194
|
|
|
* @param int $accountNumber Account number. |
195
|
|
|
* @param int $accountCheckNumber Digit account checker. |
196
|
|
|
* @param \Moip\Resource\Customer $holder |
197
|
|
|
* |
198
|
|
|
* @return \stdClass |
199
|
|
|
*/ |
200
|
|
|
private function bankAccountDataCustomer($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, Customer $holder) |
201
|
|
|
{ |
202
|
|
|
$data = new stdClass(); |
203
|
|
|
$data->refundingInstrument = new stdClass(); |
204
|
|
|
$data->refundingInstrument->method = self::METHOD_BANK_ACCOUNT; |
205
|
|
|
$data->refundingInstrument->bankAccount = new stdClass(); |
206
|
|
|
$data->refundingInstrument->bankAccount->type = $type; |
207
|
|
|
$data->refundingInstrument->bankAccount->bankNumber = $bankNumber; |
208
|
|
|
$data->refundingInstrument->bankAccount->agencyNumber = $agencyNumber; |
209
|
|
|
$data->refundingInstrument->bankAccount->agencyCheckNumber = $agencyCheckNumber; |
210
|
|
|
$data->refundingInstrument->bankAccount->accountNumber = $accountNumber; |
211
|
|
|
$data->refundingInstrument->bankAccount->accountCheckNumber = $accountCheckNumber; |
212
|
|
|
$data->refundingInstrument->bankAccount->holder = new stdClass(); |
213
|
|
|
$data->refundingInstrument->bankAccount->holder->fullname = $holder->getFullname(); |
214
|
|
|
$data->refundingInstrument->bankAccount->holder->taxDocument = new stdClass(); |
215
|
|
|
$data->refundingInstrument->bankAccount->holder->taxDocument->type = $holder->getTaxDocumentType(); |
216
|
|
|
$data->refundingInstrument->bankAccount->holder->taxDocument->number = $holder->getTaxDocumentNumber(); |
217
|
|
|
|
218
|
|
|
return $data; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Bank account is the bank address of a particular vendor or a customer. |
223
|
|
|
* |
224
|
|
|
* @param \Moip\Resource\BankAccount $bankAccount |
225
|
|
|
* |
226
|
|
|
* @return \stdClass |
227
|
|
|
*/ |
228
|
|
|
private function bankAccountData(BankAccount $bankAccount) |
229
|
|
|
{ |
230
|
|
|
$data = new stdClass(); |
231
|
|
|
$data->refundingInstrument = new stdClass(); |
232
|
|
|
$data->refundingInstrument->method = self::METHOD_BANK_ACCOUNT; |
233
|
|
|
$data->refundingInstrument->bankAccount = new stdClass(); |
234
|
|
|
$data->refundingInstrument->bankAccount->type = $bankAccount->getType(); |
235
|
|
|
$data->refundingInstrument->bankAccount->bankNumber = $bankAccount->getBankNumber(); |
236
|
|
|
$data->refundingInstrument->bankAccount->agencyNumber = $bankAccount->getAgencyNumber(); |
237
|
|
|
$data->refundingInstrument->bankAccount->agencyCheckNumber = $bankAccount->getAgencyCheckNumber(); |
238
|
|
|
$data->refundingInstrument->bankAccount->accountNumber = $bankAccount->getAccountNumber(); |
239
|
|
|
$data->refundingInstrument->bankAccount->accountCheckNumber = $bankAccount->getAccountCheckNumber(); |
240
|
|
|
$data->refundingInstrument->bankAccount->holder = new stdClass(); |
241
|
|
|
$data->refundingInstrument->bankAccount->holder->fullname = $bankAccount->getFullname(); |
242
|
|
|
$data->refundingInstrument->bankAccount->holder->taxDocument = new stdClass(); |
243
|
|
|
$data->refundingInstrument->bankAccount->holder->taxDocument->type = $bankAccount->getTaxDocumentType(); |
244
|
|
|
$data->refundingInstrument->bankAccount->holder->taxDocument->number = $bankAccount->getTaxDocumentNumber(); |
245
|
|
|
|
246
|
|
|
return $data; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Making a full refund to the bank account. |
251
|
|
|
* |
252
|
|
|
* @param string $type Kind of bank account. possible values: CHECKING, SAVING. |
253
|
|
|
* @param string $bankNumber Bank number. possible values: 001, 237, 341, 041. |
254
|
|
|
* @param int $agencyNumber Branch number. |
255
|
|
|
* @param int $agencyCheckNumber Checksum of the agency. |
256
|
|
|
* @param int $accountNumber Account number. |
257
|
|
|
* @param int $accountCheckNumber Digit account checker. |
258
|
|
|
* @param \Moip\Resource\Customer $holder |
259
|
|
|
* |
260
|
|
|
* @return Refund |
261
|
|
|
*/ |
262
|
|
|
public function bankAccountFull($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, Customer $holder) |
263
|
|
|
{ |
264
|
|
|
$data = $this->bankAccountDataCustomer($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, $holder); |
265
|
|
|
|
266
|
|
|
return $this->execute($data); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Making a partial refund in the bank account. |
271
|
|
|
* |
272
|
|
|
* @param string $type Kind of bank account. possible values: CHECKING, SAVING. |
273
|
|
|
* @param string $bankNumber Bank number. possible values: 001, 237, 341, 041. |
274
|
|
|
* @param int $agencyNumber Branch number. |
275
|
|
|
* @param int $agencyCheckNumber Checksum of the agency. |
276
|
|
|
* @param int $accountNumber Account number. |
277
|
|
|
* @param int $accountCheckNumber Digit account checker. |
278
|
|
|
* @param \Moip\Resource\Customer $holder |
279
|
|
|
* |
280
|
|
|
* @return Refund |
281
|
|
|
*/ |
282
|
|
|
public function bankAccountPartial($amount, $type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, Customer $holder) |
283
|
|
|
{ |
284
|
|
|
$data = $this->bankAccountDataCustomer($type, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber, $holder); |
285
|
|
|
$data->amount = $amount; |
286
|
|
|
|
287
|
|
|
return $this->execute($data); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Making a full refund in credit card. |
292
|
|
|
* |
293
|
|
|
* @return \Moip\Resource\Refund |
294
|
|
|
*/ |
295
|
|
|
public function creditCardFull() |
296
|
|
|
{ |
297
|
|
|
return $this->execute(); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Making a partial refund in credit card. |
302
|
|
|
* |
303
|
|
|
* @param int|float $amount value of refund. |
304
|
|
|
* |
305
|
|
|
* @return \Moip\Resource\Refund |
306
|
|
|
*/ |
307
|
|
|
public function creditCardPartial($amount) |
308
|
|
|
{ |
309
|
|
|
$data = new stdClass(); |
310
|
|
|
$data->amount = $amount; |
311
|
|
|
|
312
|
|
|
return $this->execute($data); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Get iterator. |
317
|
|
|
* |
318
|
|
|
* @return \ArrayIterator |
319
|
|
|
*/ |
320
|
|
|
public function getIterator() |
321
|
|
|
{ |
322
|
|
|
$refunds = []; |
323
|
|
|
$response = $this->httpRequest($this->getPath(), Requests::GET); |
324
|
|
|
foreach ($response->refunds as $refund) { |
325
|
|
|
$refunds[] = $this->populate($refund); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return new ArrayIterator($refunds); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Set order. |
333
|
|
|
* |
334
|
|
|
* @param \Moip\Resource\Orders $order |
335
|
|
|
*/ |
336
|
|
|
public function setOrder(Orders $order) |
337
|
|
|
{ |
338
|
|
|
$this->order = $order; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Set payment. |
343
|
|
|
* |
344
|
|
|
* @param \Moip\Resource\Payment $payment |
345
|
|
|
*/ |
346
|
|
|
public function setPayment(Payment $payment) |
347
|
|
|
{ |
348
|
|
|
$this->payment = $payment; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
View Code Duplication |
public function bankAccount($resourceId, BankAccount $bankAccount, $amount = null) |
|
|
|
|
352
|
|
|
{ |
353
|
|
|
$data = $this->bankAccountData($bankAccount); |
354
|
|
|
|
355
|
|
|
if (!is_null($amount)) { |
356
|
|
|
$data->amount = $amount; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
return $this->execute($data, $resourceId); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
View Code Duplication |
public function creditCard($resourceId, $amount = null) |
|
|
|
|
363
|
|
|
{ |
364
|
|
|
if (!is_null($amount)) { |
365
|
|
|
$data = new stdClass(); |
366
|
|
|
$data->amount = $amount; |
367
|
|
|
|
368
|
|
|
return $this->execute($data, $resourceId); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
return $this->execute(null, $resourceId); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
private function isOrder($resourceId) |
375
|
|
|
{ |
376
|
|
|
return 0 === strpos($resourceId, 'ORD'); |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
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.