Settlement Calculation
Worked numeric examples of which transactions are settled, percentage and fixed fees, VAT, in-period refund deductions, fee refunds, refund adjustment lines, carry-forward, and rounding rules.
The settlement engine is a pure function that does not depend on the DB, and it always produces the same result for the same input. All amounts are calculated as integers in the currency's minor unit; floating point is never used.
What is settled
A single batch contains transactions for only one app and one currency.
Transactions included as sale lines (SALE):
- The transaction status is one of
success,partial_refunded, orrefunded(including v1completed) - The payment confirmation time
paidAtis on or after the policy'seffectiveFromand before the end of the period (periodEnd) - The transaction is not yet locked into any batch
Transactions that were paid in an earlier period but were not settled for some reason are also included without omission, and the batch warnings (warnings) show "includes unsettled transactions from earlier periods". A transaction is locked into only one batch, so it is never settled twice.
Included as refund adjustment lines (REFUND_ADJUSTMENT): refunds on transactions already settled in a previous batch that have not yet been reflected in any batch and whose refund time is before the end of this period.
Sale lines
basePercentFee = round(gross × feeRateBps / 10000)
percentFee = basePercentFee − feeRefund (feeRefund = 0 if there are no refunds)
fixedFee = fixedFeeMinor (0 if fully refunded under a fee-refund policy)
fee = percentFee + fixedFee
vat = round(fee × vatRateBps / 10000)
net = gross − refund − fee − vatgrossis the transaction'samountMinor.refundis the sum of that transaction's refunds that occurred before the end of the period. Refunds after the end of the period are not included in this line; they become adjustment lines in the next batch.- If the fee exceeds the remaining amount and
netbecomes negative, this is shown in the batch warnings.
Refunds and fee refunds
If refundFeeRefundable = true, the percentage fee is refunded in proportion to the amount refunded.
feeRefund(cumulative refunded amount R) = round(basePercentFee × R / gross)- The fixed fee is refunded only on a full refund.
- If
refundFeeRefundable = false, the fee stays the same even when there are refunds.
Refund adjustment lines
If a transaction that has already been settled (paid out) is refunded later, the refund is deducted in the next batch as a negative line.
percentFee = −( feeRefund(previous cumulative + this refund) − feeRefund(previous cumulative) )
fixedFee = −originalFixedFee (only when this refund makes it a full refund, under a fee-refund policy)
fee = percentFee + fixedFee (negative = fee refunded to the merchant)
vat = round(fee × vatRateBps / 10000) (negative)
net = −refund − fee − vatBecause the calculation uses the difference based on the cumulative refunded amount, the total of refunded fees equals the original fee exactly, even when a payment is refunded in several parts. An adjustment line also records the number of the batch in which the transaction was originally settled (refOriginalBatchNo).
Batch totals
grossMinor = Σ line gross (adjustment line gross = 0)
refundMinor = Σ line refund
feeMinor = Σ line fee (can be negative for adjustment lines)
vatMinor = Σ line vat
adjustmentMinor = Σ adjustment line net
netMinor = Σ line net + carriedInMinorThe identity netMinor = grossMinor − refundMinor − feeMinor − vatMinor + carriedInMinor always holds.
Carry-forward
| Condition | Batch status | carryReason |
|---|---|---|
netMinor < 0 | CARRIED_FORWARD | NEGATIVE_NET |
0 ≤ netMinor < minPayoutMinor | CARRIED_FORWARD | BELOW_MIN_PAYOUT |
| Otherwise | DRAFT (awaiting verification) | — |
A carried-forward batch's netMinor is added to the carriedInMinor of the next batch created for the same app and currency (or deducted if negative), and the original batch number is recorded in the next batch's carriedFromBatchNos. Even if there are no transactions in the period, a batch is created if there is a carried-forward amount.
Rounding
roundrounds half away from zero:10.5 → 11,−10.5 → −11.- Rounding is done only per line; totals are not rounded again.
Example
Policy: KRW, feeRateBps = 350 (3.5%), fixedFeeMinor = 0, vatRateBps = 1000, refundFeeRefundable = true, minPayoutMinor = 0.
| Line | Kind | gross | refund | fee | vat | net |
|---|---|---|---|---|---|---|
| A | SALE | 12,000 | 0 | 420 | 42 | 11,538 |
| B | SALE | 5,000 | 2,000 | 105 | 11 | 2,884 |
| C | SALE | 3,300 | 3,300 | 0 | 0 | 0 |
| D | REFUND_ADJUSTMENT | 0 | 4,000 | −140 | −14 | −3,846 |
| Total | 20,300 | 9,300 | 385 | 39 | 10,576 |
Check: 20,300 − 9,300 − 385 − 39 + 0 = 10,576 = sum of line net.
When the calculation is rejected
When creating a batch, the following are treated as errors and the batch is not created (signs of data anomalies).
- Transactions in different currencies are mixed in one batch (mixing currencies is prohibited)
- A transaction's total refunds exceed the payment amount
- A transaction or refund with an amount of 0 or less
- The same transaction appears as both a sale line and an adjustment line in one batch
Settlement Policy
Settlement policy fields per app × currency (fee rate, fixed fee, VAT, fee refunds on refunds, cycle, hold days, minimum payout, timezone, start date) and the versioning and snapshot rules.
Batch Status, Verification, and Approval
The settlement batch state machine (DRAFT→VERIFIED→APPROVED→PAID, CARRIED_FORWARD, CANCELLED), three-way reconciliation, checksums, four-eyes approval, payout processing, and cancellation rules.