@arraypress/commission-calculator
v1.0.0
Published
Pure commission math for vendor marketplaces and affiliate programs. Supports percent and flat-rate commissions with discount and affiliate proration.
Maintainers
Readme
@arraypress/commission-calculator
Pure commission math for vendor marketplaces and affiliate programs. Supports percent and flat-rate commissions with discount/affiliate proration and refund proration via basis points. All amounts in cents.
Installation
npm install @arraypress/commission-calculatorQuick Start
import { computeCommission, computeRevenueShare, prorateRefund } from '@arraypress/commission-calculator';
// 20% commission on a $50 item with $5 discount share
const result = computeCommission({
itemGross: 5000,
discountShare: 500,
affiliateShare: 0,
commissionType: 'percent',
commissionRate: 20,
quantity: 1,
});
// { basis: 4500, commissionAmount: 900 }
// Distribute a $10 order discount across items
const discountShare = computeRevenueShare(1000, 3000, 10000);
// 300 ($3 share for a $30 item in a $100 order)
// Prorate commission on 50% refund
const proration = prorateRefund(900, 5000, 10000);
// { reduction: 450, newAmount: 450, isFullRefund: false }API
computeCommission(params)
Compute the commission for a single line item.
| Param | Type | Description |
|-------|------|-------------|
| itemGross | number | Line item gross in cents |
| discountShare | number | Proportional discount share in cents |
| affiliateShare | number | Proportional affiliate share in cents |
| commissionType | 'percent' \| 'flat' | Commission mode |
| commissionRate | number | Percentage (e.g. 20) or major currency units (e.g. 5 for $5) |
| quantity | number | Units sold |
Returns { basis, commissionAmount } — both in cents.
computeRevenueShare(orderLevelAmount, itemGross, orderSubtotal)
Compute an item's proportional share of an order-level amount. Returns cents, rounded.
prorateRefund(commissionAmount, refundAmount, orderAmount)
Prorate a commission on refund using basis points (1/10000).
Returns { reduction, newAmount, isFullRefund }.
Commission Types
Percent: basis = itemGross - discountShare - affiliateShare, then commission = basis × rate / 100. The basis ensures vendors don't earn on discounted or affiliate-attributed revenue.
Flat: commission = rate × 100 × quantity. Ignores discount/affiliate shares entirely. The basis is still set to itemGross for export transparency.
Refund Proration
Uses basis points for integer-precision partial refunds:
refundBps = round(refundAmount × 10000 / orderAmount)
reduction = round(commissionAmount × refundBps / 10000)Full refunds (refundAmount >= orderAmount) zero out the commission entirely.
License
MIT
