Skip to content

arraypress/discount-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@arraypress/discount-calculator

Pure cart discount math for ecommerce. Percent and fixed discounts, per-item distribution by revenue share, and total computation. All amounts in cents (integer math, no floating point).

Installation

npm install @arraypress/discount-calculator

Quick Start

import { computeCartTotals, distributeDiscountPerItem } from '@arraypress/discount-calculator';

// Compute cart total with a 20% discount + tax
const totals = computeCartTotals(
  { subtotal: 10000 }, // $100.00
  {
    discount: { id: 1, code: 'SAVE20', type: 'percent', percent_off: 20 },
    taxAmount: 500,
    shippingAmount: 800,
  }
);
// totals.total === 9300 ($80.00 + $5.00 tax + $8.00 shipping)

// Distribute the discount across line items
const cart = {
  lineItems: [
    { unitAmount: 7000, quantity: 1, isAddon: false, discountAmount: 0 },
    { unitAmount: 3000, quantity: 1, isAddon: false, discountAmount: 0 },
  ],
};
distributeDiscountPerItem(cart, totals.discountAmount);
// lineItems[0].discountAmount === 1400 (70% share)
// lineItems[1].discountAmount === 600  (30% share)

API

computeCartTotals(cart, options?)

Compute the final cart totals. Pure function.

Parameters:

  • cart.subtotal — Cart subtotal in cents
  • options.discount — Discount object with id, code, type, and percent_off or amount_off
  • options.taxAmount — Tax in cents (default: 0)
  • options.shippingAmount — Shipping in cents (default: 0)

Returns CartTotals:

  • subtotal — Original subtotal
  • discountAmount — Computed discount
  • discountId — Discount ID or null
  • discountCode — Discount code or ''
  • taxAmount, shippingAmount — Pass-through
  • total — Final total (minimum 0)

distributeDiscountPerItem(cart, discountAmount)

Distribute a discount across line items proportionally by revenue share. Mutates each line's discountAmount in place.

  • Addon lines (isAddon: true) are excluded
  • The last eligible line absorbs any rounding remainder
  • Resets all discountAmount values to 0 before distributing

Discount Types

Percent: discountAmount = Math.round(subtotal * percent_off / 100)

Fixed: discountAmount = Math.min(amount_off, subtotal) — capped at subtotal so the total never goes negative.

License

MIT

About

Pure cart discount math for ecommerce — percent/fixed discounts, per-item distribution, total computation. All amounts in cents.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors