Dart
Dart
Quickbooks Create an Invoice
See more QuickBooks Examples
Demonstrates how to create an invoice via the Quickbooks REST API.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// 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.
final jsonToken = CkJsonObject();
jsonToken.loadFile('qa_data/tokens/qb-access-token.json');
final rest = CkRest();
// Connect to the REST server.
final bTls = true;
final port = 443;
final bAutoReconnect = true;
rest.connect('sandbox-quickbooks.api.intuit.com', port, bTls, bAutoReconnect);
final sbAuth = CkStringBuilder();
sbAuth.append('Bearer ');
sbAuth.append(jsonToken.stringOf('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.
// --------------------------------------------------------------------------
// Create the following JSON:
// {
// "Line": [
// {
// "DetailType": "SalesItemLineDetail",
// "Amount": 100.0,
// "SalesItemLineDetail": {
// "ItemRef": {
// "name": "Services",
// "value": "1"
// }
// }
// }
// ],
// "CustomerRef": {
// "value": "1"
// }
// }
//
// Use the this online tool to generate the code from sample JSON:
// Generate Code to Create JSON
final jsonReq = CkJsonObject();
jsonReq.updateString('Line[0].DetailType', 'SalesItemLineDetail');
jsonReq.updateNumber('Line[0].Amount', '100.0');
jsonReq.updateString('Line[0].SalesItemLineDetail.ItemRef.name', 'Services');
jsonReq.updateString('Line[0].SalesItemLineDetail.ItemRef.value', '1');
jsonReq.updateString('CustomerRef.value', '1');
final sbRequestBody = CkStringBuilder();
jsonReq.emitSb(sbRequestBody);
rest.addHeader('Content-Type', 'application/json');
rest.addHeader('Accept', 'application/json');
rest.allowHeaderFolding = false;
final sbResponseBody = CkStringBuilder();
try {
rest.fullRequestSb('POST', '/v3/company/<realmID>/invoice', sbRequestBody, sbResponseBody);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final respStatusCode = rest.responseStatusCode;
// Success is indicated by a 200 response status code.
print('response status code = $respStatusCode');
final jsonResponse = CkJsonObject();
jsonResponse.loadSb(sbResponseBody);
jsonResponse.emitCompact = false;
print(jsonResponse.emit());
if (rest.responseStatusCode != 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": {
// "DocNumber": "1069",
// "SyncToken": "0",
// "domain": "QBO",
// "Balance": 100.0,
// "BillAddr": {
// "City": "Bayshore",
// "Line1": "4581 Finch St.",
// "PostalCode": "94326",
// "Lat": "INVALID",
// "Long": "INVALID",
// "CountrySubDivisionCode": "CA",
// "Id": "2"
// },
// "TxnDate": "2015-07-24",
// "TotalAmt": 100.0,
// "CustomerRef": {
// "name": "Amy's Bird Sanctuary",
// "value": "1"
// },
// "ShipAddr": {
// "City": "Bayshore",
// "Line1": "4581 Finch St.",
// "PostalCode": "94326",
// "Lat": "INVALID",
// "Long": "INVALID",
// "CountrySubDivisionCode": "CA",
// "Id": "109"
// },
// "LinkedTxn": [
// ],
// "DueDate": "2015-08-23",
// "PrintStatus": "NeedToPrint",
// "Deposit": 0,
// "sparse": false,
// "EmailStatus": "NotSet",
// "Line": [
// {
// "LineNum": 1,
// "Amount": 100.0,
// "SalesItemLineDetail": {
// "TaxCodeRef": {
// "value": "NON"
// },
// "ItemRef": {
// "name": "Services",
// "value": "1"
// }
// },
// "Id": "1",
// "DetailType": "SalesItemLineDetail"
// },
// {
// "DetailType": "SubTotalLineDetail",
// "Amount": 100.0,
// "SubTotalLineDetail": {}
// }
// ],
// "ApplyTaxAfterDiscount": false,
// "CustomField": [
// {
// "DefinitionId": "1",
// "Type": "StringType",
// "Name": "Crew #"
// }
// ],
// "Id": "238",
// "TxnTaxDetail": {
// "TotalTax": 0
// },
// "MetaData": {
// "CreateTime": "2015-07-24T10:33:39-07:00",
// "LastUpdatedTime": "2015-07-24T10:33:39-07:00"
// }
// },
// "time": "2015-07-24T10:33:39.11-07:00"
// }
//
var lineNum = 0;
var amount = '';
var salesItemLineDetailTaxCodeRefValue = '';
var salesItemLineDetailItemRefName = '';
var salesItemLineDetailItemRefValue = '';
var id = '';
var detailType = '';
var definitionId = '';
var invType = '';
var name = '';
final invoiceDocNumber = jsonResponse.stringOf('Invoice.DocNumber');
final invoiceSyncToken = jsonResponse.stringOf('Invoice.SyncToken');
final invoiceDomain = jsonResponse.stringOf('Invoice.domain');
final invoiceBalance = jsonResponse.stringOf('Invoice.Balance');
final invoiceBillAddrCity = jsonResponse.stringOf('Invoice.BillAddr.City');
final invoiceBillAddrLine1 = jsonResponse.stringOf('Invoice.BillAddr.Line1');
final invoiceBillAddrPostalCode = jsonResponse.stringOf('Invoice.BillAddr.PostalCode');
final invoiceBillAddrLat = jsonResponse.stringOf('Invoice.BillAddr.Lat');
final invoiceBillAddrLong = jsonResponse.stringOf('Invoice.BillAddr.Long');
final invoiceBillAddrCountrySubDivisionCode = jsonResponse.stringOf('Invoice.BillAddr.CountrySubDivisionCode');
final invoiceBillAddrId = jsonResponse.stringOf('Invoice.BillAddr.Id');
final invoiceTxnDate = jsonResponse.stringOf('Invoice.TxnDate');
final invoiceTotalAmt = jsonResponse.stringOf('Invoice.TotalAmt');
final invoiceCustomerRefName = jsonResponse.stringOf('Invoice.CustomerRef.name');
final invoiceCustomerRefValue = jsonResponse.stringOf('Invoice.CustomerRef.value');
final invoiceShipAddrCity = jsonResponse.stringOf('Invoice.ShipAddr.City');
final invoiceShipAddrLine1 = jsonResponse.stringOf('Invoice.ShipAddr.Line1');
final invoiceShipAddrPostalCode = jsonResponse.stringOf('Invoice.ShipAddr.PostalCode');
final invoiceShipAddrLat = jsonResponse.stringOf('Invoice.ShipAddr.Lat');
final invoiceShipAddrLong = jsonResponse.stringOf('Invoice.ShipAddr.Long');
final invoiceShipAddrCountrySubDivisionCode = jsonResponse.stringOf('Invoice.ShipAddr.CountrySubDivisionCode');
final invoiceShipAddrId = jsonResponse.stringOf('Invoice.ShipAddr.Id');
final invoiceDueDate = jsonResponse.stringOf('Invoice.DueDate');
final invoicePrintStatus = jsonResponse.stringOf('Invoice.PrintStatus');
final invoiceDeposit = jsonResponse.intOf('Invoice.Deposit');
final invoiceEmailStatus = jsonResponse.stringOf('Invoice.EmailStatus');
final invoiceId = jsonResponse.stringOf('Invoice.Id');
final invoiceTxnTaxDetailTotalTax = jsonResponse.intOf('Invoice.TxnTaxDetail.TotalTax');
final invoiceMetaDataCreateTime = jsonResponse.stringOf('Invoice.MetaData.CreateTime');
final invoiceMetaDataLastUpdatedTime = jsonResponse.stringOf('Invoice.MetaData.LastUpdatedTime');
final time = jsonResponse.stringOf('time');
var i = 0;
var countI = jsonResponse.sizeOfArray('Invoice.LinkedTxn');
while (i < countI) {
jsonResponse.i = i;
i++;
}
i = 0;
countI = jsonResponse.sizeOfArray('Invoice.Line');
while (i < countI) {
jsonResponse.i = i;
lineNum = jsonResponse.intOf('Invoice.Line[i].LineNum');
amount = jsonResponse.stringOf('Invoice.Line[i].Amount');
salesItemLineDetailTaxCodeRefValue = jsonResponse.stringOf('Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value');
salesItemLineDetailItemRefName = jsonResponse.stringOf('Invoice.Line[i].SalesItemLineDetail.ItemRef.name');
salesItemLineDetailItemRefValue = jsonResponse.stringOf('Invoice.Line[i].SalesItemLineDetail.ItemRef.value');
id = jsonResponse.stringOf('Invoice.Line[i].Id');
detailType = jsonResponse.stringOf('Invoice.Line[i].DetailType');
i++;
}
i = 0;
countI = jsonResponse.sizeOfArray('Invoice.CustomField');
while (i < countI) {
jsonResponse.i = i;
definitionId = jsonResponse.stringOf('Invoice.CustomField[i].DefinitionId');
invType = jsonResponse.stringOf('Invoice.CustomField[i].Type');
name = jsonResponse.stringOf('Invoice.CustomField[i].Name');
i++;
}
}