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