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
- Set up tips on the processing terminal. For more information, go to Create a predefined tip on the Self-Care Portal.
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:
| Parameter | Value |
|---|---|
amountMinorUnits | The subtotal, which is the amount before any tax or tip in minor units. |
tip | .fixedAmount(minorUnits:) with the tip amount in minor units. |
totalAmountMinorUnits | The subtotal, plus any tax, plus the tip amount in minor units. |
The following code adds a fixed tip of $2.00 to a sale:
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:
| Parameter | Value |
|---|---|
amountMinorUnits | The subtotal, which is the amount before any tax or tip in minor units. |
tip | .percentage(_:) with the tip percentage, for example, 15.0. |
totalAmountMinorUnits | The 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.
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
)