Raw
1 ---
2 generate_toc: true
3 ---
4
5 import { CodeTabs, CodeTab } from "docs-ui"
6
7 export const metadata = {
8 title: `Tax Module`,
9 }
10
11 # {metadata.title}
12
13 In this section of the documentation, you will find resources to learn more about the Tax Module and how to use it in your application.
14
15 <Note title="Looking for no-code docs?">
16
17 Refer to the [Medusa Admin User Guide](!user-guide!/settings/tax-regions) to learn how to manage tax regions using the dashboard.
18
19 </Note>
20
21 Medusa has tax related features available out-of-the-box through the Tax Module. A [module](!docs!/learn/fundamentals/modules) is a standalone package that provides features for a single domain. Each of Medusa's commerce features are placed in Commerce Modules, such as this Tax Module.
22
23 <Note>
24
25 Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation).
26
27 </Note>
28
29 ## Tax Features
30
31 - [Tax Settings Per Region](./tax-region/page.mdx): Set different tax settings for each tax region.
32 - [Tax Rates and Rules](./tax-rates-and-rules/page.mdx): Manage each region's default tax rates and override them with conditioned tax rates.
33 - [Retrieve Tax Lines for carts and orders](./tax-calculation-with-provider/page.mdx): Calculate and retrieve the tax lines of a cart or order's line items and shipping methods with tax providers.
34 - [Custom Tax Providers](./tax-provider/page.mdx): Create custom tax providers to calculate tax lines differently for each tax region.
35
36 ---
37
38 ## How to Use Tax Module's Service
39
40 In your Medusa application, you build flows around Commerce Modules. A flow is built as a [Workflow](!docs!/learn/fundamentals/workflows), which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.
41
42 You can build custom workflows and steps. You can also re-use Medusa's workflows and steps, which are provided by the `@medusajs/medusa/core-flows` package.
43
44 For example:
45
46 export const highlights = [
47 ["12", "Modules.TAX", "Resolve the module in a step."]
48 ]
49
50 ```ts title="src/workflows/create-tax-region.ts" highlights={highlights}
51 import {
52 createWorkflow,
53 WorkflowResponse,
54 createStep,
55 StepResponse,
56 } from "@medusajs/framework/workflows-sdk"
57 import { Modules } from "@medusajs/framework/utils"
58
59 const createTaxRegionStep = createStep(
60 "create-tax-region",
61 async ({}, { container }) => {
62 const taxModuleService = container.resolve(Modules.TAX)
63
64 const taxRegion = await taxModuleService.createTaxRegions({
65 country_code: "us",
66 })
67
68 return new StepResponse({ taxRegion }, taxRegion.id)
69 },
70 async (taxRegionId, { container }) => {
71 if (!taxRegionId) {
72 return
73 }
74 const taxModuleService = container.resolve(Modules.TAX)
75
76 await taxModuleService.deleteTaxRegions([taxRegionId])
77 }
78 )
79
80 export const createTaxRegionWorkflow = createWorkflow(
81 "create-tax-region",
82 () => {
83 const { taxRegion } = createTaxRegionStep()
84
85 return new WorkflowResponse({ taxRegion })
86 }
87 )
88 ```
89
90 You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:
91
92 <CodeTabs group="resource-types">
93 <CodeTab label="API Route" value="api-route">
94
95 ```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
96 import type {
97 MedusaRequest,
98 MedusaResponse,
99 } from "@medusajs/framework/http"
100 import { createTaxRegionWorkflow } from "../../workflows/create-tax-region"
101
102 export async function GET(
103 req: MedusaRequest,
104 res: MedusaResponse
105 ) {
106 const { result } = await createTaxRegionWorkflow(req.scope)
107 .run()
108
109 res.send(result)
110 }
111 ```
112
113 </CodeTab>
114 <CodeTab label="Subscriber" value="subscriber">
115
116 ```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
117 import {
118 type SubscriberConfig,
119 type SubscriberArgs,
120 } from "@medusajs/framework"
121 import { createTaxRegionWorkflow } from "../workflows/create-tax-region"
122
123 export default async function handleUserCreated({
124 event: { data },
125 container,
126 }: SubscriberArgs<{ id: string }>) {
127 const { result } = await createTaxRegionWorkflow(container)
128 .run()
129
130 console.log(result)
131 }
132
133 export const config: SubscriberConfig = {
134 event: "user.created",
135 }
136 ```
137
138 </CodeTab>
139 <CodeTab label="Scheduled Job" value="scheduled-job">
140
141 ```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]}
142 import { MedusaContainer } from "@medusajs/framework/types"
143 import { createTaxRegionWorkflow } from "../workflows/create-tax-region"
144
145 export default async function myCustomJob(
146 container: MedusaContainer
147 ) {
148 const { result } = await createTaxRegionWorkflow(container)
149 .run()
150
151 console.log(result)
152 }
153
154 export const config = {
155 name: "run-once-a-day",
156 schedule: `0 0 * * *`,
157 }
158 ```
159
160 </CodeTab>
161 </CodeTabs>
162
163 Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows).
164
165 ---
166
167 ## Configure Tax Module
168
169 The Tax Module accepts options for further configurations. Refer to [this documentation](./module-options/page.mdx) for details on the module's options.
170
171 ---
172
173 <CommerceModuleSections />