Skip to main content
Skip to main content

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 (ITaxModuleService). It accepts an array of line items and shipping methods, and the context of the calculation.

It returns the tax lines that are used to adjust the cart’s tax and grand totals.

const taxLines = await taxModuleService.getTaxLines(
[
{
id: "cali_123",
product_id: "prod_123",
unit_price: 1000,
quantity: 1,
},
{
id: "casm_123",
shipping_option_id: "so_123",
unit_price: 2000,
},
],
{
address: {
country_code: "us",
},
}
)

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

Note

Learn more about the getTaxLines method’s parameters and return type in this reference.

The example above returns the tax lines based on the tax region for the United States (US).


Tax Provider Usage in the Calculation

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

A tax provider is a TypeScript or JavaScript class that 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.


Create Tax Provider

You can create a tax provider by implementing the ITaxProvider imported from @meduasjs/types.

Then, you can add the tax provider to the providers option and use it to retrieve tax lines.

Refer to this reference to learn more about creating a tax provider.

Was this section helpful?