# Add a tax

Add a tax 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 tax to a sale, send the tax rate, the subtotal, and the total amount with your sale request. The total amount is the subtotal plus the tax amount.

Our gateway uses the tax rate and the subtotal to calculate the tax amount, and then checks that it matches the total amount that you sent.

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

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

<a id="add-a-tax-to-a-sale"></a>
## Add a tax to a sale

In your request, send the following parameters in the `Payloads.sale` object from [Run a sale](/guides/payments/tap-to-pay-on-iphone/mobile-sync/run-a-sale):

| **Parameter** | **Value** |
| --- | --- |
| `taxRatePercentage` | The tax rate as a percentage, for example, `10.0`. |
| `amountMinorUnits` | The subtotal, which is the amount before the tax. |
| `totalAmountMinorUnits` | The subtotal plus the tax amount. |

The subtotal and total amounts are in the currency's lowest denomination, also known as minor units. For example, if the subtotal is $10.00, the value is `1000` in minor units.

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 number. For example, 10.5 rounds to 10, and 11.5 rounds to 12.

If the amounts don't match, our gateway rejects the sale request.

**Note:** If you also add a tip, calculate a percentage tip on the subtotal plus the tax. For more information, go to [Add a tip](/guides/payments/tap-to-pay-on-iphone/mobile-sync/add-a-tip).

<a id="example-code"></a>
### Example code

Add the following code 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.

The code also uses banker's rounding to round the tax amount to the nearest even minor unit.

```swift
import AppSwitchGatewayReference

let subtotal = 1000                                                            // $10.00
let taxRate = 10.0                                                             // percent
let tax = Int((Double(subtotal) * taxRate / 100).rounded(.toNearestOrEven))    // 100

let payload = Payloads.sale(
    amountMinorUnits: subtotal,                                                // the subtotal, before tax
    entryMethod: .deviceReadOrManualEntry,
    dialect: client.apiType,
    terminalId: terminalId,
    currency: "USD",
    taxRatePercentage: taxRate,
    totalAmountMinorUnits: subtotal + tax,                                     // 1100
    orderId: orderId
)
```
