1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FeiMx\Pac\Drivers; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use FeiMx\Pac\Contracts\PacDriverInterface; |
7
|
|
|
use FeiMx\Pac\Exceptions\CfdiInProcessException; |
8
|
|
|
use FeiMx\Pac\Exceptions\CfdiNotCancelableException; |
9
|
|
|
use FeiMx\Pac\Exceptions\PacErrorException; |
10
|
|
|
use FeiMx\Pac\Exceptions\PacVerificationFailedException; |
11
|
|
|
use FeiMx\Pac\PacStamp; |
12
|
|
|
use FeiMx\Pac\PacUser; |
13
|
|
|
use Illuminate\Support\Carbon; |
14
|
|
|
use Illuminate\Support\Facades\Validator; |
15
|
|
|
|
16
|
|
|
class FinkokDriver extends AbstractDriver implements PacDriverInterface |
17
|
|
|
{ |
18
|
|
|
public function stamp($xml): PacStamp |
19
|
|
|
{ |
20
|
|
|
$response = $this->request( |
21
|
|
|
$this->url('stamp'), |
22
|
|
|
'Stamp', |
23
|
|
|
$this->prepareStampParams(['xml' => $xml]) |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
if (is_a($response, 'SoapFault')) { |
27
|
|
|
throw new PacErrorException($response->faultstring); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (!isset($response->stampResult->UUID)) { |
31
|
|
|
throw new PacErrorException($response->stampResult->Incidencias->Incidencia->MensajeIncidencia); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return (new PacStamp())->map($this->stampResultToAttributes($response->stampResult)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function cancel(array $uuids, $rfc, $cer, $key) |
38
|
|
|
{ |
39
|
|
|
$response = $this->request( |
40
|
|
|
$this->url('cancel'), |
41
|
|
|
'cancel', |
42
|
|
|
$this->prepareStampParams([ |
43
|
|
|
'UUIDS' => ['uuids' => $uuids], |
44
|
|
|
'taxpayer_id' => $rfc, |
45
|
|
|
'cer' => $cer, |
46
|
|
|
'key' => $key, |
47
|
|
|
]) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
if (is_a($response, 'SoapFault')) { |
51
|
|
|
throw new PacErrorException($response->faultstring); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (isset($response->cancelResult->CodEstatus)) { |
55
|
|
|
throw new PacErrorException($response->cancelResult->CodEstatus); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$statusUuid = $response->cancelResult->Folios->Folio->EstatusUUID ?? null; |
59
|
|
|
|
60
|
|
|
$this->ensureHasNoProblemsWithSat($statusUuid); |
61
|
|
|
|
62
|
|
|
$this->ensureIsCancelable($statusUuid); |
63
|
|
|
|
64
|
|
|
$this->ensureIsNotInProcess( |
65
|
|
|
$response->cancelResult->Folios->Folio->EstatusCancelacion ?? null |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
return $this->cancelResultToAttributes($response->cancelResult); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function ensureHasNoProblemsWithSat($statusUuid) |
72
|
|
|
{ |
73
|
|
|
if ($statusUuid && in_array($statusUuid, [708, 205])) { |
74
|
|
|
throw new PacErrorException('Ocurrio un error con el SAT, al intentar cancelar el comprobante.'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function ensureIsCancelable($statusUuid) |
79
|
|
|
{ |
80
|
|
|
if ($statusUuid && 'no_cancelable' == $statusUuid) { |
81
|
|
|
throw new CfdiNotCancelableException(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function ensureIsNotInProcess($canceledStatus) |
86
|
|
|
{ |
87
|
|
|
if ($canceledStatus && 'En proceso' == $canceledStatus) { |
88
|
|
|
throw new CfdiInProcessException(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function addUser($rfc, $params = []): PacUser |
93
|
|
|
{ |
94
|
|
|
$this->throwErrorIfInvalidParams($data = array_merge(['rfc' => $rfc], $params)); |
95
|
|
|
|
96
|
|
|
$response = $this->request( |
97
|
|
|
$this->url('registration'), |
98
|
|
|
'add', |
99
|
|
|
$this->prepareGenericParams(array_merge(['taxpayer_id' => $rfc], $params)) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
if (is_a($response, 'SoapFault')) { |
103
|
|
|
throw new PacErrorException($response->faultstring); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!$response->addResult->success) { |
107
|
|
|
throw new PacErrorException($response->addResult->message); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ('Account Already exists' == $response->addResult->message) { |
111
|
|
|
throw new PacErrorException('The RFC has been registered before'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return (new PacUser())->map($data); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
View Code Duplication |
public function editUser($rfc, $params = []) |
118
|
|
|
{ |
119
|
|
|
$response = $this->request( |
120
|
|
|
$this->url('registration'), |
121
|
|
|
'edit', |
122
|
|
|
$this->prepareGenericParams(array_merge(['taxpayer_id' => $rfc], $params)) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
if (is_a($response, 'SoapFault')) { |
126
|
|
|
throw new PacErrorException($response->faultstring); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!$response->editResult->success) { |
130
|
|
|
throw new PacErrorException($response->editResult->message); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $response->editResult->message; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getUsers(): ArrayAccess |
137
|
|
|
{ |
138
|
|
|
$response = $this->request($this->url('registration'), 'get', $this->prepareGenericParams(['taxpayer_id' => ''])); |
139
|
|
|
|
140
|
|
|
if (is_a($response, 'SoapFault')) { |
141
|
|
|
throw new PacErrorException($response->faultstring); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$users = collect([]); |
145
|
|
|
foreach ($response->getResult->users->ResellerUser as $resellerUser) { |
146
|
|
|
$users->push( |
147
|
|
|
(new PacUser())->map( |
148
|
|
|
$this->mapToAttributes($resellerUser) |
149
|
|
|
) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $users; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getUser($rfc = null): PacUser |
157
|
|
|
{ |
158
|
|
|
$response = $this->request($this->url('registration'), 'get', $this->prepareGenericParams(['taxpayer_id' => $rfc])); |
159
|
|
|
|
160
|
|
|
if (is_a($response, 'SoapFault')) { |
161
|
|
|
throw new PacErrorException($response->faultstring); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return (new PacUser())->map( |
165
|
|
|
$this->mapToAttributes($response->getResult->users->ResellerUser) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
View Code Duplication |
public function assignStamps($rfc = null, $credit = 0) |
170
|
|
|
{ |
171
|
|
|
$response = $this->request( |
172
|
|
|
$this->url('registration'), |
173
|
|
|
'assign', |
174
|
|
|
$this->prepareStampParams(['taxpayer_id' => $rfc, 'credit' => $credit]) |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
if (is_a($response, 'SoapFault')) { |
178
|
|
|
throw new PacErrorException($response->faultstring); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if (!$response->assignResult->success) { |
182
|
|
|
throw new PacErrorException($response->assignResult->message); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $response->assignResult->message; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
protected function url($wsdl = null) |
189
|
|
|
{ |
190
|
|
|
return $this->sandbox |
191
|
|
|
? "https://demo-facturacion.finkok.com/servicios/soap/{$wsdl}.wsdl" |
192
|
|
|
: "https://facturacion.finkok.com/servicios/soap/{$wsdl}.wsdl"; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
protected function throwErrorIfInvalidParams($params = []) |
196
|
|
|
{ |
197
|
|
|
$rules = [ |
198
|
|
|
'rfc' => 'required', |
199
|
|
|
'type_user' => 'required|in:O,P', |
200
|
|
|
'added' => 'required', |
201
|
|
|
]; |
202
|
|
|
|
203
|
|
|
if (Validator::make($params, $rules)->fails()) { |
204
|
|
|
throw new PacVerificationFailedException('The params did not contain the necessary fields'); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
protected function prepareGenericParams(array $params = []): array |
209
|
|
|
{ |
210
|
|
|
return array_merge([ |
211
|
|
|
'reseller_username' => $this->username, |
212
|
|
|
'reseller_password' => $this->password, |
213
|
|
|
], $params); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
protected function prepareStampParams(array $params = []): array |
217
|
|
|
{ |
218
|
|
|
return array_merge([ |
219
|
|
|
'username' => $this->username, |
220
|
|
|
'password' => $this->password, |
221
|
|
|
], $params); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
protected function stampResultToAttributes($stampResult): array |
225
|
|
|
{ |
226
|
|
|
return [ |
227
|
|
|
'xml' => $stampResult->xml, |
228
|
|
|
'uuid' => $stampResult->UUID, |
229
|
|
|
'date' => Carbon::parse($stampResult->Fecha), |
230
|
|
|
'statusCode' => $stampResult->CodEstatus, |
231
|
|
|
'satSeal' => $stampResult->SatSeal, |
232
|
|
|
'satCertificateNumber' => $stampResult->NoCertificadoSAT, |
233
|
|
|
]; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
protected function cancelResultToAttributes($cancelResult): array |
237
|
|
|
{ |
238
|
|
|
return [ |
239
|
|
|
'folios' => $cancelResult->Folios, |
240
|
|
|
'acuse' => $cancelResult->Acuse, |
241
|
|
|
'date' => Carbon::parse($cancelResult->Fecha), |
242
|
|
|
'rfc' => $cancelResult->RfcEmisor, |
243
|
|
|
]; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
protected function mapToAttributes($resellerUser): array |
247
|
|
|
{ |
248
|
|
|
return [ |
249
|
|
|
'status' => $resellerUser->status, |
250
|
|
|
'counter' => $resellerUser->counter, |
251
|
|
|
'credit' => $resellerUser->credit, |
252
|
|
|
'rfc' => $resellerUser->taxpayer_id, |
253
|
|
|
]; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|