How to split a discount across line items
Published
A basket-level discount has to be shared across lines. If you leave that to floating-point division, a penny can go missing or appear from nowhere, and a later refund will not match what the customer paid. Valulog splits each discount in integer minor units when the order is placed, stores the split, and uses that record for refunds.
Share in proportion, then place the leftover pennies
Take a £5.00 discount (500 pence) off a basket of £20.00, £15.00 and £10.00. In proportion that is 222.222…, 166.666… and 111.111… pence. If you simply round each line, you can end up a penny over or under the £5.00 you promised.
Valulog uses Hamilton’s method on integers: give each line the whole pence of its share, then give any leftover pennies to the lines with the largest leftover fractions (and, if those are equal, to the larger line). In this basket the shares are 222, 167 and 111 pence: £2.22, £1.67 and £1.11, which add up to £5.00 exactly.
Percentage and fixed amounts use the same split
A PERCENTAGE rule is stated in basis points, not a decimal: 1000 means 10 percent. The resulting amount is then shared across eligible lines in the same way as a FIXED_AMOUNT rule, which is stated in amount_cents. You can limit a rule to some lines with applicable_items, and you can cap it with max_discount_cents.
A line never goes below zero: each share is capped at what remains on that line.
Refund from the stored split, not from a new calculation
When a line comes back, the refund should be what was paid for that line after the discount, not a fresh guess from today’s prices. Because the split was stored on the snapshot, refunding the £15.00 line in the example returns £13.33 (1500 minus 167). Valulog does not move the money: you pass refund_amount_cents to your payment provider.
Try the split with a test key
The Sandbox plan is free, needs no card, and issues test keys (vl_test_) with 1,000 operations a month. Test data never mixes with live data. Starter is £49 a month and Growth is £199 a month, including any VAT due, for business buyers. Valulog is not a payment processor and does not give tax advice.
curl https://api.valulog.com/v1/calculate \
-H "Authorization: Bearer $VALULOG_KEY" \
-H "Idempotency-Key: order-500-off" \
-H "Content-Type: application/json" \
-d '{
"currency": "GBP",
"items": [
{ "id": "line-1", "sku": "TEE", "unit_price_cents": 2000, "quantity": 1 },
{ "id": "line-2", "sku": "HAT", "unit_price_cents": 1500, "quantity": 1 },
{ "id": "line-3", "sku": "MUG", "unit_price_cents": 1000, "quantity": 1 }
],
"discounts": [
{ "rule_id": "FIVEOFF", "type": "FIXED_AMOUNT", "amount_cents": 500 }
]
}'
# → allocated_discount_cents: 222, 167, 111 (adds up to 500)