facturas 0.5.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/errors.d.ts +16 -1
- package/dist/errors.mjs +14 -0
- package/dist/errors.mjs.map +1 -1
- package/dist/fiscal-evidence-DbbFH74x.d.ts +69 -0
- package/dist/{index-CqXVyU0E.d.ts → index-3fdgJ3c2.d.ts} +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.mjs +919 -265
- package/dist/index.mjs.map +1 -1
- package/dist/padron.d.ts +2 -2
- package/dist/padron.mjs.map +1 -1
- package/dist/{types-CKKe1Oo4.d.ts → types-Cup-oevW.d.ts} +2 -0
- package/dist/types.d.ts +1 -1
- package/dist/wsfe.d.ts +32 -3
- package/dist/wsfe.mjs +477 -108
- package/dist/wsfe.mjs.map +1 -1
- package/dist/wsmtxca.d.ts +64 -8
- package/dist/wsmtxca.mjs +543 -156
- package/dist/wsmtxca.mjs.map +1 -1
- package/package.json +1 -1
package/dist/wsfe.mjs
CHANGED
|
@@ -15,20 +15,78 @@ var ArcaInputError = class extends ArcaError {
|
|
|
15
15
|
this.detail = options?.detail;
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
|
+
var ArcaTransportError = class extends ArcaError {
|
|
19
|
+
name = "ArcaTransportError";
|
|
20
|
+
statusCode;
|
|
21
|
+
contentType;
|
|
22
|
+
responseBody;
|
|
23
|
+
constructor(message, options) {
|
|
24
|
+
super(message, "ARCA_TRANSPORT_ERROR", options);
|
|
25
|
+
this.statusCode = options?.statusCode;
|
|
26
|
+
this.contentType = options?.contentType;
|
|
27
|
+
this.responseBody = options?.responseBody;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var ArcaSoapFaultError = class extends ArcaError {
|
|
31
|
+
name = "ArcaSoapFaultError";
|
|
32
|
+
faultCode;
|
|
33
|
+
detail;
|
|
34
|
+
constructor(message, options) {
|
|
35
|
+
super(message, "ARCA_SOAP_FAULT", options);
|
|
36
|
+
this.faultCode = options?.faultCode;
|
|
37
|
+
this.detail = options?.detail;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var ArcaInvalidSoapResponseError = class extends ArcaError {
|
|
41
|
+
name = "ArcaInvalidSoapResponseError";
|
|
42
|
+
service;
|
|
43
|
+
operation;
|
|
44
|
+
endpointUrl;
|
|
45
|
+
statusCode;
|
|
46
|
+
contentType;
|
|
47
|
+
responseBodyLength;
|
|
48
|
+
responseBodyPreview;
|
|
49
|
+
parsedDetail;
|
|
50
|
+
constructor(message, options) {
|
|
51
|
+
super(message, "ARCA_INVALID_SOAP_RESPONSE", options);
|
|
52
|
+
this.service = options?.service;
|
|
53
|
+
this.operation = options?.operation;
|
|
54
|
+
this.endpointUrl = options?.endpointUrl;
|
|
55
|
+
this.statusCode = options?.statusCode;
|
|
56
|
+
this.contentType = options?.contentType;
|
|
57
|
+
this.responseBodyLength = options?.responseBodyLength;
|
|
58
|
+
this.responseBodyPreview = options?.responseBodyPreview;
|
|
59
|
+
this.parsedDetail = options?.parsedDetail;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
18
62
|
var ArcaServiceError = class extends ArcaError {
|
|
19
63
|
name = "ArcaServiceError";
|
|
20
64
|
serviceCode;
|
|
65
|
+
service;
|
|
66
|
+
operation;
|
|
67
|
+
result;
|
|
68
|
+
resultLevel;
|
|
69
|
+
results;
|
|
70
|
+
cae;
|
|
71
|
+
issues;
|
|
21
72
|
detail;
|
|
22
73
|
constructor(message, options) {
|
|
23
74
|
super(message, "ARCA_SERVICE_ERROR", options);
|
|
24
75
|
this.serviceCode = options?.serviceCode;
|
|
76
|
+
this.service = options?.service;
|
|
77
|
+
this.operation = options?.operation;
|
|
78
|
+
this.result = options?.result;
|
|
79
|
+
this.resultLevel = options?.resultLevel;
|
|
80
|
+
this.results = options?.results;
|
|
81
|
+
this.cae = options?.cae;
|
|
82
|
+
this.issues = options?.issues;
|
|
25
83
|
this.detail = options?.detail;
|
|
26
84
|
}
|
|
27
85
|
};
|
|
28
86
|
|
|
29
87
|
// src/services/wsfe.ts
|
|
30
88
|
function createWsfeService(options) {
|
|
31
|
-
async function
|
|
89
|
+
async function executeWsfeAuthenticatedRawOperation(operation, input, body = {}, retries) {
|
|
32
90
|
const auth = await options.auth.login("wsfe", {
|
|
33
91
|
representedTaxId: input.representedTaxId,
|
|
34
92
|
forceRefresh: input.forceRefresh
|
|
@@ -36,6 +94,7 @@ function createWsfeService(options) {
|
|
|
36
94
|
const response = await options.soap.execute({
|
|
37
95
|
service: "wsfe",
|
|
38
96
|
operation,
|
|
97
|
+
...retries === void 0 ? {} : { retries },
|
|
39
98
|
body: {
|
|
40
99
|
Auth: createWsfeAuth(
|
|
41
100
|
input.representedTaxId ?? options.config.taxId,
|
|
@@ -45,7 +104,16 @@ function createWsfeService(options) {
|
|
|
45
104
|
...body
|
|
46
105
|
}
|
|
47
106
|
});
|
|
48
|
-
return
|
|
107
|
+
return unwrapWsfeOperationEnvelope(operation, response.result);
|
|
108
|
+
}
|
|
109
|
+
async function executeWsfeAuthenticatedOperation(operation, input, body = {}) {
|
|
110
|
+
const result = await executeWsfeAuthenticatedRawOperation(
|
|
111
|
+
operation,
|
|
112
|
+
input,
|
|
113
|
+
body
|
|
114
|
+
);
|
|
115
|
+
throwForWsfeOperationErrors(operation, result);
|
|
116
|
+
return result;
|
|
49
117
|
}
|
|
50
118
|
async function executeWsfeOperation(operation, body = {}) {
|
|
51
119
|
const response = await options.soap.execute({
|
|
@@ -53,7 +121,9 @@ function createWsfeService(options) {
|
|
|
53
121
|
operation,
|
|
54
122
|
body
|
|
55
123
|
});
|
|
56
|
-
|
|
124
|
+
const result = unwrapWsfeOperationEnvelope(operation, response.result);
|
|
125
|
+
throwForWsfeOperationErrors(operation, result);
|
|
126
|
+
return result;
|
|
57
127
|
}
|
|
58
128
|
async function getNextVoucherNumber({
|
|
59
129
|
representedTaxId,
|
|
@@ -95,55 +165,148 @@ function createWsfeService(options) {
|
|
|
95
165
|
forceRefresh
|
|
96
166
|
});
|
|
97
167
|
}
|
|
168
|
+
function authorizeVoucherOutcome({
|
|
169
|
+
representedTaxId,
|
|
170
|
+
data,
|
|
171
|
+
voucherNumber,
|
|
172
|
+
forceRefresh
|
|
173
|
+
}) {
|
|
174
|
+
const normalizedInput = normalizeWsfeVoucherInput(data);
|
|
175
|
+
return executeWsfeAuthorization({
|
|
176
|
+
representedTaxId,
|
|
177
|
+
data: normalizedInput,
|
|
178
|
+
voucherNumber,
|
|
179
|
+
forceRefresh
|
|
180
|
+
}).then(({ outcome }) => outcome);
|
|
181
|
+
}
|
|
98
182
|
async function authorizeNormalizedVoucher({
|
|
99
183
|
representedTaxId,
|
|
100
184
|
data: normalizedInput,
|
|
101
185
|
voucherNumber,
|
|
102
186
|
forceRefresh
|
|
103
187
|
}) {
|
|
104
|
-
const
|
|
105
|
-
const auth = await options.auth.login("wsfe", {
|
|
188
|
+
const execution = await executeWsfeAuthorization({
|
|
106
189
|
representedTaxId,
|
|
190
|
+
data: normalizedInput,
|
|
191
|
+
voucherNumber,
|
|
107
192
|
forceRefresh
|
|
108
193
|
});
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
194
|
+
if (execution.error) {
|
|
195
|
+
throw execution.error;
|
|
196
|
+
}
|
|
197
|
+
if (execution.outcome.kind !== "authorized") {
|
|
198
|
+
throw createWsfeOutcomeError(execution.outcome);
|
|
199
|
+
}
|
|
200
|
+
const { cae, caeExpiry, raw } = execution.outcome;
|
|
201
|
+
if (!(caeExpiry && raw)) {
|
|
202
|
+
throw new ArcaServiceError("WSFE did not return CAE authorization data", {
|
|
203
|
+
service: "wsfe",
|
|
204
|
+
operation: "FECAESolicitar",
|
|
205
|
+
result: execution.outcome.result,
|
|
206
|
+
resultLevel: execution.outcome.resultLevel,
|
|
207
|
+
results: execution.outcome.results,
|
|
208
|
+
cae,
|
|
209
|
+
issues: [
|
|
210
|
+
...execution.outcome.errors,
|
|
211
|
+
...execution.outcome.observations
|
|
212
|
+
],
|
|
213
|
+
detail: raw
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
cae,
|
|
218
|
+
caeExpiry,
|
|
219
|
+
voucherNumber,
|
|
220
|
+
raw
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
async function executeWsfeAuthorization({
|
|
224
|
+
representedTaxId,
|
|
225
|
+
data: normalizedInput,
|
|
226
|
+
voucherNumber,
|
|
227
|
+
forceRefresh
|
|
228
|
+
}) {
|
|
229
|
+
const requestData = mapWsfeVoucherInput(normalizedInput, voucherNumber);
|
|
230
|
+
try {
|
|
231
|
+
const result = await executeWsfeAuthenticatedRawOperation(
|
|
232
|
+
"FECAESolicitar",
|
|
233
|
+
{ representedTaxId, forceRefresh },
|
|
234
|
+
{
|
|
235
|
+
FeCAEReq: {
|
|
236
|
+
FeCabReq: {
|
|
237
|
+
CantReg: 1,
|
|
238
|
+
PtoVta: normalizedInput.salesPoint,
|
|
239
|
+
CbteTipo: normalizedInput.voucherType
|
|
240
|
+
},
|
|
241
|
+
FeDetReq: {
|
|
242
|
+
FECAEDetRequest: requestData
|
|
243
|
+
}
|
|
126
244
|
}
|
|
245
|
+
},
|
|
246
|
+
0
|
|
247
|
+
);
|
|
248
|
+
return {
|
|
249
|
+
outcome: classifyWsfeAuthorization(result, voucherNumber)
|
|
250
|
+
};
|
|
251
|
+
} catch (error) {
|
|
252
|
+
return {
|
|
253
|
+
outcome: createWsfeIndeterminateOutcome(error),
|
|
254
|
+
error
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async function lookupVoucher({
|
|
259
|
+
representedTaxId,
|
|
260
|
+
number,
|
|
261
|
+
salesPoint,
|
|
262
|
+
voucherType,
|
|
263
|
+
forceRefresh
|
|
264
|
+
}) {
|
|
265
|
+
const operation = "FECompConsultar";
|
|
266
|
+
const result = await executeWsfeAuthenticatedRawOperation(
|
|
267
|
+
operation,
|
|
268
|
+
{ representedTaxId, forceRefresh },
|
|
269
|
+
{
|
|
270
|
+
FeCompConsReq: {
|
|
271
|
+
CbteNro: number,
|
|
272
|
+
PtoVta: salesPoint,
|
|
273
|
+
CbteTipo: voucherType
|
|
127
274
|
}
|
|
128
275
|
}
|
|
129
|
-
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
276
|
+
);
|
|
277
|
+
const errors = extractWsfeGlobalIssues(result, operation);
|
|
278
|
+
if (errors.length > 0 && errors.every((issue) => issue.code === "602")) {
|
|
279
|
+
return {
|
|
280
|
+
kind: "not_found",
|
|
281
|
+
service: "wsfe",
|
|
282
|
+
operation,
|
|
283
|
+
errors,
|
|
284
|
+
observations: [],
|
|
285
|
+
raw: result
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
if (errors.length > 0) {
|
|
289
|
+
throw createWsfeServiceError(operation, result, errors);
|
|
290
|
+
}
|
|
291
|
+
const raw = toWsfeRecord(result.ResultGet);
|
|
292
|
+
if (!raw) {
|
|
293
|
+
throw new ArcaServiceError("WSFE did not return the consulted voucher", {
|
|
294
|
+
service: "wsfe",
|
|
295
|
+
operation,
|
|
136
296
|
detail: result
|
|
137
297
|
});
|
|
138
298
|
}
|
|
139
299
|
return {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
300
|
+
kind: "found",
|
|
301
|
+
service: "wsfe",
|
|
302
|
+
operation,
|
|
303
|
+
voucher: mapWsfeVoucherInfo(raw),
|
|
304
|
+
observations: [],
|
|
143
305
|
raw: result
|
|
144
306
|
};
|
|
145
307
|
}
|
|
146
308
|
return {
|
|
309
|
+
authorizeVoucherOutcome,
|
|
147
310
|
authorizeVoucher,
|
|
148
311
|
async createNextVoucher({ representedTaxId, data, forceRefresh }) {
|
|
149
312
|
const normalizedInput = normalizeWsfeVoucherInput(data);
|
|
@@ -255,33 +418,11 @@ function createWsfeService(options) {
|
|
|
255
418
|
const raw = result.ResultGet ?? {};
|
|
256
419
|
return mapWsfeQuotation(raw);
|
|
257
420
|
},
|
|
258
|
-
async getVoucherInfo({
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
forceRefresh
|
|
264
|
-
}) {
|
|
265
|
-
const result = await executeWsfeAuthenticatedOperation(
|
|
266
|
-
"FECompConsultar",
|
|
267
|
-
{
|
|
268
|
-
representedTaxId,
|
|
269
|
-
forceRefresh
|
|
270
|
-
},
|
|
271
|
-
{
|
|
272
|
-
FeCompConsReq: {
|
|
273
|
-
CbteNro: number,
|
|
274
|
-
PtoVta: salesPoint,
|
|
275
|
-
CbteTipo: voucherType
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
);
|
|
279
|
-
const raw = result.ResultGet ?? null;
|
|
280
|
-
if (!raw) {
|
|
281
|
-
return null;
|
|
282
|
-
}
|
|
283
|
-
return mapWsfeVoucherInfo(raw);
|
|
284
|
-
}
|
|
421
|
+
async getVoucherInfo(input) {
|
|
422
|
+
const lookup = await lookupVoucher(input);
|
|
423
|
+
return lookup.kind === "found" ? lookup.voucher : null;
|
|
424
|
+
},
|
|
425
|
+
lookupVoucher
|
|
285
426
|
};
|
|
286
427
|
}
|
|
287
428
|
function mapWsfeVoucherInput(input, voucherNumber) {
|
|
@@ -547,17 +688,45 @@ function mapWsfeQuotation(raw) {
|
|
|
547
688
|
};
|
|
548
689
|
}
|
|
549
690
|
function mapWsfeVoucherInfo(raw) {
|
|
550
|
-
|
|
691
|
+
const voucher = {
|
|
551
692
|
voucherNumber: Number(raw.CbteDesde ?? raw.CbteHasta ?? 0),
|
|
552
|
-
...raw.CbteFch === void 0 ? {} : { voucherDate: String(raw.CbteFch) },
|
|
553
|
-
...raw.PtoVta === void 0 ? {} : { salesPoint: Number(raw.PtoVta) },
|
|
554
|
-
...raw.CbteTipo === void 0 ? {} : { voucherType: Number(raw.CbteTipo) },
|
|
555
|
-
...raw.ImpTotal === void 0 ? {} : { totalAmount: Number(raw.ImpTotal) },
|
|
556
|
-
...raw.Resultado === void 0 ? {} : { result: String(raw.Resultado) },
|
|
557
|
-
...raw.CAE === void 0 ? {} : { cae: String(raw.CAE) },
|
|
558
|
-
...raw.CAEFchVto === void 0 ? {} : { caeExpiry: String(raw.CAEFchVto) },
|
|
559
693
|
raw
|
|
560
694
|
};
|
|
695
|
+
assignWsfeValue(voucher, "voucherDate", normalizeWsfeString(raw.CbteFch));
|
|
696
|
+
assignWsfeValue(voucher, "salesPoint", normalizeWsfeNumber(raw.PtoVta));
|
|
697
|
+
assignWsfeValue(voucher, "voucherType", normalizeWsfeNumber(raw.CbteTipo));
|
|
698
|
+
assignWsfeValue(voucher, "concept", normalizeWsfeNumber(raw.Concepto));
|
|
699
|
+
assignWsfeValue(voucher, "documentType", normalizeWsfeNumber(raw.DocTipo));
|
|
700
|
+
assignWsfeValue(voucher, "documentNumber", normalizeWsfeString(raw.DocNro));
|
|
701
|
+
assignWsfeValue(
|
|
702
|
+
voucher,
|
|
703
|
+
"receiverVatConditionId",
|
|
704
|
+
normalizeWsfeNumber(raw.CondicionIVAReceptorId)
|
|
705
|
+
);
|
|
706
|
+
assignWsfeValue(voucher, "totalAmount", normalizeWsfeNumber(raw.ImpTotal));
|
|
707
|
+
assignWsfeValue(
|
|
708
|
+
voucher,
|
|
709
|
+
"nonTaxableAmount",
|
|
710
|
+
normalizeWsfeNumber(raw.ImpTotConc)
|
|
711
|
+
);
|
|
712
|
+
assignWsfeValue(voucher, "netAmount", normalizeWsfeNumber(raw.ImpNeto));
|
|
713
|
+
assignWsfeValue(voucher, "exemptAmount", normalizeWsfeNumber(raw.ImpOpEx));
|
|
714
|
+
assignWsfeValue(voucher, "taxAmount", normalizeWsfeNumber(raw.ImpTrib));
|
|
715
|
+
assignWsfeValue(voucher, "vatAmount", normalizeWsfeNumber(raw.ImpIVA));
|
|
716
|
+
assignWsfeValue(voucher, "currencyId", normalizeWsfeString(raw.MonId));
|
|
717
|
+
assignWsfeValue(voucher, "exchangeRate", normalizeWsfeNumber(raw.MonCotiz));
|
|
718
|
+
assignWsfeValue(voucher, "result", normalizeWsfeString(raw.Resultado));
|
|
719
|
+
assignWsfeValue(
|
|
720
|
+
voucher,
|
|
721
|
+
"cae",
|
|
722
|
+
normalizeWsfeString(raw.CodAutorizacion ?? raw.CAE)
|
|
723
|
+
);
|
|
724
|
+
assignWsfeValue(
|
|
725
|
+
voucher,
|
|
726
|
+
"caeExpiry",
|
|
727
|
+
normalizeWsfeString(raw.FchVto ?? raw.CAEFchVto)
|
|
728
|
+
);
|
|
729
|
+
return voucher;
|
|
561
730
|
}
|
|
562
731
|
function createWsfeAuth(representedTaxId, token, sign) {
|
|
563
732
|
return {
|
|
@@ -566,47 +735,16 @@ function createWsfeAuth(representedTaxId, token, sign) {
|
|
|
566
735
|
Cuit: Number.parseInt(String(representedTaxId), 10)
|
|
567
736
|
};
|
|
568
737
|
}
|
|
569
|
-
function
|
|
738
|
+
function unwrapWsfeOperationEnvelope(operation, response) {
|
|
570
739
|
const operationResponse = response[`${operation}Response`];
|
|
571
740
|
const result = operationResponse?.[`${operation}Result`] ?? response[`${operation}Result`] ?? response;
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
const observationsContainer = detailResponse.Observaciones;
|
|
577
|
-
const observations = normalizeWsfeErrors(observationsContainer?.Obs);
|
|
578
|
-
if (observations.length > 0) {
|
|
579
|
-
const firstObservation = observations[0];
|
|
580
|
-
if (!firstObservation) {
|
|
581
|
-
throw new ArcaServiceError(
|
|
582
|
-
"WSFE returned an empty observation list",
|
|
583
|
-
{
|
|
584
|
-
detail: result
|
|
585
|
-
}
|
|
586
|
-
);
|
|
587
|
-
}
|
|
588
|
-
throw new ArcaServiceError(firstObservation.message, {
|
|
589
|
-
serviceCode: firstObservation.code,
|
|
590
|
-
detail: result
|
|
591
|
-
});
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
const errorsContainer = result.Errors;
|
|
596
|
-
const errors = normalizeWsfeErrors(errorsContainer?.Err);
|
|
741
|
+
return result;
|
|
742
|
+
}
|
|
743
|
+
function throwForWsfeOperationErrors(operation, result) {
|
|
744
|
+
const errors = extractWsfeGlobalIssues(result, operation);
|
|
597
745
|
if (errors.length > 0) {
|
|
598
|
-
|
|
599
|
-
if (!firstError) {
|
|
600
|
-
throw new ArcaServiceError("WSFE returned an empty error list", {
|
|
601
|
-
detail: result
|
|
602
|
-
});
|
|
603
|
-
}
|
|
604
|
-
throw new ArcaServiceError(firstError.message, {
|
|
605
|
-
serviceCode: firstError.code,
|
|
606
|
-
detail: result
|
|
607
|
-
});
|
|
746
|
+
throw createWsfeServiceError(operation, result, errors);
|
|
608
747
|
}
|
|
609
|
-
return result;
|
|
610
748
|
}
|
|
611
749
|
function normalizeWsfeDetailResponse(result) {
|
|
612
750
|
const detailResponse = result.FeDetResp;
|
|
@@ -616,17 +754,248 @@ function normalizeWsfeDetailResponse(result) {
|
|
|
616
754
|
}
|
|
617
755
|
return rawDetail ?? {};
|
|
618
756
|
}
|
|
619
|
-
function
|
|
757
|
+
function classifyWsfeAuthorization(result, voucherNumber) {
|
|
758
|
+
const operation = "FECAESolicitar";
|
|
759
|
+
const header = toWsfeRecord(result.FeCabResp) ?? {};
|
|
760
|
+
const detail = normalizeWsfeDetailResponse(result);
|
|
761
|
+
const headerResult = normalizeWsfeResult(header.Resultado);
|
|
762
|
+
const detailResult = normalizeWsfeResult(detail.Resultado);
|
|
763
|
+
const resultCode = detailResult ?? headerResult;
|
|
764
|
+
const resultLevel = getWsfeResultLevel(headerResult, detailResult);
|
|
765
|
+
const cae = normalizeWsfeString(detail.CAE);
|
|
766
|
+
const caeExpiry = normalizeWsfeString(detail.CAEFchVto);
|
|
767
|
+
const errors = extractWsfeGlobalIssues(result, operation, "header");
|
|
768
|
+
const observations = extractWsfeObservations(
|
|
769
|
+
detail,
|
|
770
|
+
detailResult === "R" ? "business" : "observation"
|
|
771
|
+
);
|
|
772
|
+
const hasInfrastructureError = errors.some(
|
|
773
|
+
(issue) => issue.category === "infrastructure"
|
|
774
|
+
);
|
|
775
|
+
const base = {
|
|
776
|
+
service: "wsfe",
|
|
777
|
+
operation,
|
|
778
|
+
results: createWsfeResults(headerResult, detailResult),
|
|
779
|
+
errors,
|
|
780
|
+
observations,
|
|
781
|
+
raw: result
|
|
782
|
+
};
|
|
783
|
+
const context = {
|
|
784
|
+
base,
|
|
785
|
+
headerResult,
|
|
786
|
+
detailResult,
|
|
787
|
+
resultCode,
|
|
788
|
+
resultLevel,
|
|
789
|
+
cae,
|
|
790
|
+
caeExpiry
|
|
791
|
+
};
|
|
792
|
+
if (hasContradictoryWsfeResults(context)) {
|
|
793
|
+
return createWsfeStructuredIndeterminate(context, "contradictory_response");
|
|
794
|
+
}
|
|
795
|
+
if (hasInfrastructureError) {
|
|
796
|
+
return createWsfeStructuredIndeterminate(context, "incomplete_response");
|
|
797
|
+
}
|
|
798
|
+
if (isAuthorizedWsfeContext(context)) {
|
|
799
|
+
return {
|
|
800
|
+
...base,
|
|
801
|
+
kind: "authorized",
|
|
802
|
+
result: "A",
|
|
803
|
+
resultLevel: "detail",
|
|
804
|
+
cae: context.cae,
|
|
805
|
+
caeExpiry: context.caeExpiry,
|
|
806
|
+
voucherNumber
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
if (isRejectedWsfeDetailContext(context)) {
|
|
810
|
+
return {
|
|
811
|
+
...base,
|
|
812
|
+
kind: "rejected",
|
|
813
|
+
result: "R",
|
|
814
|
+
resultLevel: "detail"
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
if (isRejectedWsfeHeaderContext(context)) {
|
|
818
|
+
return {
|
|
819
|
+
...base,
|
|
820
|
+
kind: "rejected",
|
|
821
|
+
result: "R",
|
|
822
|
+
resultLevel: "header"
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
return createWsfeStructuredIndeterminate(
|
|
826
|
+
context,
|
|
827
|
+
hasWsfeCaeContradiction(context) ? "contradictory_response" : "incomplete_response"
|
|
828
|
+
);
|
|
829
|
+
}
|
|
830
|
+
function getWsfeResultLevel(headerResult, detailResult) {
|
|
831
|
+
if (detailResult) {
|
|
832
|
+
return "detail";
|
|
833
|
+
}
|
|
834
|
+
return headerResult ? "header" : void 0;
|
|
835
|
+
}
|
|
836
|
+
function hasContradictoryWsfeResults(context) {
|
|
837
|
+
return Boolean(
|
|
838
|
+
context.headerResult && context.detailResult && context.headerResult !== context.detailResult
|
|
839
|
+
);
|
|
840
|
+
}
|
|
841
|
+
function isAuthorizedWsfeContext(context) {
|
|
842
|
+
return Boolean(
|
|
843
|
+
context.detailResult === "A" && context.headerResult !== "R" && context.base.errors.length === 0 && context.cae && context.caeExpiry
|
|
844
|
+
);
|
|
845
|
+
}
|
|
846
|
+
function isRejectedWsfeDetailContext(context) {
|
|
847
|
+
return context.detailResult === "R" && context.headerResult !== "A" && !context.cae;
|
|
848
|
+
}
|
|
849
|
+
function isRejectedWsfeHeaderContext(context) {
|
|
850
|
+
return context.headerResult === "R" && context.detailResult === void 0 && !context.cae && context.base.errors.length > 0 && context.base.errors.every((issue) => issue.category === "business");
|
|
851
|
+
}
|
|
852
|
+
function hasWsfeCaeContradiction(context) {
|
|
853
|
+
return (context.resultCode === "A" || context.resultCode === "R") && Boolean(context.cae);
|
|
854
|
+
}
|
|
855
|
+
function createWsfeStructuredIndeterminate(context, reason) {
|
|
856
|
+
const outcome = {
|
|
857
|
+
...context.base,
|
|
858
|
+
kind: "indeterminate",
|
|
859
|
+
reason
|
|
860
|
+
};
|
|
861
|
+
assignWsfeValue(outcome, "result", context.resultCode);
|
|
862
|
+
assignWsfeValue(outcome, "resultLevel", context.resultLevel);
|
|
863
|
+
assignWsfeValue(outcome, "cae", context.cae);
|
|
864
|
+
assignWsfeValue(outcome, "caeExpiry", context.caeExpiry);
|
|
865
|
+
return outcome;
|
|
866
|
+
}
|
|
867
|
+
function createWsfeResults(headerResult, detailResult) {
|
|
868
|
+
const results = {};
|
|
869
|
+
assignWsfeValue(results, "header", headerResult);
|
|
870
|
+
assignWsfeValue(results, "detail", detailResult);
|
|
871
|
+
return results;
|
|
872
|
+
}
|
|
873
|
+
function createWsfeIndeterminateOutcome(error) {
|
|
874
|
+
return {
|
|
875
|
+
kind: "indeterminate",
|
|
876
|
+
service: "wsfe",
|
|
877
|
+
operation: "FECAESolicitar",
|
|
878
|
+
results: {},
|
|
879
|
+
reason: getArcaIndeterminateReason(error),
|
|
880
|
+
errors: [],
|
|
881
|
+
observations: []
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
function getArcaIndeterminateReason(error) {
|
|
885
|
+
if (error instanceof ArcaTransportError) {
|
|
886
|
+
return "transport_error";
|
|
887
|
+
}
|
|
888
|
+
if (error instanceof ArcaSoapFaultError) {
|
|
889
|
+
return "soap_fault";
|
|
890
|
+
}
|
|
891
|
+
if (error instanceof ArcaInvalidSoapResponseError) {
|
|
892
|
+
return "invalid_response";
|
|
893
|
+
}
|
|
894
|
+
return "unexpected_error";
|
|
895
|
+
}
|
|
896
|
+
function createWsfeOutcomeError(outcome) {
|
|
897
|
+
const issues = [...outcome.errors, ...outcome.observations];
|
|
898
|
+
const firstIssue = issues[0];
|
|
899
|
+
const message = firstIssue ? formatWsfeIssue(firstIssue) : outcome.kind === "rejected" ? "WSFE rejected the voucher authorization" : outcome.result === "A" ? "WSFE did not return CAE authorization data" : "WSFE did not return conclusive voucher authorization data";
|
|
900
|
+
return new ArcaServiceError(message, {
|
|
901
|
+
service: "wsfe",
|
|
902
|
+
operation: outcome.operation,
|
|
903
|
+
...firstIssue?.code === void 0 ? {} : { serviceCode: firstIssue.code },
|
|
904
|
+
...outcome.result === void 0 ? {} : { result: outcome.result },
|
|
905
|
+
...outcome.resultLevel === void 0 ? {} : { resultLevel: outcome.resultLevel },
|
|
906
|
+
results: outcome.results,
|
|
907
|
+
...outcome.kind === "indeterminate" && outcome.cae ? { cae: outcome.cae } : {},
|
|
908
|
+
issues,
|
|
909
|
+
detail: outcome.raw
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
function createWsfeServiceError(operation, result, issues) {
|
|
913
|
+
const firstIssue = issues[0];
|
|
914
|
+
return new ArcaServiceError(
|
|
915
|
+
firstIssue ? formatWsfeIssue(firstIssue) : "WSFE returned a service error",
|
|
916
|
+
{
|
|
917
|
+
service: "wsfe",
|
|
918
|
+
operation,
|
|
919
|
+
...firstIssue?.code === void 0 ? {} : { serviceCode: firstIssue.code },
|
|
920
|
+
issues,
|
|
921
|
+
detail: result
|
|
922
|
+
}
|
|
923
|
+
);
|
|
924
|
+
}
|
|
925
|
+
function extractWsfeGlobalIssues(result, operation, resultLevel) {
|
|
926
|
+
const errorsContainer = toWsfeRecord(result.Errors);
|
|
927
|
+
return normalizeWsfeIssueEntries(errorsContainer?.Err).map((entry) => ({
|
|
928
|
+
service: "wsfe",
|
|
929
|
+
operation,
|
|
930
|
+
source: "error",
|
|
931
|
+
category: operation === "FECAESolicitar" && WSFE_AUTHORIZATION_INFRASTRUCTURE_CODES.has(entry.code ?? "") ? "infrastructure" : operation === "FECAESolicitar" ? "business" : "unknown",
|
|
932
|
+
...entry.code === void 0 ? {} : { code: entry.code },
|
|
933
|
+
message: entry.message,
|
|
934
|
+
...resultLevel === void 0 ? {} : { resultLevel }
|
|
935
|
+
}));
|
|
936
|
+
}
|
|
937
|
+
function extractWsfeObservations(detail, category) {
|
|
938
|
+
const observationsContainer = toWsfeRecord(detail.Observaciones);
|
|
939
|
+
return normalizeWsfeIssueEntries(observationsContainer?.Obs).map((entry) => ({
|
|
940
|
+
service: "wsfe",
|
|
941
|
+
operation: "FECAESolicitar",
|
|
942
|
+
source: "observation",
|
|
943
|
+
category,
|
|
944
|
+
...entry.code === void 0 ? {} : { code: entry.code },
|
|
945
|
+
message: entry.message,
|
|
946
|
+
resultLevel: "detail"
|
|
947
|
+
}));
|
|
948
|
+
}
|
|
949
|
+
function normalizeWsfeIssueEntries(rawErrors) {
|
|
620
950
|
const entries = Array.isArray(rawErrors) ? rawErrors : rawErrors ? [rawErrors] : [];
|
|
621
951
|
return entries.map((entry) => entry).map((entry) => {
|
|
622
|
-
const code = entry.Code ?? entry.code
|
|
952
|
+
const code = entry.Code ?? entry.code;
|
|
623
953
|
const message = entry.Msg ?? entry.msg ?? "Unknown WSFE error";
|
|
624
954
|
return {
|
|
625
|
-
code: String(code),
|
|
626
|
-
message:
|
|
955
|
+
...code === void 0 ? {} : { code: String(code) },
|
|
956
|
+
message: String(message)
|
|
627
957
|
};
|
|
628
958
|
});
|
|
629
959
|
}
|
|
960
|
+
function formatWsfeIssue(issue) {
|
|
961
|
+
return issue.code ? `(${issue.code}) ${issue.message}` : issue.message;
|
|
962
|
+
}
|
|
963
|
+
function normalizeWsfeResult(value) {
|
|
964
|
+
if (typeof value !== "string") {
|
|
965
|
+
return void 0;
|
|
966
|
+
}
|
|
967
|
+
const normalized = value.trim().toUpperCase();
|
|
968
|
+
return normalized || void 0;
|
|
969
|
+
}
|
|
970
|
+
function normalizeWsfeString(value) {
|
|
971
|
+
if (value === void 0 || value === null) {
|
|
972
|
+
return void 0;
|
|
973
|
+
}
|
|
974
|
+
const normalized = String(value).trim();
|
|
975
|
+
return normalized || void 0;
|
|
976
|
+
}
|
|
977
|
+
function normalizeWsfeNumber(value) {
|
|
978
|
+
if (value === void 0 || value === null || value === "") {
|
|
979
|
+
return void 0;
|
|
980
|
+
}
|
|
981
|
+
const normalized = Number(value);
|
|
982
|
+
return Number.isFinite(normalized) ? normalized : void 0;
|
|
983
|
+
}
|
|
984
|
+
function assignWsfeValue(target, key, value) {
|
|
985
|
+
if (value !== void 0) {
|
|
986
|
+
target[key] = value;
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
function toWsfeRecord(value) {
|
|
990
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
991
|
+
}
|
|
992
|
+
var WSFE_AUTHORIZATION_INFRASTRUCTURE_CODES = /* @__PURE__ */ new Set([
|
|
993
|
+
"500",
|
|
994
|
+
"501",
|
|
995
|
+
"502",
|
|
996
|
+
"600",
|
|
997
|
+
"601"
|
|
998
|
+
]);
|
|
630
999
|
function getWsfeResultEntries(result, key) {
|
|
631
1000
|
const rawEntries = result.ResultGet?.[key];
|
|
632
1001
|
if (!rawEntries) {
|