Rust Requires Chilkat v11.0.0+
Rust
Hungary NAV Manage Annulment Request
See more Hungary NAV Invoicing Examples
Demonstrates the manageAnnulment request for the Hungarian NAV Online Invoicing System REST API v2.0.Chilkat Rust Downloads
// 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"?>
// <ManageAnnulmentRequest xmlns="http://schemas.nav.gov.hu/OSA/2.0/api">
// <header>
// <requestId>RID338592103413</requestId>
// <timestamp>2019-09-11T13:37:09.385Z</timestamp>
// <requestVersion>2.0</requestVersion>
// <headerVersion>1.0</headerVersion>
// </header>
// <user>
// <login>lwilsmn0uqdxe6u</login>
// <passwordHash>2F43840A882CFDB7DB0 ... F6CA57FE8CD74DC28E</passwordHash>
// <taxNumber>11111111</taxNumber>
// <requestSignature>A297E3BD9 ... 1959ADFAF5</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>dbd03076-3a9b-4312-bbbb-0cee3a6472572P11CS49ASIL</exchangeToken>
// <annulmentOperations>
// <annulmentOperation>
// <index>1</index>
// <annulmentOperation>ANNUL</annulmentOperation>
// <invoiceAnnulment>PD94bWwgdm ... ubnVsbWVudD4=</invoiceAnnulment>
// </annulmentOperation>
// </annulmentOperations>
// </ManageAnnulmentRequest>
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 first (and only) index hash.
let invoice_operation = "ANNUL".to_string();
let invoice_data = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI/Pg0KPEludm9pY2VBbm51bG1lbnQgeG1sbnM9Imh0dHA6Ly9zY2hlbWFzLm5hdi5nb3YuaHUvT1NBLzIuMC9hbm51bCI+DQoJCTxhbm51bG1lbnRSZWZlcmVuY2U+MjIyMjIyMjI8L2FubnVsbWVudFJlZmVyZW5jZT4NCgkJPGFubnVsbWVudFRpbWVzdGFtcD4yMDE4LTA2LTE4VDA5OjEwOjQ1LjMwOVo8L2FubnVsbWVudFRpbWVzdGFtcD4NCgkJPGFubnVsbWVudENvZGU+RVJSQVRJQ19EQVRBPC9hbm51bG1lbnRDb2RlPg0KCQk8YW5udWxtZW50UmVhc29uPmNyZWF0ZSBzemFtbGEgYW5udWw8L2FubnVsbWVudFJlYXNvbj4NCjwvSW52b2ljZUFubnVsbWVudD4=".to_string();
let sb_hash_base = chilkat::StringBuilder::new();
let _ = sb_hash_base.append(&invoice_operation);
let _ = sb_hash_base.append(&invoice_data);
let first_hash = crypt.hash_string_enc(&sb_hash_base.get_as_string().unwrap_or_default()).unwrap_or_default();
// Append the index hash to the final hash base.
let _ = sb_final_hash_base.append(&first_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("ManageAnnulmentRequest");
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("annulmentOperations|annulmentOperation|index", "1");
xml.update_child_content("annulmentOperations|annulmentOperation|annulmentOperation", &invoice_operation);
xml.update_child_content("annulmentOperations|annulmentOperation|invoiceAnnulment", &invoice_data);
// POST the XML to https://api-test.onlineszamla.nav.gov.hu/invoiceService/v2/manageAnnulment
let http = chilkat::Http::new();
http.set_accept("application/xml");
let endpoint = "https://api-test.onlineszamla.nav.gov.hu/invoiceService/v2/manageAnnulment".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"?>
// <ManageAnnulmentResponse xmlns="http://schemas.nav.gov.hu/OSA/2.0/api" xmlns:ns2="http://schemas.nav.gov.hu/OSA/2.0/data">
// <header>
// <requestId>RID465317110495</requestId>
// <timestamp>2020-03-25T15:08:40Z</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>2WT6XG9LQ0U9VQNA</transactionId>
// </ManageAnnulmentResponse>
// Use this online tool to generate parsing code from sample XML:
// Generate Parsing Code from XML
let manage_annulment_response_xmlns = resp_xml.get_attr_value("xmlns").unwrap_or_default();
let manage_annulment_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();