Prerequisites: Authentication · 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.
Before you begin
- Set up the tax on the processing terminal. For more information, go to Create a tax on the Self-Care Portal.
Add a tax to a sale
In your request, send the following parameters in the Payloads.sale object from 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.
Example code
Add the following code 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.
The code also uses banker's rounding to round the tax amount to the nearest even minor unit.
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
)