Prompt
How do I add VAT calculation to my ecommerce app?
Latest observation
To add VAT calculation to an ecommerce app, you usually need to handle 3 things:
- Determine the correct VAT rate
- Calculate VAT for prices in the cart/checkout
- Store/display tax correctly on invoices and orders
1) Decide what price basis you use
There are two common approaches:
- Prices are VAT-exclusive
Example: item price is100.00, VAT is added at checkout. - Prices are VAT-inclusive
Example: item price shown as120.00, and you back-calculate the VAT portion.
Pick one and be consistent across your app.
2) Get the correct VAT rate
VAT depends on:
- customer country
- seller country
- product type
- business customer vs consumer
- thresholds / OSS / reverse charge rules
At minimum, you need a tax rules table, for example:
DEstandard VAT:19%FRstandard VAT:20%- reduced rates for food/books/etc.
If you sell internationally, tax determination can get complex fast.
3) Calculate VAT
If prices are VAT-exclusive
VAT = net_price × VAT_rate
gross_price = net_price + VAT
Example:
- Net price:
100.00 - VAT rate:
20% - VAT:
20.00 - Gross:
120.00
If prices are VAT-inclusive
VAT = gross_price × VAT_rate / (1 + VAT_rate)
net_price = gross_price - VAT
Example:
- Gross price:
120.00 - VAT rate:
20% - VAT:
20.00 - Net:
100.00
4) Use decimal-safe math
Do not use floating-point numbers for money.
Use:
- integers in minor units (cents)
- or a decimal library
Example:
- store
1000instead of10.00if using cents
5) Sample logic
JavaScript example
function calcVatExclusive(netCents, vatRate) {
const vatCents = Math.round(netCents * vatRate);
const grossCents = netCents + vatCents;
return { vatCents, grossCents };
}
// 100.00 with 20% VAT
console.log(calcVatExclusive(10000, 0.20));
// { vatCents: 2000, grossCents: 12000 }
Inclusive price example
function calcVatInclusive(grossCents, vatRate) {
const netCents = Math.round(grossCents / (1 + vatRate));
const vatCents = grossCents - netCents;
return { netCents, vatCents };
}
console.log(calcVatInclusive(12000, 0.20));
// { netCents: 10000, vatCents: 2000 }
6) Apply VAT per line item
For checkout/invoices, calculate VAT per item or per tax rate group:
- item subtotal
- discount
- shipping
- VAT
- total
Be careful: rounding per line item vs rounding on the whole cart can cause 1-cent differences. Most systems calculate by line item and then sum.
7) Show VAT separately in checkout and invoices
Typical breakdown:
- Subtotal:
100.00 - VAT (20%):
20.00 - Shipping:
5.00 - Total:
125.00
Also store:
- tax rate
- tax amount
- tax jurisdiction/country
- whether reverse charge applied
8) Consider edge cases
- zero-rated items
- tax-exempt customers
- shipping taxability
- discounts before/after tax
- multiple tax rates in one cart
- cross-border sales
- reverse charge for B2B
- VAT registration threshold/OSS rules
9) Best practice
If you sell in multiple countries, consider using a tax engine/service like:
- Avalara
- TaxJar
- Stripe Tax
- Quaderno
They help with rules, registration, and reporting.
If you want, I can help you with:
- a database schema for VAT
- frontend/backend code in your stack
- a checkout calculation example
- EU VAT rules for a specific country setup