|
1
|
export const metadata = { |
|
2
|
title: `Tax Calculation with the Tax Provider`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this guide, you’ll learn how tax lines are calculated using the tax provider. |
|
8
|
|
|
9
|
## Tax Lines Calculation |
|
10
|
|
|
11
|
Tax lines are calculated and retrieved using the [getTaxLines method of the Tax Module’s main service](/references/tax/getTaxLines). It accepts an array of line items and shipping methods, and the context of the calculation. |
|
12
|
|
|
13
|
For example: |
|
14
|
|
|
15
|
```ts |
|
16
|
const taxLines = await taxModuleService.getTaxLines( |
|
17
|
[ |
|
18
|
{ |
|
19
|
id: "cali_123", |
|
20
|
product_id: "prod_123", |
|
21
|
unit_price: 10, |
|
22
|
quantity: 1, |
|
23
|
}, |
|
24
|
{ |
|
25
|
id: "casm_123", |
|
26
|
shipping_option_id: "so_123", |
|
27
|
unit_price: 20, |
|
28
|
}, |
|
29
|
], |
|
30
|
{ |
|
31
|
address: { |
|
32
|
country_code: "us", |
|
33
|
}, |
|
34
|
} |
|
35
|
) |
|
36
|
``` |
|
37
|
|
|
38
|
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. |
|
39
|
|
|
40
|
The example above retrieves the tax lines based on the tax region for the United States. |
|
41
|
|
|
42
|
The method returns tax lines for the line item and shipping methods. For example: |
|
43
|
|
|
44
|
```json |
|
45
|
[ |
|
46
|
{ |
|
47
|
"line_item_id": "cali_123", |
|
48
|
"rate_id": "txr_1", |
|
49
|
"rate": 10, |
|
50
|
"code": "XXX", |
|
51
|
"name": "Tax Rate 1", |
|
52
|
"data": { |
|
53
|
"state_rate": 6, |
|
54
|
"county_rate": 2, |
|
55
|
"city_rate": 2 |
|
56
|
} |
|
57
|
}, |
|
58
|
{ |
|
59
|
"shipping_line_id": "casm_123", |
|
60
|
"rate_id": "txr_2", |
|
61
|
"rate": 5, |
|
62
|
"code": "YYY", |
|
63
|
"name": "Tax Rate 2" |
|
64
|
} |
|
65
|
] |
|
66
|
``` |
|
67
|
|
|
68
|
Each tax line can include a `data` field with additional information returned by the tax provider, such as a breakdown of rates by jurisdiction (state, county, city). This field is stored on the tax line and is useful when integrating with third-party tax services like Avalara that return granular tax details. |
|
69
|
|
|
70
|
--- |
|
71
|
|
|
72
|
## Using the Tax Provider in the Calculation |
|
73
|
|
|
74
|
The tax lines retrieved by the `getTaxLines` method are actually retrieved from the tax region’s [Tax Module Provider](../tax-provider/page.mdx). |
|
75
|
|
|
76
|
A tax module implements the logic to shape tax lines. Each tax region uses a tax provider. |
|
77
|
|
|
78
|
Learn more about tax providers, configuring, and creating them in the [Tax Module Provider](../tax-provider/page.mdx) guide. |