Skip to content
developers

Add a tip

Add a fixed or percentage tip to a sale that you run through Mobile Sync.

Prerequisites: Authentication · Run a sale

To add a tip to a sale, send the tip amount or the tip percentage, the subtotal, and the total amount with your sale request. The total amount is the subtotal plus the tax amount, if any, plus the tip amount. Our gateway uses the subtotal and any tax to calculate the tip amount, and then checks it against the total amount that you sent.

Before you begin

Note: Add the code on this page to the pay() function from Run a sale, in place of the call to makeSalePayload. The code uses the client, terminalId, and orderId values from that function, and a subtotal value, which is the amount before any tax or tip.

Integration steps

Add a fixed tip

To add a fixed tip, add the tip amount to the subtotal and any tax to calculate the total amount of the sale. When you build the payment instruction in Run a sale, set the following parameters in Payloads.sale:

ParameterValue
amountMinorUnitsThe subtotal, which is the amount before any tax or tip in minor units.
tip.fixedAmount(minorUnits:) with the tip amount in minor units.
totalAmountMinorUnitsThe subtotal, plus any tax, plus the tip amount in minor units.

The following code adds a fixed tip of $2.00 to a sale:

swift
import AppSwitchGatewayReference

let tip = 200                                                  // $2.00

let payload = Payloads.sale(
    amountMinorUnits: subtotal,
    entryMethod: .deviceReadOrManualEntry,
    dialect: client.apiType,
    terminalId: terminalId,
    currency: "USD",
    tip: .fixedAmount(minorUnits: tip),
    totalAmountMinorUnits: subtotal + tip,
    orderId: orderId
)

If the sale also includes a tax, add the tax amount to totalAmountMinorUnits. For more information, go to Add a tax.

Add a percentage tip

To add a percentage tip, use the following formulas to calculate the tip amount:

  • Percentage tip = (subtotal + tax) × tip percentage
  • Total = subtotal + tax + tip

Our gateway uses banker's rounding, also known as round half to even. If a value is exactly halfway between two minor units, our gateway rounds it to the nearest even minor unit. For example, 10.5 rounds to 10, and 11.5 rounds to 12.

For example, a $1.00 subtotal with a 10% tax and a 5% tip:

  • Percentage tip = (100 + 10) × 0.05 = 5.5, which rounds to 6 cents.
  • Total = 100 + 10 + 6 = 116 cents.

Our gateway calculates the tip amount and checks it against the total amount of the sale. If the amounts don't match, our gateway rejects the sale request.

When you build the payment instruction in Run a sale, set the following parameters in Payloads.sale:

ParameterValue
amountMinorUnitsThe subtotal, which is the amount before any tax or tip in minor units.
tip.percentage(_:) with the tip percentage, for example, 15.0.
totalAmountMinorUnitsThe subtotal, plus any tax, plus the tip amount in minor units.

The following code calculates the total amount of the sale, including a percentage tip of 15%. The sale doesn't include a tax, and passes nil for taxRate.

swift
import AppSwitchGatewayReference

/// The total to charge: the subtotal, plus the tax, plus the tip.
/// The tax is a percentage of the subtotal. A percentage tip is a percentage of the subtotal plus the tax.
func saleTotalInCents(subtotalInCents: Int, taxRate: Double?, tipPercentage: Double?) -> Int {
    var total = subtotalInCents
    if let taxRate {
        total += Int((Double(subtotalInCents) * taxRate / 100).rounded(.toNearestOrEven))
    }
    if let tipPercentage {
        total += Int((Double(total) * tipPercentage / 100).rounded(.toNearestOrEven))
    }
    return total
}

let tipPercentage = 15.0

let payload = Payloads.sale(
    amountMinorUnits: subtotal,
    entryMethod: .deviceReadOrManualEntry,
    dialect: client.apiType,
    terminalId: terminalId,
    currency: "USD",
    tip: .percentage(tipPercentage),
    totalAmountMinorUnits: saleTotalInCents(subtotalInCents: subtotal, taxRate: nil,
                                            tipPercentage: tipPercentage),
    orderId: orderId
)

Browse guides

Search documentation

API reference169
Guides131
Knowledge38
legal1
Solutions32
Workflows74
↑↓highlight↵openView all search results

Menu

Theme

Sign out

Your saved plans remain in your organization. This browser’s private draft and account view will be cleared.

Talk to an engineer