Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 40 ยท Pricing, Promotions, Tax

Sales Tax, VAT and GST

Understand e-commerce taxes as a developer: inclusive vs exclusive prices, nexus, destination rules, EU VAT and OSS, tax classes, rounding and reverse charge.

Tax is the part of e-commerce where the code is easy and the rules are hard. The arithmetic is a multiplication. The difficulty is knowing which rate applies, whether you must charge tax at all, how to round, and what to show on the invoice. Rates and thresholds also change often.

This lesson gives you the mental model that lets you build tax handling that is correct in structure, even if you hand the actual rates to a specialist service. It is an overview for developers, not legal or tax advice: always confirm obligations with a qualified accountant for the countries where you sell. All numbers in the examples are illustrative.

Three families of tax

  • US sales tax. Charged by state (and often county and city) at the point of sale. It is added on top of the displayed price, and the seller collects it and passes it on. There is no national sales tax.
  • VAT (value-added tax). Used across Europe, the UK and many other countries. Businesses charge VAT on sales and reclaim the VAT they paid on purchases, so only the final consumer bears it. Prices shown to consumers usually include it.
  • GST (goods and services tax). Works like VAT in Australia, New Zealand, India, Singapore, Canada and elsewhere. Some countries layer several taxes (Canada has federal GST plus provincial taxes; India splits tax between central and state portions).

As a developer you can treat all three the same way structurally: a tax is a rate chosen from a context, applied to a taxable amount, producing a tax amount that must be stored on each order line.

Inclusive versus exclusive prices

A price is either tax-exclusive (net: tax is added at checkout) or tax-inclusive (gross: the tax is already inside the displayed number). Consumer shops in VAT countries show inclusive prices; US shops show exclusive prices; B2B shops often show net prices.

When a price is inclusive, you must extract the tax from it:

TEXT
net = gross / (1 + rate)
tax = gross - net

Compute the tax as gross - net, not by multiplying again. That guarantees the two parts add up to the gross price exactly. The engine you build must support both modes, because the same shop can show inclusive prices to consumers and exclusive prices to logged-in businesses.

Python Adding and extracting tax with Decimal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the third line: a gross price of 9.99 at 19% splits into 8.39 net and 1.60 tax, because the net is rounded and the tax is the remainder. For the type used here, see Decimal and Fractions; in production you would store integer cents and apply the same rounding rule, as in Representing Money Correctly.

Rounding: per line or per invoice

The last lines of the example show why rounding is a rule you must pick on purpose. Three items of 0.99 at 7.5% produce 0.07 tax each when rounded per line (0.21 in total), but 0.22 when the tax is calculated once on the invoice total of 2.97. With three items of 1.50 at 5%, the result flips: 0.24 per line versus 0.23 per invoice.

Neither is universally right. Some jurisdictions or tax services require one method, and some allow either as long as you are consistent. Practical advice:

  • Store tax per line on the order, since refunds and returns work on single lines.
  • Use the same method everywhere: cart, checkout, invoice, refunds and accounting export.
  • Make sure the order's tax total equals the sum of its stored line taxes, so documents never disagree.
  • When prices are inclusive, tax is extracted from the line gross, and the invoice must still balance.

Which rate applies? Tax classes and location

Two inputs choose the rate.

1. The product's tax class. Most goods use the standard rate. Some categories get a reduced rate or zero rate (books, basic food, children's clothing, medicines, digital goods and others, depending on the country). A tax class (standard, reduced, zero, exempt, digital) on each product or variant lets the tax logic stay separate from the catalog. Never hard-code rates on products; point to a class and look the rate up.

2. The place where the tax is due.

  • Destination-based rules use the buyer's address. This is the norm for EU consumer sales and most US states.
  • Origin-based rules use the seller's location. A few US states use origin sourcing for sales within the state.
  • In practice you need a full shipping address (state, ZIP or postal code) before you can show a final tax figure. For digital goods, the buyer's country is established from billing data and other evidence.
flowchart TD
    A["Order line:<br/>product + address + buyer type"] --> B{"Do you have to<br/>charge tax here?"}
    B -->|"no (no nexus, export)"| Z["Tax 0, record why"]
    B -->|"yes"| C{"Business buyer in another<br/>EU country with valid VAT ID?"}
    C -->|"yes"| R["Reverse charge:<br/>tax 0, invoice note"]
    C -->|"no"| D["Tax class + destination<br/>give the rate"]
    D --> E["Compute and round<br/>per line"]

A tax decision is a short chain of questions; each outcome should be stored with a reason.

Nexus in the United States

In the US you only collect sales tax in states where you have nexus, a sufficient connection to the state. Historically this meant a physical presence such as an office, warehouse or employee. Since a 2018 Supreme Court decision (South Dakota v. Wayfair), states can also require out-of-state sellers to collect tax once they pass an economic nexus threshold, which is usually based on sales volume or transaction counts in that state. Thresholds and rules differ by state and change over time, and some items (groceries, clothing) are exempt in some states. Marketplace facilitators, such as large online marketplaces, are in many states responsible for collecting tax on their sellers' sales.

For developers, the lesson is architectural: keep a list of the places where you are registered, and make the "do we charge tax here?" decision an explicit step, as in the diagram above.

EU VAT, OSS and reverse charge

Selling to consumers across EU borders used to mean registering for VAT in every country you shipped to. The One-Stop Shop (OSS) scheme, introduced in July 2021, lets a seller register once and file a single return covering sales to consumers in other EU countries. Above an EU-wide threshold for distance sales (set at 10,000 euros), you charge the destination country's VAT rate rather than your own.

For business-to-business sales between EU countries, the supplier usually does not charge VAT. The business buyer accounts for it in its own VAT return. This is the reverse charge mechanism. To apply it, you need the buyer's VAT ID, which can be checked against the EU's VIES service, and the invoice must say that the reverse charge applies. If the check fails or the ID is missing, treat the buyer as a consumer. Exports of goods outside the EU are generally zero-rated, but the buyer may owe import VAT and customs duties locally.

A destination-based calculator

The calculator below prices one cart for five different buyers. The rates come from a small table of illustrative values. It rounds per line, handles tax classes, applies the reverse charge and treats a non-EU shipment as an export. It also assumes the seller is registered in the destination (nexus or OSS), which a real system must check first.

Python Tax by destination, class and buyer type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The same cart, with a net value of 119.80, costs 138.97 in Germany, 139.42 for a French consumer (French rates apply because of destination rules), 129.69 in Texas, and 119.80 for the French business (reverse charge) and the Norwegian customer (export). The boots and the book are taxed differently in the EU cases because their tax classes differ.

Tax services and storing results

Tax rates, product taxability rules, boundaries (which streets belong to which city district) and filing requirements change constantly. Most growing shops use a tax service or provider through an API: you send the line items, addresses and customer type, and receive a rate and amount for each line. Even then, own the structure:

  • Call the service at checkout, once the address is known; cache identical requests briefly.
  • Store the result on the order: rate, amount, jurisdiction and class per line. Never recompute historical orders from today's rules.
  • Keep a fallback table for when the service is down, and record that it was used.
  • Send refunds back through the same logic, with the same rates as the original sale.
  • Reconcile what you charged with what you report.

Do not copy rates from a blog post or an old Stack Overflow answer into production. A stale rate is a compliance problem, not a rounding error.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A tax-inclusive price of 119.00 includes 19% tax. How should the tax part be computed?

    Extract the net by dividing by (1 + rate), then take the tax as the remainder so the parts add up exactly to the gross price.
  2. 2. Which statement about rounding is the most accurate?

    Rounding each line and rounding the total can disagree. Pick the required or documented method and apply it in the cart, invoice and refunds.
  3. 3. A German shop sells to a French company that supplies a valid VAT ID. What usually happens to the VAT on the invoice?

    For B2B sales between EU countries with a valid VAT ID, the reverse charge shifts the VAT accounting to the buyer. The invoice must mention it.
  4. 4. Why should each order line store the tax rate, amount and jurisdiction it was charged?

    Tax rules change. Historical documents must stay consistent, and a refund should reverse the original tax exactly.
  5. 5. What does "nexus" mean for a US online seller?

    Nexus can come from physical presence or from economic activity above a state's threshold. It decides where you must collect tax.

Summary

  • Structurally, tax is a rate chosen from a context (product class, place, buyer type), applied to an amount, and stored per line.
  • Support both inclusive and exclusive prices; when extracting tax, compute net = gross / (1 + rate) and tax = gross - net.
  • Choose one rounding method (per line or per invoice), apply it everywhere, and keep the totals consistent with the stored lines.
  • US sales tax depends on nexus and destination; EU VAT uses destination rates for consumers (via OSS) and the reverse charge for business buyers with a valid VAT ID.
  • Use a tax service for rates, keep the decision chain explicit, store the result on the order, and treat all of this as overview knowledge, not legal advice.

Next: Currencies and Localization.