# Add a tax

Add a predefined tax or a custom tax to a Tap to Pay on iPhone sale using the CoreTax object.

**Prerequisites:** [Authentication](/api/authentication) · [Run a sale](/guides/payments/tap-to-pay-on-iphone/run-a-sale)

If you want to add a tax to a sale, you can [add a tax that you've predefined in the terminal](#add-a-predefined-tax), or you can [add a custom tax amount](#add-a-custom-tax).

**Note:** A sale can include only one tax at a time. If you assign a predefined tax, it replaces any other tax that you previously assigned to the sale, including a custom tax.

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

You can use the SDK to add a tax that you've already set up in the terminal. Your predefined taxes are available in `terminal.settings.taxListArray`. Each entry is a `CoreTax` with a `percentage`, `amount`, and `name` already set. You don't need to carry out any calculations, our gateway automatically adds the tax amount for you.

**Note:** `taxListArray` can be empty if there are no predefined taxes in your terminal. Check that a tax is available before you select one. To create a tax, go to [Add a tax](https://support.payroc.com/self-care-portal-transaction-management/create-a-tax-on-the-self-care-portal?from_search=238962045&swifty_search_highlight=add+a+tax).

In your app, let the merchant or the POS choose which tax to apply to the sale. The following code selects the first tax in the list:

**`Swift`**

```swift
if let taxes = terminal.settings.taxListArray, taxes.count > 0 {
    sale.tax = taxes[0] as? CoreTax
}
```

**`Objective-C`**

```objectivec
NSMutableArray *taxes = [[WTPSTerminal singleton] getSettings].taxListArray;
sale.tax = taxes.firstObject;
```

The following example runs a complete sale with a predefined tax:

**`Swift`**

```swift
guard terminal.getDevice() != NODEVICE else {
    // The reader isn't connected. Prompt the merchant to connect before you continue.
    return
}

let sale = CoreSale()
sale.amount = 42.00
sale.transactionInputMethod = TAP
sale.orderId = "YOUR_ORDER_REFERENCE"

if let taxes = terminal.settings.taxListArray, taxes.count > 0 {
    sale.tax = taxes[0] as? CoreTax
}

terminal.processSale(sale)
```

**`Objective-C`**

```objectivec
if ([[WTPSTerminal singleton] getDevice] == NODEVICE) {
    // The reader isn't connected. Prompt the merchant to connect before you continue.
    return;
}

CoreSale *sale = [[CoreSale alloc] init];
sale.amount = [NSNumber numberWithDouble:42.00];
sale.transactionInputMethod = TAP;
sale.orderId = @"YOUR_ORDER_REFERENCE";

NSMutableArray *taxes = [[WTPSTerminal singleton] getSettings].taxListArray;
sale.tax = taxes.firstObject;

[[WTPSTerminal singleton] processSale:sale];
```

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

You can use the SDK to add a custom tax that's specific to each sale. The SDK accepts only an amount for a custom tax, but you can use `CoreUtil` to calculate the tax amount from a percentage.

<a id="add-a-custom-tax-by-amount"></a>
### Add a custom tax by amount

To add a custom tax by amount and name, use `addTaxWithAmount:withTaxName:`. The following example adds a tax amount of $3.50 to a sale:

**`Swift`**

```swift
sale.addTax(withAmount: 3.50, withTaxName: "Custom tax")
```

**`Objective-C`**

```objectivec
[sale addTaxWithAmount:@3.50 withTaxName:@"Custom tax"];
```

The following example runs a complete sale with a custom tax amount:

**`Swift`**

```swift
guard terminal.getDevice() != NODEVICE else {
    // The reader isn't connected. Prompt the merchant to connect before you continue.
    return
}

let sale = CoreSale()
sale.amount = 42.00
sale.transactionInputMethod = TAP
sale.orderId = "YOUR_ORDER_REFERENCE"
sale.addTax(withAmount: 3.50, withTaxName: "Custom tax")

terminal.processSale(sale)
```

**`Objective-C`**

```objectivec
if ([[WTPSTerminal singleton] getDevice] == NODEVICE) {
    // The reader isn't connected. Prompt the merchant to connect before you continue.
    return;
}

CoreSale *sale = [[CoreSale alloc] init];
sale.amount = [NSNumber numberWithDouble:42.00];
sale.transactionInputMethod = TAP;
sale.orderId = @"YOUR_ORDER_REFERENCE";
[sale addTaxWithAmount:@3.50 withTaxName:@"Custom tax"];

[[WTPSTerminal singleton] processSale:sale];
```

<a id="add-a-custom-tax-by-percentage"></a>
### Add a custom tax by percentage

`addTaxWithAmount:withTaxName:` has no percentage parameter. To add a custom tax based on a percentage, build a `CoreTax` yourself, and then use `CoreUtil` to calculate the amount. The following example adds a tax of 8% to a sale.

**`Swift`**

```swift
let tax = CoreTax()
tax.percentage = 8
tax.name = "Sales tax"
tax.amount = (CoreUtil.singleton() as! CoreUtil).calcTax(
    tax.percentage,
    withAmount: sale.amount,
    with: terminal.currency
)

sale.tax = tax
```

**`Objective-C`**

```objectivec
CoreTax *tax = [[CoreTax alloc] init];
tax.percentage = @8;
tax.name = @"Sales tax";
tax.amount = [[CoreUtil singleton] calcTax:tax.percentage
                                withAmount:sale.amount
                              withCurrency:[[WTPSTerminal singleton] getCurrency]];

sale.tax = tax;
```

The following example runs a complete sale with a custom tax amount based on a percentage:

**`Swift`**

```swift
guard terminal.getDevice() != NODEVICE else {
    // The reader isn't connected. Prompt the merchant to connect before you continue.
    return
}

let sale = CoreSale()
sale.amount = 42.00
sale.transactionInputMethod = TAP
sale.orderId = "YOUR_ORDER_REFERENCE"

let tax = CoreTax()
tax.percentage = 8
tax.name = "Sales tax"
tax.amount = (CoreUtil.singleton() as! CoreUtil).calcTax(
    tax.percentage,
    withAmount: sale.amount,
    with: terminal.currency
)

sale.tax = tax

terminal.processSale(sale)
```

**`Objective-C`**

```objectivec
if ([[WTPSTerminal singleton] getDevice] == NODEVICE) {
    // The reader isn't connected. Prompt the merchant to connect before you continue.
    return;
}

CoreSale *sale = [[CoreSale alloc] init];
sale.amount = [NSNumber numberWithDouble:42.00];
sale.transactionInputMethod = TAP;
sale.orderId = @"YOUR_ORDER_REFERENCE";

CoreTax *tax = [[CoreTax alloc] init];
tax.percentage = @8;
tax.name = @"Sales tax";
tax.amount = [[CoreUtil singleton] calcTax:tax.percentage
                                withAmount:sale.amount
                              withCurrency:[[WTPSTerminal singleton] getCurrency]];

sale.tax = tax;

[[WTPSTerminal singleton] processSale:sale];
```
