# Add a tip

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

**Prerequisites:** [Authentication](/api/authentication) · [Run a sale](/guides/payments/tap-to-pay-on-iphone/mobile-sync/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.

<a id="before-you-begin"></a>
## Before you begin

- Set up tips on the processing terminal. For more information, go to [Create a predefined tip on the Self-Care Portal](https://support.payroc.com/en-US/self-care-portal-transaction-management/create-a-predefined-tip-on-the-self-care-portal).

**Note:** Add the code on this page to the `pay()` function from [Run a sale](/guides/payments/tap-to-pay-on-iphone/mobile-sync/run-a-sale#step-3-switch-to-mobile-sync-and-send-the-request), 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.

<a id="integration-steps"></a>
## Integration steps

- [Add a fixed tip](#add-a-fixed-tip)
- [Add a percentage tip](#add-a-percentage-tip)

<a id="add-a-fixed-tip"></a>
## 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](/guides/payments/tap-to-pay-on-iphone/mobile-sync/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:

```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](/guides/payments/tap-to-pay-on-iphone/mobile-sync/add-a-tax).

<a id="add-a-percentage-tip"></a>
## 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](/guides/payments/tap-to-pay-on-iphone/mobile-sync/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`.

```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
)
```
