The calculation pipeline
Every line item on an invoice passes through these seven steps in order:Example 1: Usage-based pricing with tiered rates
A SaaS company charges for API calls using tiered pricing:
Customer usage: 150,000 API calls this month
Step-by-step calculation:
Example 2: Multiple adjustments in order
A customer has a plan with:- Usage-based compute pricing: $0.10 per compute hour
- 10% percentage discount
- $50 minimum charge
- $500 maximum charge
The order of adjustments matters significantly. Percentage discounts are applied before minimums, which means a discount can reduce the subtotal below the minimum, and then the minimum kicks in. This is intentional—it ensures customers always pay at least the minimum commitment.
Example 3: Prepaid credits with minimum charges
A customer has:- $500 prepaid credits
- Usage-based charges totaling $300
- $400 minimum commitment
Example 4: Multi-line item with invoice-level adjustments
A customer has two prices on their plan:- Compute: $0.10/hour, 1,000 hours used = $100
- Storage: $0.05/GB, 500 GB used = $25
- Invoice-level discount: $20 off (applies to both)
Invoice-level adjustments are distributed proportionally based on each line item’s share of the total subtotal. This ensures the adjustment is fairly allocated across all applicable prices.
Example 5: Virtual currency with conversion
A customer uses “Compute Credits” (virtual currency) with:- Conversion rate: 1 credit = $0.50 USD
- 1,000 credits prepaid
- 1,500 credits used this period
Example 6: Threshold billing (partial invoices)
Threshold billing issues an invoice when accumulated charges exceed a threshold. Here’s how subsequent invoices handle previously invoiced amounts. Setup:- Threshold: $500
- First invoice issued at $520 (charges accumulated to that point)
- End of period: total charges are $800
Example 7: Complex scenario with all components
A customer has:- Usage price: Tiered API calls ($0.01 for first 10K, $0.005 thereafter)
- Fixed fee: $100/month platform fee (in-arrears)
- Percent discount: 15% (invoice-level)
- Minimum: $200
- Prepaid credits: $150 USD
- Customer balance: $30 (from previous refund)
- Tax rate: 8%
Adjustment sign conventions
When working with the API or debugging invoice calculations, understanding delta signs is important:Debugging calculation discrepancies
If an invoice total doesn’t match expectations:- Check adjustment order: Adjustments apply in a specific sequence. A discount might reduce charges below a minimum, which then gets raised.
- Verify prepaid credit eligibility: Only in-arrears charges are eligible for prepaid credit deduction. In-advance fixed fees are not.
- Confirm currency matching: Prepaid credits must match the currency of the prices they’re applied to.
- Review threshold billing: If using threshold billing, check if amounts were already invoiced on a partial invoice.
- Inspect customer balance: Customer balance is applied after tax, which can cause confusion if you’re comparing pre-tax amounts.