2023-03-02 11:01:17 +00:00
|
|
|
import TaxJar from "taxjar";
|
2023-04-21 05:55:43 +00:00
|
|
|
import { AddressParams, Config, CreateOrderParams, TaxParams } from "taxjar/dist/util/types";
|
2023-05-05 06:15:47 +00:00
|
|
|
import { createLogger, Logger } from "../../lib/logger";
|
2023-06-15 07:01:50 +00:00
|
|
|
import { TaxJarConfig } from "./taxjar-connection-schema";
|
2023-03-02 11:01:17 +00:00
|
|
|
|
|
|
|
const createTaxJarSettings = (config: TaxJarConfig): Config => {
|
|
|
|
const settings: Config = {
|
2023-06-15 07:01:50 +00:00
|
|
|
apiKey: config.credentials.apiKey,
|
2023-03-02 11:01:17 +00:00
|
|
|
apiUrl: config.isSandbox ? TaxJar.SANDBOX_API_URL : TaxJar.DEFAULT_API_URL,
|
|
|
|
};
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
};
|
|
|
|
|
2023-04-17 11:58:21 +00:00
|
|
|
export type FetchTaxForOrderArgs = {
|
|
|
|
params: TaxParams;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type CreateOrderArgs = {
|
|
|
|
params: CreateOrderParams;
|
|
|
|
};
|
|
|
|
|
2023-04-21 05:55:43 +00:00
|
|
|
export type ValidateAddressArgs = {
|
|
|
|
params: AddressParams;
|
|
|
|
};
|
|
|
|
|
2023-03-02 11:01:17 +00:00
|
|
|
export class TaxJarClient {
|
|
|
|
private client: TaxJar;
|
2023-05-05 06:15:47 +00:00
|
|
|
private logger: Logger;
|
2023-03-02 11:01:17 +00:00
|
|
|
|
|
|
|
constructor(providerConfig: TaxJarConfig) {
|
2023-06-20 15:53:27 +00:00
|
|
|
this.logger = createLogger({ name: "TaxJarClient" });
|
2023-03-02 11:01:17 +00:00
|
|
|
const settings = createTaxJarSettings(providerConfig);
|
|
|
|
const taxJarClient = new TaxJar(settings);
|
2023-04-17 11:58:21 +00:00
|
|
|
|
2023-03-02 11:01:17 +00:00
|
|
|
this.client = taxJarClient;
|
|
|
|
}
|
|
|
|
|
2023-04-17 11:58:21 +00:00
|
|
|
async fetchTaxForOrder({ params }: FetchTaxForOrderArgs) {
|
|
|
|
const response = await this.client.taxForOrder(params);
|
|
|
|
|
2023-03-02 11:01:17 +00:00
|
|
|
return response;
|
|
|
|
}
|
2023-03-10 12:04:25 +00:00
|
|
|
|
2023-04-17 11:58:21 +00:00
|
|
|
async createOrder({ params }: CreateOrderArgs) {
|
|
|
|
return this.client.createOrder(params);
|
|
|
|
}
|
2023-04-21 05:55:43 +00:00
|
|
|
|
2023-06-19 10:06:05 +00:00
|
|
|
/**
|
|
|
|
* TaxJar validateAddress doesn't work. It's turned off for now.
|
|
|
|
* @see https://github.com/taxjar/taxjar-node/issues/70
|
|
|
|
* @todo Revisit this when TaxJar fixes the issue. Alternatively, create a custom validation.
|
|
|
|
*/
|
2023-04-21 05:55:43 +00:00
|
|
|
async validateAddress({ params }: ValidateAddressArgs) {
|
2023-06-19 10:06:05 +00:00
|
|
|
// return this.client.validateAddress(params);
|
2023-04-21 05:55:43 +00:00
|
|
|
}
|
2023-06-20 15:53:27 +00:00
|
|
|
|
|
|
|
async getTaxCodes() {
|
|
|
|
// ! This function doesn't accept any params. This may be troublesome if we want to do pagination/filtering on the frontend.
|
|
|
|
return this.client.categories();
|
|
|
|
}
|
2023-03-02 11:01:17 +00:00
|
|
|
}
|