1
|
|
|
<?php namespace SOSTheBlack\Moip; |
2
|
|
|
|
3
|
|
|
use App; |
4
|
|
|
use Moip; |
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* MoipAbstract |
9
|
|
|
* |
10
|
|
|
* @package SOSTheBlack\Moip |
11
|
|
|
* @author Jean Cesar Garcia <[email protected]> <SOSTheBlack> |
12
|
|
|
* @version 1.* |
13
|
|
|
**/ |
14
|
|
|
abstract class MoipAbstract |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Api of MoIP |
18
|
|
|
* |
19
|
|
|
* @var \SOSTheBlack\Moip\Api |
20
|
|
|
**/ |
21
|
|
|
protected $api; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* data of configuration of the MoIP |
25
|
|
|
* |
26
|
|
|
* @var array table moip |
27
|
|
|
**/ |
28
|
|
|
protected $moip; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* undocumented class variable |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
**/ |
35
|
|
|
protected $data; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* initialize() |
39
|
|
|
* |
40
|
|
|
* @return \SOSTheBlack\Moip\Api |
41
|
|
|
*/ |
42
|
|
|
protected function initialize() |
43
|
|
|
{ |
44
|
|
|
$this->moip = Moip::first(); |
45
|
|
|
$this->api = App::make('\SOSTheBlack\Moip\Api'); |
46
|
|
|
$this->api->setEnvironment(! $this->moip->environment); |
47
|
|
|
$this->api->setCredential([ |
48
|
|
|
'key' => $this->moip->key, |
49
|
|
|
'token' => $this->moip->token |
50
|
|
|
]); |
51
|
|
|
return $this->api; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* setData() |
56
|
|
|
* |
57
|
|
|
* @param string[] $data |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
protected function setData($data) |
61
|
|
|
{ |
62
|
|
|
$this->data = $data; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* getValidate() |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
protected function getValidate() |
71
|
|
|
{ |
72
|
|
|
return $this->moip->validate === 0 ? 'Basic' : 'Identification'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* getUniqueId() |
77
|
|
|
* |
78
|
|
|
* adds unique id in the order |
79
|
|
|
* |
80
|
|
|
* @return string|false |
81
|
|
|
*/ |
82
|
|
|
protected function getUniqueId() |
83
|
|
|
{ |
84
|
|
|
return isset($this->data['unique_id']) ? $this->data['unique_id'] : false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* getReason() |
89
|
|
|
* |
90
|
|
|
* adds payment reason in the order |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
protected function getReason() |
95
|
|
|
{ |
96
|
|
|
return isset($this->data['reason']) ? $this->data['reason'] : $this->moip->reason; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* getParams |
101
|
|
|
* |
102
|
|
|
* Validation of the params sent |
103
|
|
|
* |
104
|
|
|
* @param string $oneParam |
105
|
|
|
* @param string $twoParam |
106
|
|
|
* @param boolean $db |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
protected function getParams($oneParam, $twoParam = null, $db = false) |
110
|
|
|
{ |
111
|
|
|
if (! $twoParam) { |
|
|
|
|
112
|
|
|
if ($db === true) { |
113
|
|
|
return isset($this->data[$oneParam]) ? $this->data[$oneParam] : $this->moip->$oneParam; |
114
|
|
|
} else { |
115
|
|
|
return isset($this->data[$oneParam]) ? $this->data[$oneParam] : ''; |
116
|
|
|
} |
117
|
|
|
} else { |
118
|
|
|
return isset($this->data[$oneParam][$twoParam]) ? $this->data[$oneParam][$twoParam] : ''; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* billetConf |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
protected function billetConf() |
127
|
|
|
{ |
128
|
|
|
$this->api->setBilletConf( |
129
|
|
|
$this->getParams('billet', 'expiration', true), |
130
|
|
|
(boolean) $this->getParams('billet', 'workingDays', true), |
131
|
|
|
$this->getBilletInstructions(), |
132
|
|
|
$this->getParams('billet', 'uriLogo', true) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* getBilletInstructions |
138
|
|
|
* |
139
|
|
|
* @return string[] |
140
|
|
|
*/ |
141
|
|
|
private function getBilletInstructions() |
142
|
|
|
{ |
143
|
|
|
$billet = [ |
144
|
|
|
'instructions' => [ |
145
|
|
|
'firstLine', |
146
|
|
|
'secondLine', |
147
|
|
|
'lastLine' |
148
|
|
|
] |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
foreach ($billet['instructions'] as $keyInstructions => $valueInstructions) { |
152
|
|
|
$billet['instructions'][$keyInstructions] = |
153
|
|
|
isset($this->data['billet']['instructions'][$keyInstructions]) ? |
154
|
|
|
$this->data['billet']['instructions'][$keyInstructions] : |
155
|
|
|
$this->moip->$valueInstructions; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $billet['instructions']; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* getPaymentWay |
163
|
|
|
* |
164
|
|
|
* Adds payment way in the order |
165
|
|
|
* |
166
|
|
|
* @return false|null |
167
|
|
|
*/ |
168
|
|
|
protected function getPaymentWay() |
169
|
|
|
{ |
170
|
|
|
if (! isset($this->data['paymentWay'])) { |
171
|
|
|
return false; |
172
|
|
|
} else { |
173
|
|
|
$payment = $this->data['paymentWay']; |
174
|
|
|
$arrayWay = [ |
175
|
|
|
'creditCard', |
176
|
|
|
'billet' , |
177
|
|
|
'financing' , |
178
|
|
|
'debit' , |
179
|
|
|
'debitCard' |
180
|
|
|
]; |
181
|
|
|
|
182
|
|
|
foreach ($arrayWay as $arrayWayKey => $arrayWayValue) { |
183
|
|
|
if (isset($payment[$arrayWayKey]) && $payment[$arrayWayKey] == $arrayWayValue) { |
184
|
|
|
$this->api->addPaymentWay($arrayWayValue); |
185
|
|
|
} else { |
186
|
|
|
if ($this->moip->$arrayWayValue === 1) { |
187
|
|
|
$this->api->addPaymentWay($arrayWayValue); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* getMessage |
196
|
|
|
* |
197
|
|
|
* @return void |
198
|
|
|
*/ |
199
|
|
|
protected function getMessage() |
200
|
|
|
{ |
201
|
|
|
if (isset($this->data['message'])) { |
202
|
|
|
foreach ($this->data['message'] as $keyMessage => $valueMessage) { |
|
|
|
|
203
|
|
|
$this->api->addMessage($valueMessage); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* getComission |
210
|
|
|
* |
211
|
|
|
* @return void |
212
|
|
|
*/ |
213
|
|
|
protected function getComission() |
214
|
|
|
{ |
215
|
|
|
if (isset($this->data['comission'])) { |
216
|
|
|
//dd($this->data['comission']); |
217
|
|
|
foreach ($this->data['comission'] as $keyComission => $valueComission) { |
|
|
|
|
218
|
|
|
$this->api->addComission( |
219
|
|
|
isset($valueComission['reason']) ? $valueComission['reason'] : null, |
220
|
|
|
isset($valueComission['receiver']) ? $valueComission['receiver'] : null, |
221
|
|
|
isset($valueComission['value']) ? $valueComission['value'] : null, |
222
|
|
|
isset($valueComission['percentageValue']) ? $valueComission['percentageValue'] : null, |
223
|
|
|
isset($valueComission['ratePayer']) ? $valueComission['ratePayer'] : null |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* getParcel |
231
|
|
|
* |
232
|
|
|
* @return void |
233
|
|
|
*/ |
234
|
|
|
protected function getParcel() |
235
|
|
|
{ |
236
|
|
|
if (isset($this->data['parcel'])) { |
237
|
|
|
foreach ($this->data['parcel'] as $keyParcel => $valueParcel) { |
|
|
|
|
238
|
|
|
$this->api->addParcel( |
239
|
|
|
isset($valueParcel['min']) ? $valueParcel['min'] : null, |
240
|
|
|
isset($valueParcel['max']) ? $valueParcel['max'] : null, |
241
|
|
|
isset($valueParcel['rate']) ? $valueParcel['rate'] : null, |
242
|
|
|
isset($valueParcel['transfer']) ? $valueParcel['transfer'] : false, |
243
|
|
|
isset($valueParcel['receipt']) ? $valueParcel['receipt'] : false |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* responseValidation |
251
|
|
|
* |
252
|
|
|
* @param array $send |
253
|
|
|
* @param array|string $answer |
254
|
|
|
* @return void |
255
|
|
|
*/ |
256
|
|
|
protected function responseValidation($send, $answer) |
257
|
|
|
{ |
258
|
|
|
if ($send->error != false) { |
259
|
|
|
throw new Exception($send->error); |
260
|
|
|
} elseif (is_string($answer)) { |
261
|
|
|
throw new Exception($answer); |
262
|
|
|
} elseif ($answer->error !== false) { |
263
|
|
|
throw new Exception($answer->error); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
} |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: