ValulogDashboard

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.

£5 off a three-line basket (curl)
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)

Valulog is not a payment processor and does not give tax advice. Payments stay with your payment provider; Valulog records how prices, discounts and refunds were calculated.

Run this exact payload in Sandbox

£5 off a three-line basket: calculate the basket, then refund 1 unit of line-2 from the frozen snapshot. Free test key, no card.

# 1. Calculate and freeze the basket (needs jq to read the transaction_id)
TX=$(curl -s https://api.valulog.com/v1/calculate \
  -H "Authorization: Bearer $VALULOG_KEY" \
  -H "Idempotency-Key: demo-fiveoff-calc" \
  -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,
      "application_base": "REMAINING_BALANCE"
    }
  ]
}' | jq -r .transaction_id)

# 2. Refund 1 unit(s) of line-2 from the frozen snapshot
curl https://api.valulog.com/v1/refunds \
  -H "Authorization: Bearer $VALULOG_KEY" \
  -H "Idempotency-Key: demo-fiveoff-refund-1" \
  -H "Content-Type: application/json" \
  -d "{\"transaction_id\": \"$TX\", \"line_item_id\": \"line-2\", \"quantity\": 1, \"reason\": \"Customer return\"}"
Try it here first, no sign-in

The button keeps this payload in your browser and opens your dashboard (sign in with your email first if you are not already), where it is ready to run with your vl_test_ key.

Change the numbers and watch the refund

No sign-in. Edit the basket and the discount rules, then refund a line. This runs Valulog’s own pricing engine (engine 1.3.0) in your browser: nothing is sent or stored, and the IDs, hash and timestamps are generated for this simulation.

Basket (prices in pence)
LineSKUUnit priceQtyDiscountableRemove
line-1
line-2
line-3
Discount rules (applied in this order)

Result: how each discount was split

Refund part of the order

Refunds come from the frozen line, not today’s prices. Refund the same line again to see the over-refund guard; the last unit takes any leftover penny.

POST /v1/calculate · … response

…

Copies a ready-to-run curl script for this exact basket and refund, and keeps it on your dashboard. Free sandbox: no card, 1,000 operations a month.

Related