Prerequisites: Authentication · Run a sale
To provide more information about a sale, you can add individual line items. Create a CoreLineItem object for each item, and assign the collection to the CoreSale object's lineItems array.
The CoreLineItem object includes the following fields:
| Field | Description |
|---|---|
commodityCode | Commodity code of the item. |
productCode | Product code of the item. |
itemDescription | Description of the item. |
unitOfMeasure | Unit of measure. A three-letter code used in international trade. For more information about the values that you can use, go to Units of measurement. |
unitPrice | Price for each unit of measure before any taxes or discounts |
quantity | Number of units. |
discountRate | Discount percentage to apply to the item. |
taxes | The list of taxes to apply to the item. For more information, go to Add a tax to a line item. |
Important: The SDK doesn't calculate the sale amount from the line items for you. Set sale.amount to the subtotal of all your line items. The subtotal is the unit price multiplied by the quantity, minus any discount.
The following example adds a line item to a sale:
Swift
let item = CoreLineItem()
item.commodityCode = "AA"
item.productCode = "BB"
item.itemDescription = "Bread"
item.unitOfMeasure = EA
item.unitPrice = 2.00
item.quantity = 2
item.discountRate = 0
item.taxes = []
let lineItems = NSMutableArray()
lineItems.add(item)
let sale = CoreSale()
sale.amount = NSNumber(value: 4.00)
sale.transactionInputMethod = TAP
sale.lineItems = lineItems
terminal.processSale(sale)
Objective-C
CoreLineItem *item = [[CoreLineItem alloc] init];
item.commodityCode = @"AA";
item.productCode = @"BB";
item.itemDescription = @"Bread";
item.unitOfMeasure = EA;
item.unitPrice = @2.00;
item.quantity = @2;
item.discountRate = @0;
item.taxes = [NSMutableArray array];
NSMutableArray *lineItems = [NSMutableArray array];
[lineItems addObject:item];
CoreSale *sale = [[CoreSale alloc] init];
sale.amount = @4.00;
sale.transactionInputMethod = TAP;
sale.lineItems = lineItems;
[[WTPSTerminal singleton] processSale:sale];
Add a tax to a line item
To apply a tax to an individual line item, add a CoreTax to the item's taxes array. To calculate the tax amount from a percentage, use CoreUtil, and then set a global CoreTax on the sale with the total tax amount.
Swift
let tax = CoreTax()
tax.percentage = 10
let taxAmount = (CoreUtil.singleton() as! CoreUtil).calcTax(
tax.percentage,
withAmount: item.unitPrice,
with: terminal.currency
)
tax.amount = taxAmount
item.taxes = [tax]
let globalTax = CoreTax()
globalTax.amount = taxAmount
globalTax.name = "Item tax"
sale.tax = globalTax
Objective-C
CoreTax *tax = [[CoreTax alloc] init];
tax.percentage = @10;
NSNumber *taxAmount = [[CoreUtil singleton] calcTax:tax.percentage
withAmount:item.unitPrice
withCurrency:[[WTPSTerminal singleton] getCurrency]];
tax.amount = taxAmount;
item.taxes = [NSMutableArray arrayWithObject:tax];
CoreTax *globalTax = [[CoreTax alloc] init];
globalTax.amount = taxAmount;
globalTax.name = @"Item tax";
sale.tax = globalTax;
Note: If you want to apply a tax to the whole sale instead of a single line item, go to Add a tax.
The following example runs a complete sale with a line item and a tax applied to that line item:
Swift
guard terminal.getDevice() != NODEVICE else {
return
}
let item = CoreLineItem()
item.commodityCode = "AA"
item.productCode = "BB"
item.itemDescription = "Bread"
item.unitOfMeasure = EA
item.unitPrice = 2.00
item.quantity = 2
item.discountRate = 0
let tax = CoreTax()
tax.percentage = 10
tax.amount = (CoreUtil.singleton() as! CoreUtil).calcTax(
tax.percentage,
withAmount: item.unitPrice,
with: terminal.currency
)
item.taxes = [tax]
let lineItems = NSMutableArray()
lineItems.add(item)
let globalTax = CoreTax()
globalTax.amount = tax.amount
globalTax.name = "Item tax"
let sale = CoreSale()
sale.amount = 4.00
sale.transactionInputMethod = TAP
sale.orderId = "YOUR_ORDER_REFERENCE"
sale.lineItems = lineItems
sale.tax = globalTax
terminal.processSale(sale)
Objective-C
if ([[WTPSTerminal singleton] getDevice] == NODEVICE) {
return;
}
CoreLineItem *item = [[CoreLineItem alloc] init];
item.commodityCode = @"AA";
item.productCode = @"BB";
item.itemDescription = @"Bread";
item.unitOfMeasure = EA;
item.unitPrice = @2.00;
item.quantity = @2;
item.discountRate = @0;
CoreTax *tax = [[CoreTax alloc] init];
tax.percentage = @10;
tax.amount = [[CoreUtil singleton] calcTax:tax.percentage
withAmount:item.unitPrice
withCurrency:[[WTPSTerminal singleton] getCurrency]];
item.taxes = [NSMutableArray arrayWithObject:tax];
NSMutableArray *lineItems = [NSMutableArray array];
[lineItems addObject:item];
CoreTax *globalTax = [[CoreTax alloc] init];
globalTax.amount = tax.amount;
globalTax.name = @"Item tax";
CoreSale *sale = [[CoreSale alloc] init];
sale.amount = @4.00;
sale.transactionInputMethod = TAP;
sale.orderId = @"YOUR_ORDER_REFERENCE";
sale.lineItems = lineItems;
sale.tax = globalTax;
[[WTPSTerminal singleton] processSale:sale];