Completed
Push — master ( 894b3e...f9d107 )
by Jorge
01:20
created

FinkokDriver::prepareStampParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace FeiMx\Pac\Drivers;
4
5
use ArrayAccess;
6
use FeiMx\Pac\Contracts\PacDriverInterface;
7
use FeiMx\Pac\Exceptions\PacErrorException;
8
use FeiMx\Pac\Exceptions\PacVerificationFailedException;
9
use FeiMx\Pac\PacStamp;
10
use FeiMx\Pac\PacUser;
11
use Illuminate\Support\Carbon;
12
use Illuminate\Support\Facades\Validator;
13
14
class FinkokDriver extends AbstractDriver implements PacDriverInterface
15
{
16
    public function stamp($xml): PacStamp
17
    {
18
        $response = $this->request(
19
            $this->url('stamp'),
20
            'Stamp',
21
            $this->prepareStampParams(['xml' => $xml])
22
        );
23
24
        if (is_a($response, 'SoapFault')) {
25
            throw new PacErrorException($response->faultstring);
26
        }
27
28
        if (!isset($response->stampResult->UUID)) {
29
            throw new PacErrorException(
30
                $response->stampResult->Incidencias->Incidencia->MensajeIncidencia,
31
                $response->stampResult->Incidencias->Incidencia->CodigoError
32
            );
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
        return $this->cancelResultToAttributes($response->cancelResult);
60
    }
61
62
    public function addUser($rfc, $params = []): PacUser
63
    {
64
        $this->throwErrorIfInvalidParams($data = array_merge(['rfc' => $rfc], $params));
65
66
        $response = $this->request(
67
            $this->url('registration'),
68
            'add',
69
            $this->prepareGenericParams(array_merge(['taxpayer_id' => $rfc], $params))
70
        );
71
72
        if (is_a($response, 'SoapFault')) {
73
            throw new PacErrorException($response->faultstring);
74
        }
75
76
        if (!$response->addResult->success) {
77
            throw new PacErrorException($response->addResult->message);
78
        }
79
80
        if ('Account Already exists' == $response->addResult->message) {
81
            throw new PacErrorException('The RFC has been registered before');
82
        }
83
84
        return (new PacUser())->map($data);
85
    }
86
87 View Code Duplication
    public function editUser($rfc, $params = [])
88
    {
89
        $response = $this->request(
90
            $this->url('registration'),
91
            'edit',
92
            $this->prepareGenericParams(array_merge(['taxpayer_id' => $rfc], $params))
93
        );
94
95
        if (is_a($response, 'SoapFault')) {
96
            throw new PacErrorException($response->faultstring);
97
        }
98
99
        if (!$response->editResult->success) {
100
            throw new PacErrorException($response->editResult->message);
101
        }
102
103
        return $response->editResult->message;
104
    }
105
106
    public function getUsers(): ArrayAccess
107
    {
108
        $response = $this->request($this->url('registration'), 'get', $this->prepareGenericParams(['taxpayer_id' => '']));
109
110
        if (is_a($response, 'SoapFault')) {
111
            throw new PacErrorException($response->faultstring);
112
        }
113
114
        $users = collect([]);
115
        foreach ($response->getResult->users->ResellerUser as $resellerUser) {
116
            $users->push(
117
                (new PacUser())->map(
118
                    $this->mapToAttributes($resellerUser)
119
                )
120
            );
121
        }
122
123
        return $users;
124
    }
125
126
    public function getUser($rfc = null): PacUser
127
    {
128
        $response = $this->request($this->url('registration'), 'get', $this->prepareGenericParams(['taxpayer_id' => $rfc]));
129
130
        if (is_a($response, 'SoapFault')) {
131
            throw new PacErrorException($response->faultstring);
132
        }
133
134
        return (new PacUser())->map(
135
            $this->mapToAttributes($response->getResult->users->ResellerUser)
136
        );
137
    }
138
139 View Code Duplication
    public function assignStamps($rfc = null, $credit = 0)
140
    {
141
        $response = $this->request(
142
            $this->url('registration'),
143
            'assign',
144
            $this->prepareStampParams(['taxpayer_id' => $rfc, 'credit' => $credit])
145
        );
146
147
        if (is_a($response, 'SoapFault')) {
148
            throw new PacErrorException($response->faultstring);
149
        }
150
151
        if (!$response->assignResult->success) {
152
            throw new PacErrorException($response->assignResult->message);
153
        }
154
155
        return $response->assignResult->message;
156
    }
157
158
    protected function url($wsdl = null)
159
    {
160
        return $this->sandbox
161
            ? "https://demo-facturacion.finkok.com/servicios/soap/{$wsdl}.wsdl"
162
            : "https://facturacion.finkok.com/servicios/soap/{$wsdl}.wsdl";
163
    }
164
165
    protected function throwErrorIfInvalidParams($params = [])
166
    {
167
        $rules = [
168
            'rfc' => 'required',
169
            'type_user' => 'required|in:O,P',
170
            'added' => 'required',
171
        ];
172
173
        if (Validator::make($params, $rules)->fails()) {
174
            throw new PacVerificationFailedException('The params did not contain the necessary fields');
175
        }
176
    }
177
178
    protected function prepareGenericParams(array $params = []): array
179
    {
180
        return array_merge([
181
            'reseller_username' => $this->username,
182
            'reseller_password' => $this->password,
183
        ], $params);
184
    }
185
186
    protected function prepareStampParams(array $params = []): array
187
    {
188
        return array_merge([
189
            'username' => $this->username,
190
            'password' => $this->password,
191
        ], $params);
192
    }
193
194
    protected function stampResultToAttributes($stampResult): array
195
    {
196
        return [
197
            'xml' => $stampResult->xml,
198
            'uuid' => $stampResult->UUID,
199
            'date' => Carbon::parse($stampResult->Fecha),
200
            'statusCode' => $stampResult->CodEstatus,
201
            'satSeal' => $stampResult->SatSeal,
202
            'satCertificateNumber' => $stampResult->NoCertificadoSAT,
203
        ];
204
    }
205
206
    protected function cancelResultToAttributes($cancelResult): array
207
    {
208
        return [
209
            'folios' => $cancelResult->Folios,
210
            'acuse' => $cancelResult->Acuse,
211
            'date' => Carbon::parse($cancelResult->Fecha),
212
            'rfc' => $cancelResult->RfcEmisor,
213
        ];
214
    }
215
216
    protected function mapToAttributes($resellerUser): array
217
    {
218
        return [
219
            'status' => $resellerUser->status,
220
            'counter' => $resellerUser->counter,
221
            'credit' => $resellerUser->credit,
222
            'rfc' => $resellerUser->taxpayer_id,
223
        ];
224
    }
225
}
226