Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Hungary NAV Manage Invoice Request

See more Hungary NAV Invoicing Examples

Demonstrates the manageInvoice request for the Hungarian NAV Online Invoicing System REST API v2.0.

Chilkat Rust Downloads

Rust

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// Build the following XML:

// Use this online tool to generate code from sample XML: 
// Generate Code to Create XML

// <?xml version="1.0" encoding="UTF-8"?>
// <ManageInvoiceRequest xmlns="http://schemas.nav.gov.hu/OSA/2.0/api">
// 	<header>
// 		<requestId>RID181837288942</requestId>
// 		<timestamp>2019-09-11T12:44:55.442Z</timestamp>
// 		<requestVersion>2.0</requestVersion>
// 		<headerVersion>1.0</headerVersion>
// 	</header>
// 	<user>
// 		<login>lwilsmn0uqdxe6u</login>
// 		<passwordHash>2F43840A882CFDB7DB0FEC07D419D030D864B47B6B541DC280EF81B937B7A176E33C052B0D26638CC18A7A2C08D8D311733078A774BF43F6CA57FE8CD74DC28E</passwordHash>
// 		<taxNumber>11111111</taxNumber>
// 		<requestSignature>E4D191A48EE8828A1E84C1F841A2B4E1699ECB49C4CDA1DC7A057765FD935872219644CC2B5A93B8A344404E4FD8ECA4902B5DBCF993E768DC558B0F0281E775</requestSignature>
// 	</user>
// 	<software>
// 		<softwareId>123456789123456789</softwareId>
// 		<softwareName>string</softwareName>
// 		<softwareOperation>LOCAL_SOFTWARE</softwareOperation>
// 		<softwareMainVersion>string</softwareMainVersion>
// 		<softwareDevName>string</softwareDevName>
// 		<softwareDevContact>string</softwareDevContact>
// 		<softwareDevCountryCode>HU</softwareDevCountryCode>
// 		<softwareDevTaxNumber>string</softwareDevTaxNumber>
// 	</software>
// 	<exchangeToken>b1aca173-d9e8-4561-9237-0511eed99eaa2P0ZHLXBRI2U</exchangeToken>
// 	<invoiceOperations>
// 		<compressedContent>false</compressedContent>
// 		<invoiceOperation>
// 			<index>1</index>
// 			<invoiceOperation>CREATE</invoiceOperation>
// 			<invoiceData>base64 contents of invoiceData1 XML file</invoiceData>
// 		</invoiceOperation>
// 		<invoiceOperation>
// 			<index>2</index>
// 			<invoiceOperation>CREATE</invoiceOperation>
// 			<invoiceData>base64 contents of invoiceData2 XML file</invoiceData>
// 		</invoiceOperation>
// 		<invoiceOperation>
// 			<index>3</index>
// 			<invoiceOperation>CREATE</invoiceOperation>
// 			<invoiceData>base64 contents of invoiceData3 XML file</invoiceData>
// 		</invoiceOperation>
// 	</invoiceOperations>
// </ManageInvoiceRequest>
// 

// First load the invoiceData for the 3 XML invoice files we'll be sending.
let bd_invoice_data1 = chilkat::BinData::new();
let bd_invoice_data2 = chilkat::BinData::new();
let bd_invoice_data3 = chilkat::BinData::new();
let _ = bd_invoice_data1.load_file("qa_data/nav_invoicing/invoiceData1.xml").is_ok();
let _ = bd_invoice_data2.load_file("qa_data/nav_invoicing/invoiceData2.xml").is_ok();
if bd_invoice_data3.load_file("qa_data/nav_invoicing/invoiceData3.xml").is_err() {
    println!("Failed to load invoice data.");
    return;
}

let crypt = chilkat::Crypt2::new();

let dt_now = chilkat::DateTime::new();
let _ = dt_now.set_from_current_system_time();
println!("{}", dt_now.get_as_timestamp(false).unwrap_or_default());

// The hash algorithm for the password is SHA512 (not SHA3-512).
crypt.set_hash_algorithm("sha512");
crypt.set_encoding_mode("hex");
let my_password = "my-password".to_string();
let password_hash = crypt.hash_string_enc(&my_password).unwrap_or_default();

// Generate a random request ID like "RID215118906689"
let prng = chilkat::Prng::new();
let sb_request_id = chilkat::StringBuilder::new();
let _ = sb_request_id.append("RID");
let _ = sb_request_id.append(&prng.random_string(12, true, false, false).unwrap_or_default());
println!("generated requestId = {}", sb_request_id.get_as_string().unwrap_or_default());

// Calculate the requestSignature
crypt.set_hash_algorithm("sha3-512");
let signature_key = "ce-8f5e-215119fa7dd621DLMRHRLH2S".to_string();

let sb_final_hash_base = chilkat::StringBuilder::new();

// First append the timestamp because we are going to remove certain chars/parts.
let _ = sb_final_hash_base.append(&dt_now.get_as_timestamp(false).unwrap_or_default());
let mut num_replaced = sb_final_hash_base.replace("Z", "");
num_replaced = sb_final_hash_base.replace("-", "");
num_replaced = sb_final_hash_base.replace(":", "");
num_replaced = sb_final_hash_base.replace("T", "");

// Prepend the requestId and append the signatureKey
let _ = sb_final_hash_base.prepend(&sb_request_id.get_as_string().unwrap_or_default());
let _ = sb_final_hash_base.append(&signature_key);

// Calculate each index hash and add it to the sbFinalHashBase
let sb_hash_base = chilkat::StringBuilder::new();
let _ = sb_hash_base.append("CREATE");
let _ = sb_hash_base.append(&bd_invoice_data1.get_encoded("base64").unwrap_or_default());
let mut index_hash = crypt.hash_string_enc(&sb_hash_base.get_as_string().unwrap_or_default()).unwrap_or_default();
let _ = sb_final_hash_base.append(&index_hash);

sb_hash_base.clear();
let _ = sb_hash_base.append("CREATE");
let _ = sb_hash_base.append(&bd_invoice_data2.get_encoded("base64").unwrap_or_default());
index_hash = crypt.hash_string_enc(&sb_hash_base.get_as_string().unwrap_or_default()).unwrap_or_default();
let _ = sb_final_hash_base.append(&index_hash);

sb_hash_base.clear();
let _ = sb_hash_base.append("CREATE");
let _ = sb_hash_base.append(&bd_invoice_data3.get_encoded("base64").unwrap_or_default());
index_hash = crypt.hash_string_enc(&sb_hash_base.get_as_string().unwrap_or_default()).unwrap_or_default();
let _ = sb_final_hash_base.append(&index_hash);

// Get our request signature (using sha3-512 because our HashAlgorithm was set to "sha3-512" up above...)
let request_signature = crypt.hash_string_enc(&sb_final_hash_base.get_as_string().unwrap_or_default()).unwrap_or_default();

// Load our recently obtained exchange token.
// See Hungary NAV Invoicing Token Exchange Sample Code
let xml_exchange_token = chilkat::Xml::new();
if xml_exchange_token.load_xml_file("qa_data/tokens/nav_exchange_token.xml").is_err() {
    println!("{}", xml_exchange_token.last_error_text());
    return;
}

// Get the base64 encoded/encrypted exchange token.
// IMPORTANT:  Make sure to use the exchange token before it expires.
// If it expired, then get a new one..
let encoded_encrypted_exchange_token = xml_exchange_token.get_child_content("encodedExchangeToken").unwrap_or_default();

// Decode to binary.
let bd_exchange_token = chilkat::BinData::new();
let _ = bd_exchange_token.append_encoded(&encoded_encrypted_exchange_token, "base64");

// Decrypt using your 16-digit replacement key:
crypt.set_crypt_algorithm("aes");
crypt.set_cipher_mode("ecb");
crypt.set_key_length(128);
crypt.set_encoding_mode("base64");
// Pass your 16-digit replacement key here:
crypt.set_encoded_key("99952BBAAAAA8XYZ", "ascii");
let _ = crypt.decrypt_bd(&bd_exchange_token);
let exchange_token = bd_exchange_token.get_string("utf-8").unwrap_or_default();
println!("exchange token = {}", exchange_token);

// Now build the XML..
let xml = chilkat::Xml::new();
xml.set_tag("ManageInvoiceRequest");
let _ = xml.add_attribute("xmlns", "http://schemas.nav.gov.hu/OSA/2.0/api");
xml.update_child_content("header|requestId", &sb_request_id.get_as_string().unwrap_or_default());
xml.update_child_content("header|timestamp", &dt_now.get_as_timestamp(false).unwrap_or_default());
xml.update_child_content("header|requestVersion", "2.0");
xml.update_child_content("header|headerVersion", "1.0");
xml.update_child_content("user|login", "lwilsmn0uqdxe6u");
xml.update_child_content("user|passwordHash", &password_hash);
xml.update_child_content("user|taxNumber", "11111111");
xml.update_child_content("user|requestSignature", &request_signature);
xml.update_child_content("software|softwareId", "123456789123456789");
xml.update_child_content("software|softwareName", "string");
xml.update_child_content("software|softwareOperation", "LOCAL_SOFTWARE");
xml.update_child_content("software|softwareMainVersion", "string");
xml.update_child_content("software|softwareDevName", "string");
xml.update_child_content("software|softwareDevContact", "string");
xml.update_child_content("software|softwareDevCountryCode", "HU");
xml.update_child_content("software|softwareDevTaxNumber", "string");
xml.update_child_content("exchangeToken", &exchange_token);
xml.update_child_content("invoiceOperations|compressedContent", "false");
xml.update_child_content("invoiceOperations|invoiceOperation|index", "1");
xml.update_child_content("invoiceOperations|invoiceOperation|invoiceOperation", "CREATE");
xml.update_child_content("invoiceOperations|invoiceOperation|invoiceData", &bd_invoice_data1.get_encoded("base64").unwrap_or_default());
xml.update_child_content("invoiceOperations|invoiceOperation[1]|index", "2");
xml.update_child_content("invoiceOperations|invoiceOperation[1]|invoiceOperation", "CREATE");
xml.update_child_content("invoiceOperations|invoiceOperation[1]|invoiceData", &bd_invoice_data2.get_encoded("base64").unwrap_or_default());
xml.update_child_content("invoiceOperations|invoiceOperation[2]|index", "3");
xml.update_child_content("invoiceOperations|invoiceOperation[2]|invoiceOperation", "CREATE");
xml.update_child_content("invoiceOperations|invoiceOperation[2]|invoiceData", &bd_invoice_data3.get_encoded("base64").unwrap_or_default());

// POST the XML to https://api-test.onlineszamla.nav.gov.hu/invoiceService/v2/manageInvoice
let http = chilkat::Http::new();
http.set_accept("application/xml");
let endpoint = "https://api-test.onlineszamla.nav.gov.hu/invoiceService/v2/manageInvoice".to_string();
let resp = chilkat::HttpResponse::new();
if http.http_str("POST", &endpoint, &xml.get_xml().unwrap_or_default(), "utf-8", "application/xml", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

println!("Response status code = {}", resp.status_code());

let resp_xml = chilkat::Xml::new();
let _ = resp_xml.load_xml(&resp.body_str());
println!("Response body:");
println!("{}", resp_xml.get_xml().unwrap_or_default());

// The result looks like this:

// <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
// <ManageInvoiceResponse xmlns="http://schemas.nav.gov.hu/OSA/2.0/api" xmlns:ns2="http://schemas.nav.gov.hu/OSA/2.0/data">
//     <header>
//         <requestId>RID871830318143</requestId>
//         <timestamp>2020-03-25T15:51:25Z</timestamp>
//         <requestVersion>2.0</requestVersion>
//         <headerVersion>1.0</headerVersion>
//     </header>
//     <result>
//         <funcCode>OK</funcCode>
//     </result>
//     <software>
//         <softwareId>123456789123456789</softwareId>
//         <softwareName>string</softwareName>
//         <softwareOperation>LOCAL_SOFTWARE</softwareOperation>
//         <softwareMainVersion>string</softwareMainVersion>
//         <softwareDevName>string</softwareDevName>
//         <softwareDevContact>string</softwareDevContact>
//         <softwareDevCountryCode>HU</softwareDevCountryCode>
//         <softwareDevTaxNumber>string</softwareDevTaxNumber>
//     </software>
//     <transactionId>2WT9GFSKFUU1V4XP</transactionId>
// </ManageInvoiceResponse>

// Use this online tool to generate parsing code from sample XML: 
// Generate Parsing Code from XML

let manage_invoice_response_xmlns = resp_xml.get_attr_value("xmlns").unwrap_or_default();
let manage_invoice_response_xmlns_ns2 = resp_xml.get_attr_value("xmlns:ns2").unwrap_or_default();
let request_id = resp_xml.get_child_content("header|requestId").unwrap_or_default();
let timestamp = resp_xml.get_child_content("header|timestamp").unwrap_or_default();
let request_version = resp_xml.get_child_content("header|requestVersion").unwrap_or_default();
let header_version = resp_xml.get_child_content("header|headerVersion").unwrap_or_default();
let func_code = resp_xml.get_child_content("result|funcCode").unwrap_or_default();
let software_id = resp_xml.get_child_content("software|softwareId").unwrap_or_default();
let software_name = resp_xml.get_child_content("software|softwareName").unwrap_or_default();
let software_operation = resp_xml.get_child_content("software|softwareOperation").unwrap_or_default();
let software_main_version = resp_xml.get_child_content("software|softwareMainVersion").unwrap_or_default();
let software_dev_name = resp_xml.get_child_content("software|softwareDevName").unwrap_or_default();
let software_dev_contact = resp_xml.get_child_content("software|softwareDevContact").unwrap_or_default();
let software_dev_country_code = resp_xml.get_child_content("software|softwareDevCountryCode").unwrap_or_default();
let software_dev_tax_number = resp_xml.get_child_content("software|softwareDevTaxNumber").unwrap_or_default();
let transaction_id = resp_xml.get_child_content("transactionId").unwrap_or_default();