feat: support ARCA production environment with dynamic endpoint configuration and environment selection
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { IArcaCredentialDocument } from "./ArcaCredentials.Adapter.Mongoose";
|
||||
import { ARCA_WSAA_ENVIRONMENT, ARCA_WSAA_SERVICE } from "../ArcaWsaaTokens/ArcaWsaaTokens.Interface";
|
||||
|
||||
export enum ARCA_CREDENTIAL_STATUS {
|
||||
CSR_GENERATED = "CSR_GENERATED",
|
||||
@@ -26,8 +27,9 @@ export type TestArcaWsaaLoginParams = {
|
||||
|
||||
export type ArcaWsaaLoginTestResult = {
|
||||
status: "cached" | "created";
|
||||
environment: "homologation";
|
||||
service: "wsfe";
|
||||
environment: ARCA_WSAA_ENVIRONMENT;
|
||||
service: ARCA_WSAA_SERVICE;
|
||||
endpoints: ArcaEndpointMetadata;
|
||||
expirationTime: Date;
|
||||
cached: boolean;
|
||||
};
|
||||
@@ -38,8 +40,9 @@ export type ArcaWsfeLastVoucherParams = {
|
||||
};
|
||||
|
||||
export type ArcaWsfeLastVoucherResult = {
|
||||
environment: "homologation";
|
||||
service: "wsfe";
|
||||
environment: ARCA_WSAA_ENVIRONMENT;
|
||||
service: ARCA_WSAA_SERVICE;
|
||||
endpoints: ArcaEndpointMetadata;
|
||||
pointOfSale: number;
|
||||
voucherType: 11;
|
||||
lastVoucherNumber: number;
|
||||
@@ -58,8 +61,9 @@ export type ArcaWsfeIssueFacturaCParams = {
|
||||
};
|
||||
|
||||
export type ArcaWsfeIssueFacturaCResult = {
|
||||
environment: "homologation";
|
||||
service: "wsfe";
|
||||
environment: ARCA_WSAA_ENVIRONMENT;
|
||||
service: ARCA_WSAA_SERVICE;
|
||||
endpoints: ArcaEndpointMetadata;
|
||||
pointOfSale: number;
|
||||
voucherType: 11;
|
||||
voucherNumber: number;
|
||||
@@ -78,9 +82,15 @@ export type ArcaWsfeDiagnosticsParams = {
|
||||
sessionUser: string;
|
||||
};
|
||||
|
||||
export type ArcaEndpointMetadata = {
|
||||
wsaaHost?: string;
|
||||
wsfeHost?: string;
|
||||
};
|
||||
|
||||
export type ArcaWsfeDiagnosticsResult = {
|
||||
environment: "homologation";
|
||||
service: "wsfe";
|
||||
environment: ARCA_WSAA_ENVIRONMENT;
|
||||
service: ARCA_WSAA_SERVICE;
|
||||
endpoints: ArcaEndpointMetadata;
|
||||
dummy: {
|
||||
ok: boolean;
|
||||
appServer?: string;
|
||||
|
||||
@@ -200,6 +200,35 @@ class ArcaCredentialsManager implements IArcaCredentialsManager {
|
||||
return parsedValue;
|
||||
}
|
||||
|
||||
private getArcaEnvironment(): ARCA_WSAA_ENVIRONMENT {
|
||||
const environment = (process.env.ARCA_ENVIRONMENT || ARCA_WSAA_ENVIRONMENT.HOMOLOGATION).trim().toLowerCase();
|
||||
|
||||
if (!Object.values(ARCA_WSAA_ENVIRONMENT).includes(environment as ARCA_WSAA_ENVIRONMENT)) {
|
||||
throw new Error("ARCA_ENVIRONMENT debe ser 'homologation' o 'production'");
|
||||
}
|
||||
|
||||
return environment as ARCA_WSAA_ENVIRONMENT;
|
||||
}
|
||||
|
||||
private getEndpointHost(value?: string): string | undefined {
|
||||
if (!value) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
return new URL(value).host;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
private getArcaEndpointMetadata() {
|
||||
return {
|
||||
wsaaHost: this.getEndpointHost(process.env.ARCA_WSAA_URL),
|
||||
wsfeHost: this.getEndpointHost(process.env.ARCA_WSFE_URL),
|
||||
};
|
||||
}
|
||||
|
||||
private signTraCms(traXml: string, certificatePem: string, privateKeyPem: string): string {
|
||||
const p7 = forge.pkcs7.createSignedData();
|
||||
p7.content = forge.util.createBuffer(traXml, "utf8");
|
||||
@@ -270,7 +299,7 @@ class ArcaCredentialsManager implements IArcaCredentialsManager {
|
||||
cached: boolean;
|
||||
}> {
|
||||
const service = ARCA_WSAA_SERVICE.WSFE;
|
||||
const environment = ARCA_WSAA_ENVIRONMENT.HOMOLOGATION;
|
||||
const environment = this.getArcaEnvironment();
|
||||
const cachedToken = await ArcaWsaaTokens.getValidToken({ companyId, service, environment });
|
||||
|
||||
if (cachedToken) {
|
||||
@@ -582,7 +611,8 @@ class ArcaCredentialsManager implements IArcaCredentialsManager {
|
||||
private parseWsfeIssueFacturaCReturn(
|
||||
xml: string,
|
||||
pointOfSale: number,
|
||||
voucherNumber: number
|
||||
voucherNumber: number,
|
||||
environment: ARCA_WSAA_ENVIRONMENT
|
||||
): ArcaWsfeIssueFacturaCResult {
|
||||
const faultString = this.extractXmlValue(xml, "faultstring");
|
||||
|
||||
@@ -600,8 +630,9 @@ class ArcaCredentialsManager implements IArcaCredentialsManager {
|
||||
}
|
||||
|
||||
return {
|
||||
environment: ARCA_WSAA_ENVIRONMENT.HOMOLOGATION,
|
||||
environment,
|
||||
service: ARCA_WSAA_SERVICE.WSFE,
|
||||
endpoints: this.getArcaEndpointMetadata(),
|
||||
pointOfSale,
|
||||
voucherType: FACTURA_C_VOUCHER_TYPE,
|
||||
voucherNumber,
|
||||
@@ -807,13 +838,14 @@ class ArcaCredentialsManager implements IArcaCredentialsManager {
|
||||
await this.validateCompanyAdmin(data.companyId, data.sessionUser);
|
||||
|
||||
const service = ARCA_WSAA_SERVICE.WSFE;
|
||||
const environment = ARCA_WSAA_ENVIRONMENT.HOMOLOGATION;
|
||||
const environment = this.getArcaEnvironment();
|
||||
const loginTicket = await this.getWsaaLoginTicket(data.companyId);
|
||||
|
||||
return {
|
||||
status: loginTicket.cached ? "cached" : "created",
|
||||
environment,
|
||||
service,
|
||||
endpoints: this.getArcaEndpointMetadata(),
|
||||
expirationTime: loginTicket.expirationTime,
|
||||
cached: loginTicket.cached,
|
||||
};
|
||||
@@ -823,6 +855,7 @@ class ArcaCredentialsManager implements IArcaCredentialsManager {
|
||||
await this.validateCompanyAdmin(data.companyId, data.sessionUser);
|
||||
const fiscalProfile = await this.requireActiveFiscalProfile(data.companyId, data.sessionUser);
|
||||
const loginTicket = await this.getWsaaLoginTicket(data.companyId);
|
||||
const environment = this.getArcaEnvironment();
|
||||
const voucherType = FACTURA_C_VOUCHER_TYPE;
|
||||
let responseXml: string;
|
||||
|
||||
@@ -842,8 +875,9 @@ class ArcaCredentialsManager implements IArcaCredentialsManager {
|
||||
}
|
||||
|
||||
return {
|
||||
environment: ARCA_WSAA_ENVIRONMENT.HOMOLOGATION,
|
||||
environment,
|
||||
service: ARCA_WSAA_SERVICE.WSFE,
|
||||
endpoints: this.getArcaEndpointMetadata(),
|
||||
pointOfSale: fiscalProfile.pointOfSale,
|
||||
voucherType,
|
||||
lastVoucherNumber: this.parseWsfeLastVoucherReturn(responseXml),
|
||||
@@ -859,6 +893,7 @@ class ArcaCredentialsManager implements IArcaCredentialsManager {
|
||||
|
||||
const fiscalProfile = await this.requireActiveFiscalProfile(data.companyId, data.sessionUser);
|
||||
const loginTicket = await this.getWsaaLoginTicket(data.companyId);
|
||||
const environment = this.getArcaEnvironment();
|
||||
const voucherType = 11;
|
||||
const receiverDocumentType = this.parseXmlInteger(
|
||||
data.receiverDocumentType ?? FINAL_CONSUMER_DOCUMENT_TYPE,
|
||||
@@ -908,7 +943,8 @@ class ArcaCredentialsManager implements IArcaCredentialsManager {
|
||||
"FECAESolicitar"
|
||||
),
|
||||
fiscalProfile.pointOfSale,
|
||||
voucherNumber
|
||||
voucherNumber,
|
||||
environment
|
||||
);
|
||||
} catch (error) {
|
||||
throw new ArcaWsfeIssueFacturaCError(
|
||||
@@ -927,11 +963,13 @@ class ArcaCredentialsManager implements IArcaCredentialsManager {
|
||||
await this.validateCompanyAdmin(data.companyId, data.sessionUser);
|
||||
const fiscalProfile = await this.requireActiveFiscalProfile(data.companyId, data.sessionUser);
|
||||
const loginTicket = await this.getWsaaLoginTicket(data.companyId);
|
||||
const environment = this.getArcaEnvironment();
|
||||
const voucherType = 11;
|
||||
|
||||
const diagnostics: ArcaWsfeDiagnosticsResult = {
|
||||
environment: ARCA_WSAA_ENVIRONMENT.HOMOLOGATION,
|
||||
environment,
|
||||
service: ARCA_WSAA_SERVICE.WSFE,
|
||||
endpoints: this.getArcaEndpointMetadata(),
|
||||
dummy: { ok: false },
|
||||
voucherTypes: { ok: false },
|
||||
pointsOfSale: {
|
||||
|
||||
@@ -6,6 +6,7 @@ export enum ARCA_WSAA_SERVICE {
|
||||
|
||||
export enum ARCA_WSAA_ENVIRONMENT {
|
||||
HOMOLOGATION = "homologation",
|
||||
PRODUCTION = "production",
|
||||
}
|
||||
|
||||
export type ArcaWsaaTokenLookupParams = {
|
||||
|
||||
Reference in New Issue
Block a user