Swift
Swift
Quickbooks Send an Invoice
See more QuickBooks Examples
Demonstrates how to send an invoice using the Quickbooks REST API.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First get our previously obtained OAuth2 access token.
let jsonToken = CkoJsonObject()!
success = jsonToken.loadFile(path: "qa_data/tokens/qb-access-token.json")
let rest = CkoRest()!
// Connect to the REST server.
var bTls: Bool = true
var port: Int = 443
var bAutoReconnect: Bool = true
success = rest.connect(hostname: "sandbox-quickbooks.api.intuit.com", port: port, tls: bTls, autoReconnect: bAutoReconnect)
let sbAuth = CkoStringBuilder()!
sbAuth.append(value: "Bearer ")
sbAuth.append(value: jsonToken.string(of: "access_token"))
rest.authorization = sbAuth.getAsString()
// --------------------------------------------------------------------------
// Note: The above code to setup the initial REST connection
// can be done once. After connecting, any number of REST calls can be made.
// If the connection is lost, the next REST method call will automatically
// reconnect if needed.
// --------------------------------------------------------------------------
// Technically, the POST has an empty request body, but the Quickbooks documentation indicates that
// the Content-Type header should be set to "application/octet-stream", which really makes no sense
// because there is not content. (How can no content have a type???)
rest.addHeader(name: "Content-Type", value: "application/octet-stream")
rest.allowHeaderFolding = false
let sbResponseBody = CkoStringBuilder()!
success = rest.fullRequestNoBodySb(httpVerb: "POST", uriPath: "/v3/company/<realmID>/invoice/<invoiceId>/send?sendTo=<emailAddr>", sb: sbResponseBody)
if success != true {
print("\(rest.lastErrorText!)")
return
}
var respStatusCode: Int = rest.responseStatusCode.intValue
// Success is indicated by a 200 response status code.
print("response status code = \(respStatusCode)")
let jsonResponse = CkoJsonObject()!
jsonResponse.loadSb(sb: sbResponseBody)
jsonResponse.emitCompact = false
print("\(jsonResponse.emit()!)")
if rest.responseStatusCode.intValue != 200 {
print("Failed.")
return
}
// Sample output...
// (See the parsing code below..)
//
// Use the this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// {
// "Invoice": {
// "TxnDate": "2013-03-14",
// "domain": "QBO",
// "CurrencyRef": {
// "name": "United States Dollar",
// "value": "USD"
// },
// "ShipDate": "2013-03-01",
// "TrackingNum": "123456789",
// "ClassRef": {
// "name": "Class 1",
// "value": "200900000000000003901"
// },
// "PrintStatus": "NeedToPrint",
// "SalesTermRef": {
// "value": "4"
// },
// "DeliveryInfo": {
// "DeliveryType": "Email",
// "DeliveryTime": "2014-12-17T11:50:52-08:00"
// },
// "TotalAmt": 52.0,
// "Line": [
// {
// "Description": "Sample invoice create request",
// "DetailType": "SalesItemLineDetail",
// "SalesItemLineDetail": {
// "TaxCodeRef": {
// "value": "TAX"
// },
// "Qty": 1,
// "UnitPrice": 50,
// "ServiceDate": "2013-03-04",
// "ItemRef": {
// "name": "Hours",
// "value": "2"
// }
// },
// "LineNum": 1,
// "Amount": 50.0,
// "Id": "1"
// },
// {
// "DetailType": "SubTotalLineDetail",
// "Amount": 50.0,
// "SubTotalLineDetail": {}
// },
// {
// "DetailType": "DiscountLineDetail",
// "Amount": 5.0,
// "DiscountLineDetail": {
// "DiscountAccountRef": {
// "name": "Discounts given",
// "value": "30"
// },
// "PercentBased": true,
// "DiscountPercent": 10
// }
// },
// {
// "DetailType": "SalesItemLineDetail",
// "Amount": 2.0,
// "SalesItemLineDetail": {
// "ItemRef": {
// "value": "SHIPPING_ITEM_ID"
// }
// }
// }
// ],
// "DueDate": "2013-05-13",
// "MetaData": {
// "CreateTime": "2013-03-14T01:42:16-07:00",
// "LastUpdatedTime": "2014-12-17T11:50:58-08:00"
// },
// "DocNumber": "Sample_Inv#2",
// "PrivateNote": "Summary for sample invoice",
// "sparse": false,
// "DepositToAccountRef": {
// "name": "Undeposited Funds",
// "value": "4"
// },
// "CustomerMemo": {
// "value": "This is the customer message"
// },
// "EmailStatus": "EmailSent",
// "Deposit": 12.0,
// "Balance": 40.0,
// "CustomerRef": {
// "name": "Mr V3 Service Customer Jr2",
// "value": "15"
// },
// "TxnTaxDetail": {
// "TxnTaxCodeRef": {
// "value": "5"
// },
// "TotalTax": 5.0,
// "TaxLine": [
// {
// "DetailType": "TaxLineDetail",
// "Amount": 5.0,
// "TaxLineDetail": {
// "NetAmountTaxable": 50.0,
// "TaxPercent": 10,
// "TaxRateRef": {
// "value": "2"
// },
// "PercentBased": true
// }
// }
// ]
// },
// "SyncToken": "0",
// "BillEmail": {
// "Address": "test@intuit.com"
// },
// "ShipAddr": {
// "City": "San Jose",
// "Country": "USA",
// "Line5": "Cube 999",
// "Line4": "Dept 12",
// "Line3": "123 street",
// "Line2": "Building 1",
// "Line1": "Intuit",
// "PostalCode": "95123",
// "Lat": "37.2374847",
// "Long": "-121.8277925",
// "CountrySubDivisionCode": "CA",
// "Id": "36"
// },
// "DepartmentRef": {
// "name": "Mountain View",
// "value": "1"
// },
// "ShipMethodRef": {
// "name": "UPS",
// "value": "UPS"
// },
// "BillAddr": {
// "City": "Mountain View",
// "Country": "USA",
// "Line5": "Cube 999",
// "Line4": "Dept 12",
// "Line3": "123 street",
// "Line2": "Building 1",
// "Line1": "Google",
// "PostalCode": "95123",
// "Lat": "37.2374847",
// "Long": "-121.8277925",
// "CountrySubDivisionCode": "CA",
// "Id": "35"
// },
// "ApplyTaxAfterDiscount": false,
// "CustomField": [
// {
// "StringValue": "Custom1",
// "Type": "StringType",
// "Name": "Custom 1"
// },
// {
// "StringValue": "Custom2",
// "Type": "StringType",
// "Name": "Custom 2"
// },
// {
// "StringValue": "Custom3",
// "Type": "StringType",
// "Name": "Custom 3"
// }
// ],
// "Id": "96"
// },
// "time": "2013-03-14T13:32:04.895-07:00"
// }
//
var Description: String?
var DetailType: String?
var SalesItemLineDetailTaxCodeRefValue: String?
var SalesItemLineDetailQty: Int
var SalesItemLineDetailUnitPrice: Int
var SalesItemLineDetailServiceDate: String?
var SalesItemLineDetailItemRefName: String?
var SalesItemLineDetailItemRefValue: String?
var LineNum: Int
var Amount: String?
var Id: String?
var DiscountLineDetailDiscountAccountRefName: String?
var DiscountLineDetailDiscountAccountRefValue: String?
var DiscountLineDetailPercentBased: Bool
var DiscountLineDetailDiscountPercent: Int
var TaxLineDetailNetAmountTaxable: String?
var TaxLineDetailTaxPercent: Int
var TaxLineDetailTaxRateRefValue: String?
var TaxLineDetailPercentBased: Bool
var StringValue: String?
var Type: String?
var Name: String?
var InvoiceTxnDate: String? = jsonResponse.string(of: "Invoice.TxnDate")
var InvoiceDomain: String? = jsonResponse.string(of: "Invoice.domain")
var InvoiceCurrencyRefName: String? = jsonResponse.string(of: "Invoice.CurrencyRef.name")
var InvoiceCurrencyRefValue: String? = jsonResponse.string(of: "Invoice.CurrencyRef.value")
var InvoiceShipDate: String? = jsonResponse.string(of: "Invoice.ShipDate")
var InvoiceTrackingNum: String? = jsonResponse.string(of: "Invoice.TrackingNum")
var InvoiceClassRefName: String? = jsonResponse.string(of: "Invoice.ClassRef.name")
var InvoiceClassRefValue: String? = jsonResponse.string(of: "Invoice.ClassRef.value")
var InvoicePrintStatus: String? = jsonResponse.string(of: "Invoice.PrintStatus")
var InvoiceSalesTermRefValue: String? = jsonResponse.string(of: "Invoice.SalesTermRef.value")
var InvoiceDeliveryInfoDeliveryType: String? = jsonResponse.string(of: "Invoice.DeliveryInfo.DeliveryType")
var InvoiceDeliveryInfoDeliveryTime: String? = jsonResponse.string(of: "Invoice.DeliveryInfo.DeliveryTime")
var InvoiceTotalAmt: String? = jsonResponse.string(of: "Invoice.TotalAmt")
var InvoiceDueDate: String? = jsonResponse.string(of: "Invoice.DueDate")
var InvoiceMetaDataCreateTime: String? = jsonResponse.string(of: "Invoice.MetaData.CreateTime")
var InvoiceMetaDataLastUpdatedTime: String? = jsonResponse.string(of: "Invoice.MetaData.LastUpdatedTime")
var InvoiceDocNumber: String? = jsonResponse.string(of: "Invoice.DocNumber")
var InvoicePrivateNote: String? = jsonResponse.string(of: "Invoice.PrivateNote")
var InvoiceSparse: Bool = jsonResponse.bool(of: "Invoice.sparse")
var InvoiceDepositToAccountRefName: String? = jsonResponse.string(of: "Invoice.DepositToAccountRef.name")
var InvoiceDepositToAccountRefValue: String? = jsonResponse.string(of: "Invoice.DepositToAccountRef.value")
var InvoiceCustomerMemoValue: String? = jsonResponse.string(of: "Invoice.CustomerMemo.value")
var InvoiceEmailStatus: String? = jsonResponse.string(of: "Invoice.EmailStatus")
var InvoiceDeposit: String? = jsonResponse.string(of: "Invoice.Deposit")
var InvoiceBalance: String? = jsonResponse.string(of: "Invoice.Balance")
var InvoiceCustomerRefName: String? = jsonResponse.string(of: "Invoice.CustomerRef.name")
var InvoiceCustomerRefValue: String? = jsonResponse.string(of: "Invoice.CustomerRef.value")
var InvoiceTxnTaxDetailTxnTaxCodeRefValue: String? = jsonResponse.string(of: "Invoice.TxnTaxDetail.TxnTaxCodeRef.value")
var InvoiceTxnTaxDetailTotalTax: String? = jsonResponse.string(of: "Invoice.TxnTaxDetail.TotalTax")
var InvoiceSyncToken: String? = jsonResponse.string(of: "Invoice.SyncToken")
var InvoiceBillEmailAddress: String? = jsonResponse.string(of: "Invoice.BillEmail.Address")
var InvoiceShipAddrCity: String? = jsonResponse.string(of: "Invoice.ShipAddr.City")
var InvoiceShipAddrCountry: String? = jsonResponse.string(of: "Invoice.ShipAddr.Country")
var InvoiceShipAddrLine5: String? = jsonResponse.string(of: "Invoice.ShipAddr.Line5")
var InvoiceShipAddrLine4: String? = jsonResponse.string(of: "Invoice.ShipAddr.Line4")
var InvoiceShipAddrLine3: String? = jsonResponse.string(of: "Invoice.ShipAddr.Line3")
var InvoiceShipAddrLine2: String? = jsonResponse.string(of: "Invoice.ShipAddr.Line2")
var InvoiceShipAddrLine1: String? = jsonResponse.string(of: "Invoice.ShipAddr.Line1")
var InvoiceShipAddrPostalCode: String? = jsonResponse.string(of: "Invoice.ShipAddr.PostalCode")
var InvoiceShipAddrLat: String? = jsonResponse.string(of: "Invoice.ShipAddr.Lat")
var InvoiceShipAddrLong: String? = jsonResponse.string(of: "Invoice.ShipAddr.Long")
var InvoiceShipAddrCountrySubDivisionCode: String? = jsonResponse.string(of: "Invoice.ShipAddr.CountrySubDivisionCode")
var InvoiceShipAddrId: String? = jsonResponse.string(of: "Invoice.ShipAddr.Id")
var InvoiceDepartmentRefName: String? = jsonResponse.string(of: "Invoice.DepartmentRef.name")
var InvoiceDepartmentRefValue: String? = jsonResponse.string(of: "Invoice.DepartmentRef.value")
var InvoiceShipMethodRefName: String? = jsonResponse.string(of: "Invoice.ShipMethodRef.name")
var InvoiceShipMethodRefValue: String? = jsonResponse.string(of: "Invoice.ShipMethodRef.value")
var InvoiceBillAddrCity: String? = jsonResponse.string(of: "Invoice.BillAddr.City")
var InvoiceBillAddrCountry: String? = jsonResponse.string(of: "Invoice.BillAddr.Country")
var InvoiceBillAddrLine5: String? = jsonResponse.string(of: "Invoice.BillAddr.Line5")
var InvoiceBillAddrLine4: String? = jsonResponse.string(of: "Invoice.BillAddr.Line4")
var InvoiceBillAddrLine3: String? = jsonResponse.string(of: "Invoice.BillAddr.Line3")
var InvoiceBillAddrLine2: String? = jsonResponse.string(of: "Invoice.BillAddr.Line2")
var InvoiceBillAddrLine1: String? = jsonResponse.string(of: "Invoice.BillAddr.Line1")
var InvoiceBillAddrPostalCode: String? = jsonResponse.string(of: "Invoice.BillAddr.PostalCode")
var InvoiceBillAddrLat: String? = jsonResponse.string(of: "Invoice.BillAddr.Lat")
var InvoiceBillAddrLong: String? = jsonResponse.string(of: "Invoice.BillAddr.Long")
var InvoiceBillAddrCountrySubDivisionCode: String? = jsonResponse.string(of: "Invoice.BillAddr.CountrySubDivisionCode")
var InvoiceBillAddrId: String? = jsonResponse.string(of: "Invoice.BillAddr.Id")
var InvoiceApplyTaxAfterDiscount: Bool = jsonResponse.bool(of: "Invoice.ApplyTaxAfterDiscount")
var InvoiceId: String? = jsonResponse.string(of: "Invoice.Id")
var time: String? = jsonResponse.string(of: "time")
var i: Int = 0
var count_i: Int = jsonResponse.size(ofArray: "Invoice.Line").intValue
while i < count_i {
jsonResponse.i = i
Description = jsonResponse.string(of: "Invoice.Line[i].Description")
DetailType = jsonResponse.string(of: "Invoice.Line[i].DetailType")
SalesItemLineDetailTaxCodeRefValue = jsonResponse.string(of: "Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value")
SalesItemLineDetailQty = jsonResponse.int(of: "Invoice.Line[i].SalesItemLineDetail.Qty").intValue
SalesItemLineDetailUnitPrice = jsonResponse.int(of: "Invoice.Line[i].SalesItemLineDetail.UnitPrice").intValue
SalesItemLineDetailServiceDate = jsonResponse.string(of: "Invoice.Line[i].SalesItemLineDetail.ServiceDate")
SalesItemLineDetailItemRefName = jsonResponse.string(of: "Invoice.Line[i].SalesItemLineDetail.ItemRef.name")
SalesItemLineDetailItemRefValue = jsonResponse.string(of: "Invoice.Line[i].SalesItemLineDetail.ItemRef.value")
LineNum = jsonResponse.int(of: "Invoice.Line[i].LineNum").intValue
Amount = jsonResponse.string(of: "Invoice.Line[i].Amount")
Id = jsonResponse.string(of: "Invoice.Line[i].Id")
DiscountLineDetailDiscountAccountRefName = jsonResponse.string(of: "Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.name")
DiscountLineDetailDiscountAccountRefValue = jsonResponse.string(of: "Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.value")
DiscountLineDetailPercentBased = jsonResponse.bool(of: "Invoice.Line[i].DiscountLineDetail.PercentBased")
DiscountLineDetailDiscountPercent = jsonResponse.int(of: "Invoice.Line[i].DiscountLineDetail.DiscountPercent").intValue
i = i + 1
}
i = 0
count_i = jsonResponse.size(ofArray: "Invoice.TxnTaxDetail.TaxLine").intValue
while i < count_i {
jsonResponse.i = i
DetailType = jsonResponse.string(of: "Invoice.TxnTaxDetail.TaxLine[i].DetailType")
Amount = jsonResponse.string(of: "Invoice.TxnTaxDetail.TaxLine[i].Amount")
TaxLineDetailNetAmountTaxable = jsonResponse.string(of: "Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.NetAmountTaxable")
TaxLineDetailTaxPercent = jsonResponse.int(of: "Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxPercent").intValue
TaxLineDetailTaxRateRefValue = jsonResponse.string(of: "Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxRateRef.value")
TaxLineDetailPercentBased = jsonResponse.bool(of: "Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.PercentBased")
i = i + 1
}
i = 0
count_i = jsonResponse.size(ofArray: "Invoice.CustomField").intValue
while i < count_i {
jsonResponse.i = i
StringValue = jsonResponse.string(of: "Invoice.CustomField[i].StringValue")
Type = jsonResponse.string(of: "Invoice.CustomField[i].Type")
Name = jsonResponse.string(of: "Invoice.CustomField[i].Name")
i = i + 1
}
}