2023-03-02 11:01:17 +00:00
|
|
|
import Avatax from "avatax";
|
|
|
|
import { CreateTransactionModel } from "avatax/lib/models/CreateTransactionModel";
|
2023-03-30 11:12:52 +00:00
|
|
|
import pino from "pino";
|
2023-03-10 12:04:25 +00:00
|
|
|
import packageJson from "../../../package.json";
|
2023-03-30 11:12:52 +00:00
|
|
|
import { createLogger } from "../../lib/logger";
|
2023-03-02 11:01:17 +00:00
|
|
|
import { AvataxConfig } from "./avatax-config";
|
|
|
|
|
|
|
|
type AvataxSettings = {
|
|
|
|
appName: string;
|
|
|
|
appVersion: string;
|
|
|
|
environment: "sandbox" | "production";
|
|
|
|
machineName: string;
|
|
|
|
timeout: number;
|
|
|
|
logOptions?: {
|
|
|
|
logEnabled: boolean;
|
|
|
|
logLevel: number;
|
|
|
|
logRequestAndResponseInfo: boolean;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultAvataxSettings: AvataxSettings = {
|
|
|
|
appName: packageJson.name,
|
|
|
|
appVersion: packageJson.version,
|
|
|
|
environment: "sandbox",
|
|
|
|
machineName: "tax-app",
|
|
|
|
timeout: 5000,
|
|
|
|
};
|
|
|
|
|
|
|
|
const createAvataxSettings = (config: AvataxConfig): AvataxSettings => {
|
|
|
|
const settings: AvataxSettings = {
|
|
|
|
...defaultAvataxSettings,
|
|
|
|
environment: config.isSandbox ? "sandbox" : "production",
|
|
|
|
};
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class AvataxClient {
|
|
|
|
private client: Avatax;
|
2023-03-30 11:12:52 +00:00
|
|
|
private logger: pino.Logger;
|
2023-03-02 11:01:17 +00:00
|
|
|
|
|
|
|
constructor(config: AvataxConfig) {
|
2023-03-30 11:12:52 +00:00
|
|
|
this.logger = createLogger({ service: "AvataxClient" });
|
|
|
|
this.logger.trace("AvataxClient constructor");
|
2023-03-02 11:01:17 +00:00
|
|
|
const { username, password } = config;
|
|
|
|
const credentials = {
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
};
|
|
|
|
const settings = createAvataxSettings(config);
|
|
|
|
const avataxClient = new Avatax(settings).withSecurity(credentials);
|
2023-03-30 11:12:52 +00:00
|
|
|
this.logger.trace({ client: avataxClient }, "External Avatax client created");
|
2023-03-02 11:01:17 +00:00
|
|
|
this.client = avataxClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchTaxesForOrder(model: CreateTransactionModel) {
|
2023-03-30 11:12:52 +00:00
|
|
|
this.logger.debug({ model }, "fetchTaxesForOrder called with:");
|
|
|
|
|
2023-03-02 11:01:17 +00:00
|
|
|
return this.client.createTransaction({ model });
|
|
|
|
}
|
|
|
|
|
|
|
|
async ping() {
|
2023-03-30 11:12:52 +00:00
|
|
|
this.logger.debug("ping called");
|
2023-03-10 12:04:25 +00:00
|
|
|
try {
|
|
|
|
const result = await this.client.ping();
|
|
|
|
|
|
|
|
return {
|
|
|
|
authenticated: result.authenticated,
|
|
|
|
...(!result.authenticated && {
|
|
|
|
error: "Avatax was not able to authenticate with the provided credentials.",
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
authenticated: false,
|
|
|
|
error: "Avatax was not able to authenticate with the provided credentials.",
|
|
|
|
};
|
|
|
|
}
|
2023-03-02 11:01:17 +00:00
|
|
|
}
|
|
|
|
}
|