Skip to Content
StagePayDocs v2

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, or refunded (including v1 completed)
  • The payment confirmation time paidAt is on or after the policy's effectiveFrom and 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 − vat
  • gross is the transaction's amountMinor.
  • refund is 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 net becomes 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 − vat

Because 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 + carriedInMinor

The identity netMinor = grossMinor − refundMinor − feeMinor − vatMinor + carriedInMinor always holds.

Carry-forward

ConditionBatch statuscarryReason
netMinor < 0CARRIED_FORWARDNEGATIVE_NET
0 ≤ netMinor < minPayoutMinorCARRIED_FORWARDBELOW_MIN_PAYOUT
OtherwiseDRAFT (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

  • round rounds 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.

LineKindgrossrefundfeevatnet
ASALE12,00004204211,538
BSALE5,0002,000105112,884
CSALE3,3003,300000
DREFUND_ADJUSTMENT04,000−140−14−3,846
Total20,3009,3003853910,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

On this page