Zig
Zig
Quickbooks Update an Invoice
See more QuickBooks Examples
Demonstrates how to update an invoice using the Quickbooks REST API.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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.
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
try json_token.loadFile("qa_data/tokens/qb-access-token.json");
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect to the REST server.
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
try rest.connect("sandbox-quickbooks.api.intuit.com", port, b_tls, b_auto_reconnect);
const sb_auth = try chilkat.StringBuilder.init();
defer sb_auth.deinit();
sb_auth.append("Bearer ") catch {};
sb_auth.append(try json_token.stringOf(alloc, "access_token")) catch {};
rest.setAuthorization(try sb_auth.getAsString(alloc));
// --------------------------------------------------------------------------
// 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:
// {
// "DocNumber": "1070",
// "SyncToken": "0",
// "domain": "QBO",
// "Balance": 150.0,
// "BillAddr": {
// "City": "Bayshore",
// "Line1": "4581 Finch St.",
// "PostalCode": "94326",
// "Lat": "INVALID",
// "Long": "INVALID",
// "CountrySubDivisionCode": "CA",
// "Id": "2"
// },
// "TxnDate": "2015-07-24",
// "TotalAmt": 150.0,
// "CustomerRef": {
// "name": "Amy's Bird Sanctuary",
// "value": "1"
// },
// "CustomerMemo": {
// "value": "Added customer memo."
// },
// "ShipAddr": {
// "City": "Bayshore",
// "Line1": "4581 Finch St.",
// "PostalCode": "94326",
// "Lat": "INVALID",
// "Long": "INVALID",
// "CountrySubDivisionCode": "CA",
// "Id": "109"
// },
// "LinkedTxn": [
// ],
// "DueDate": "2015-08-23",
// "PrintStatus": "NeedToPrint",
// "EmailStatus": "NotSet",
// "sparse": false,
// "Line": [
// {
// "LineNum": 1,
// "Amount": 150.0,
// "SalesItemLineDetail": {
// "TaxCodeRef": {
// "value": "NON"
// },
// "ItemRef": {
// "name": "Services",
// "value": "1"
// }
// },
// "Id": "1",
// "DetailType": "SalesItemLineDetail"
// },
// {
// "DetailType": "SubTotalLineDetail",
// "Amount": 150.0,
// "SubTotalLineDetail": {}
// }
// ],
// "ApplyTaxAfterDiscount": false,
// "CustomField": [
// {
// "DefinitionId": "1",
// "Type": "StringType",
// "Name": "Crew #"
// }
// ],
// "Id": "239",
// "TxnTaxDetail": {
// "TotalTax": 0
// },
// "MetaData": {
// "CreateTime": "2015-07-24T10:35:08-07:00",
// "LastUpdatedTime": "2015-07-24T10:35:08-07:00"
// }
// }
//
// Use the this online tool to generate the code from sample JSON:
// Generate Code to Create JSON
const json_req = try chilkat.JsonObject.init();
defer json_req.deinit();
json_req.updateString("DocNumber", "1070") catch {};
json_req.updateString("SyncToken", "0") catch {};
json_req.updateString("domain", "QBO") catch {};
json_req.updateNumber("Balance", "150.0") catch {};
json_req.updateString("BillAddr.City", "Bayshore") catch {};
json_req.updateString("BillAddr.Line1", "4581 Finch St.") catch {};
json_req.updateString("BillAddr.PostalCode", "94326") catch {};
json_req.updateString("BillAddr.Lat", "INVALID") catch {};
json_req.updateString("BillAddr.Long", "INVALID") catch {};
json_req.updateString("BillAddr.CountrySubDivisionCode", "CA") catch {};
json_req.updateString("BillAddr.Id", "2") catch {};
json_req.updateString("TxnDate", "2015-07-24") catch {};
json_req.updateNumber("TotalAmt", "150.0") catch {};
json_req.updateString("CustomerRef.name", "Amy's Bird Sanctuary") catch {};
json_req.updateString("CustomerRef.value", "1") catch {};
json_req.updateString("CustomerMemo.value", "Added customer memo.") catch {};
json_req.updateString("ShipAddr.City", "Bayshore") catch {};
json_req.updateString("ShipAddr.Line1", "4581 Finch St.") catch {};
json_req.updateString("ShipAddr.PostalCode", "94326") catch {};
json_req.updateString("ShipAddr.Lat", "INVALID") catch {};
json_req.updateString("ShipAddr.Long", "INVALID") catch {};
json_req.updateString("ShipAddr.CountrySubDivisionCode", "CA") catch {};
json_req.updateString("ShipAddr.Id", "109") catch {};
json_req.updateNewArray("LinkedTxn") catch {};
json_req.updateString("DueDate", "2015-08-23") catch {};
json_req.updateString("PrintStatus", "NeedToPrint") catch {};
json_req.updateString("EmailStatus", "NotSet") catch {};
json_req.updateBool("sparse", false) catch {};
json_req.updateInt("Line[0].LineNum", 1) catch {};
json_req.updateNumber("Line[0].Amount", "150.0") catch {};
json_req.updateString("Line[0].SalesItemLineDetail.TaxCodeRef.value", "NON") catch {};
json_req.updateString("Line[0].SalesItemLineDetail.ItemRef.name", "Services") catch {};
json_req.updateString("Line[0].SalesItemLineDetail.ItemRef.value", "1") catch {};
json_req.updateString("Line[0].Id", "1") catch {};
json_req.updateString("Line[0].DetailType", "SalesItemLineDetail") catch {};
json_req.updateString("Line[1].DetailType", "SubTotalLineDetail") catch {};
json_req.updateNumber("Line[1].Amount", "150.0") catch {};
json_req.updateNewObject("Line[1].SubTotalLineDetail") catch {};
json_req.updateBool("ApplyTaxAfterDiscount", false) catch {};
json_req.updateString("CustomField[0].DefinitionId", "1") catch {};
json_req.updateString("CustomField[0].Type", "StringType") catch {};
json_req.updateString("CustomField[0].Name", "Crew #") catch {};
json_req.updateString("Id", "239") catch {};
json_req.updateInt("TxnTaxDetail.TotalTax", 0) catch {};
json_req.updateString("MetaData.CreateTime", "2015-07-24T10:35:08-07:00") catch {};
json_req.updateString("MetaData.LastUpdatedTime", "2015-07-24T10:35:08-07:00") catch {};
const sb_request_body = try chilkat.StringBuilder.init();
defer sb_request_body.deinit();
json_req.emitSb(sb_request_body) catch {};
rest.addHeader("Content-Type", "application/json") catch {};
rest.addHeader("Accept", "application/json") catch {};
rest.setAllowHeaderFolding(false);
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
rest.fullRequestSb("POST", "/v3/company/<realmID>/invoice", sb_request_body, sb_response_body) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
const resp_status_code = rest.getResponseStatusCode();
// Success is indicated by a 200 response status code.
std.debug.print("response status code = {d}\n", .{resp_status_code});
const json_response = try chilkat.JsonObject.init();
defer json_response.deinit();
json_response.loadSb(sb_response_body) catch {};
json_response.setEmitCompact(false);
std.debug.print("{s}\n", .{try json_response.emit(alloc)});
if (rest.getResponseStatusCode() != 200) {
std.debug.print("Failed.\n", .{});
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": "2015-07-24",
// "domain": "QBO",
// "PrintStatus": "NeedToPrint",
// "TotalAmt": 150.0,
// "Line": [
// {
// "LineNum": 1,
// "Amount": 150.0,
// "SalesItemLineDetail": {
// "TaxCodeRef": {
// "value": "NON"
// },
// "ItemRef": {
// "name": "Services",
// "value": "1"
// }
// },
// "Id": "1",
// "DetailType": "SalesItemLineDetail"
// },
// {
// "DetailType": "SubTotalLineDetail",
// "Amount": 150.0,
// "SubTotalLineDetail": {}
// }
// ],
// "DueDate": "2015-08-23",
// "ApplyTaxAfterDiscount": false,
// "DocNumber": "1070",
// "sparse": false,
// "CustomerMemo": {
// "value": "Added customer memo."
// },
// "Deposit": 0,
// "Balance": 150.0,
// "CustomerRef": {
// "name": "Amy's Bird Sanctuary",
// "value": "1"
// },
// "TxnTaxDetail": {
// "TotalTax": 0
// },
// "SyncToken": "1",
// "LinkedTxn": [
// ],
// "ShipAddr": {
// "CountrySubDivisionCode": "CA",
// "City": "Bayshore",
// "PostalCode": "94326",
// "Id": "118",
// "Line1": "4581 Finch St."
// },
// "EmailStatus": "NotSet",
// "BillAddr": {
// "CountrySubDivisionCode": "CA",
// "City": "Bayshore",
// "PostalCode": "94326",
// "Id": "117",
// "Line1": "4581 Finch St."
// },
// "MetaData": {
// "CreateTime": "2015-07-24T10:35:08-07:00",
// "LastUpdatedTime": "2015-07-24T10:53:39-07:00"
// },
// "CustomField": [
// {
// "DefinitionId": "1",
// "Type": "StringType",
// "Name": "Crew #"
// }
// ],
// "Id": "239"
// },
// "time": "2015-07-24T10:53:39.287-07:00"
// }
//
var line_num: i32 = 0;
var amount: [:0]const u8 = "";
var sales_item_line_detail_tax_code_ref_value: [:0]const u8 = "";
var sales_item_line_detail_item_ref_name: [:0]const u8 = "";
var sales_item_line_detail_item_ref_value: [:0]const u8 = "";
var id: [:0]const u8 = "";
var detail_type: [:0]const u8 = "";
var definition_id: [:0]const u8 = "";
var inv_type: [:0]const u8 = "";
var name: [:0]const u8 = "";
_ = try json_response.stringOf(alloc, "Invoice.TxnDate");
_ = try json_response.stringOf(alloc, "Invoice.domain");
_ = try json_response.stringOf(alloc, "Invoice.PrintStatus");
_ = try json_response.stringOf(alloc, "Invoice.TotalAmt");
_ = try json_response.stringOf(alloc, "Invoice.DueDate");
_ = json_response.boolOf("Invoice.ApplyTaxAfterDiscount");
_ = try json_response.stringOf(alloc, "Invoice.DocNumber");
_ = json_response.boolOf("Invoice.sparse");
_ = try json_response.stringOf(alloc, "Invoice.CustomerMemo.value");
_ = json_response.intOf("Invoice.Deposit");
_ = try json_response.stringOf(alloc, "Invoice.Balance");
_ = try json_response.stringOf(alloc, "Invoice.CustomerRef.name");
_ = try json_response.stringOf(alloc, "Invoice.CustomerRef.value");
_ = json_response.intOf("Invoice.TxnTaxDetail.TotalTax");
_ = try json_response.stringOf(alloc, "Invoice.SyncToken");
_ = try json_response.stringOf(alloc, "Invoice.ShipAddr.CountrySubDivisionCode");
_ = try json_response.stringOf(alloc, "Invoice.ShipAddr.City");
_ = try json_response.stringOf(alloc, "Invoice.ShipAddr.PostalCode");
_ = try json_response.stringOf(alloc, "Invoice.ShipAddr.Id");
_ = try json_response.stringOf(alloc, "Invoice.ShipAddr.Line1");
_ = try json_response.stringOf(alloc, "Invoice.EmailStatus");
_ = try json_response.stringOf(alloc, "Invoice.BillAddr.CountrySubDivisionCode");
_ = try json_response.stringOf(alloc, "Invoice.BillAddr.City");
_ = try json_response.stringOf(alloc, "Invoice.BillAddr.PostalCode");
_ = try json_response.stringOf(alloc, "Invoice.BillAddr.Id");
_ = try json_response.stringOf(alloc, "Invoice.BillAddr.Line1");
_ = try json_response.stringOf(alloc, "Invoice.MetaData.CreateTime");
_ = try json_response.stringOf(alloc, "Invoice.MetaData.LastUpdatedTime");
_ = try json_response.stringOf(alloc, "Invoice.Id");
_ = try json_response.stringOf(alloc, "time");
var i: i32 = 0;
var count_i: i32 = json_response.sizeOfArray("Invoice.Line");
while (i < count_i) {
json_response.setI(i);
line_num = json_response.intOf("Invoice.Line[i].LineNum");
amount = try json_response.stringOf(alloc, "Invoice.Line[i].Amount");
sales_item_line_detail_tax_code_ref_value = try json_response.stringOf(alloc, "Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value");
sales_item_line_detail_item_ref_name = try json_response.stringOf(alloc, "Invoice.Line[i].SalesItemLineDetail.ItemRef.name");
sales_item_line_detail_item_ref_value = try json_response.stringOf(alloc, "Invoice.Line[i].SalesItemLineDetail.ItemRef.value");
id = try json_response.stringOf(alloc, "Invoice.Line[i].Id");
detail_type = try json_response.stringOf(alloc, "Invoice.Line[i].DetailType");
i = i + 1;
}
i = 0;
count_i = json_response.sizeOfArray("Invoice.LinkedTxn");
while (i < count_i) {
json_response.setI(i);
i = i + 1;
}
i = 0;
count_i = json_response.sizeOfArray("Invoice.CustomField");
while (i < count_i) {
json_response.setI(i);
definition_id = try json_response.stringOf(alloc, "Invoice.CustomField[i].DefinitionId");
inv_type = try json_response.stringOf(alloc, "Invoice.CustomField[i].Type");
name = try json_response.stringOf(alloc, "Invoice.CustomField[i].Name");
i = i + 1;
}
}