Tax Calculation with the Tax Provider

In this document, you’ll learn how tax lines are calculated and what a tax provider is.

Tax Lines Calculation#

Tax lines are calculated and retrieved using the getTaxLines method of the Tax Module’s main service. It accepts an array of line items and shipping methods, and the context of the calculation.

For example:

Code
1const taxLines = await taxModuleService.getTaxLines(2  [3    {4      id: "cali_123",5      product_id: "prod_123",6      unit_price: 1000,7      quantity: 1,8    },9    {10      id: "casm_123",11      shipping_option_id: "so_123",12      unit_price: 2000,13    },14  ],15  {16    address: {17      country_code: "us",18    },19  }20)

The context object is used to determine which tax regions and rates to use in the calculation. It includes properties related to the address and customer.

The example above retrieves the tax lines based on the tax region for the United States.

The method returns tax lines for the line item and shipping methods. For example:

Code
1[2  {3    "line_item_id": "cali_123",4    "rate_id": "txr_1",5    "rate": 10,6    "code": "XXX",7    "name": "Tax Rate 1"8  },9  {10    "shipping_line_id": "casm_123",11    "rate_id": "txr_2",12    "rate": 5,13    "code": "YYY",14    "name": "Tax Rate 2"15  }16]

Using the Tax Provider in the Calculation#

The tax lines retrieved by the getTaxLines method are actually retrieved from the tax region’s provider.

A tax module provider whose main service implements the logic to shape tax lines. Each tax region has a tax provider.

The Tax Module provides a system tax provider that only transforms calculated item and shipping tax rates into the required return type.

Was this page helpful?
Edit this page