private async void logGetButton_Click(object sender, EventArgs e) { AfcRestClient client = await GetAfcRestClient(); if (client == null) { return; } try { if (processIdTextBox.Text != null) { var response = await client.GetGeoBatchLog(processIdTextBox.Text); geoBatchResponseTextBox.Text = Newtonsoft.Json.JsonConvert.SerializeObject(response, Newtonsoft.Json.Formatting.Indented); } else { return; } } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Error Processing Sample Request", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { client.Dispose(); } }
/// <summary> /// Example showing how to get address information for a PCode. /// </summary> private static void GetAddress() { uint pcode = 452600; // PCode for Crested Butte, CO PrintSeparator(); Console.WriteLine($"API: GET {BaseAddress}/api/v1/Location/Address/{pcode}"); Console.WriteLine(); using (var client = new AfcRestClient(BaseAddress, UserName, Password, ClientId, ClientProfileId)) { try { // Invoke REST API to get the server time AddressData[] addressData = client.GetAddress(pcode).Result; Console.WriteLine($"RESPONSE:"); Console.WriteLine(JsonConvert.SerializeObject(addressData, Formatting.Indented)); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine($"ERROR: {ex.Message}"); } } }
private async void uploadButton_ClickAsync(object sender, EventArgs e) { // Get client for invoking Avalara for Communications REST API AfcRestClient client = await GetAfcRestClient(); if (client == null) { return; } OpenFileDialog dialog = new OpenFileDialog(); if (dialog.ShowDialog() == DialogResult.OK) { fileUploadLabel.Text = "File Uploaded:" + dialog.SafeFileName; byte[] buffer = File.ReadAllBytes(dialog.FileName); try { var response = await client.GeoBatchUpload(buffer, dialog.SafeFileName); geoBatchResponseTextBox.Text = Newtonsoft.Json.JsonConvert.SerializeObject(response, Newtonsoft.Json.Formatting.Indented); processIdTextBox.Text = response.ProcessId.ToString(); } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Error Processing Sample Request", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { client.Dispose(); } } }
/// <summary> /// Validates the credentials and retuns an instance of the AfcRestClient for invoking the REST API. /// </summary> /// <returns>AfcRestClient object or null if credentials are invalid.</returns> private async Task <AfcRestClient> GetAfcRestClient() { string error = string.Empty; if (string.IsNullOrWhiteSpace(urlStatusLabel.Text)) { MessageBox.Show(this, "Enter the URL for the AFC REST API in the RestDemoApplication.exe.config file.", "Invalid URL", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } if (string.IsNullOrWhiteSpace(usernameTextBox.Text)) { MessageBox.Show(this, "Please enter username.", "Missing Credentials", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } else if (string.IsNullOrWhiteSpace(passwordTextBox.Text)) { MessageBox.Show(this, "Please enter password.", "Missing Credentials", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } try { int?clientID = string.IsNullOrEmpty(clientIDTextBox.Text) ? null : (int?)int.Parse(clientIDTextBox.Text); int?profileID = string.IsNullOrEmpty(profileIDTextBox.Text) ? null : (int?)int.Parse(profileIDTextBox.Text); // Invoke API to get validate credentials and return the Comms Platform version var client = new AfcRestClient(ConfigurationManager.AppSettings["AfcRestApiUrl"], usernameTextBox.Text, passwordTextBox.Text, clientID, profileID); string version = await client.GetAsync($"api/v1/Application/RESTVersion"); versionStatusLabel.Text = $"Comms Platform Version: {version}"; return(client); } catch (AggregateException ex) { MessageBox.Show(this, ex.InnerException?.Message ?? ex.Message, "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(null); }
/// <summary> /// Example showing how to obtain the PCode for a location. /// </summary> private static void GetPCode() { PrintSeparator(); Console.WriteLine($"API: POST {BaseAddress}/api/v1/Location/PCode"); Console.WriteLine(); using (var client = new AfcRestClient(BaseAddress, UserName, Password, ClientId, ClientProfileId)) { try { // Create a RequestPCodeDetail object for the request body // NOTE: only one of the following properties must be populated: FipsCode, NpaNxx, or ZipAddress var request = new RequestPCodeDetail { FipsCode = null, NpaNxx = null, ZipAddress = new ZipAddress { CountryIso = "USA", State = "WA", County = "King", Locality = "Seattle", ZipCode = "98101" } }; Console.WriteLine($"REQUEST BODY:"); Console.WriteLine(request.ToJson()); Console.WriteLine(); // Invoke REST API to obtain the PCode for the location uint pcode = client.GetPCode(request).Result; Console.WriteLine($"RESPONSE:"); Console.WriteLine(pcode); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine($"ERROR: {ex.Message}"); } } }
private async void statusGetButton_Click(object sender, EventArgs e) { AfcRestClient client = await GetAfcRestClient(); if (client == null) { return; } try { if (processIdTextBox.Text != null) { var response = await client.GetGeoBatchStatus(processIdTextBox.Text); geoBatchResponseTextBox.Text = Newtonsoft.Json.JsonConvert.SerializeObject(response, Newtonsoft.Json.Formatting.Indented); geoBatchInputFileLink = response.Downloads.InputFileDownload; geoBatchOutputFileLink = response.Downloads.OutputFileDownload; if (!string.IsNullOrEmpty(geoBatchInputFileLink)) { geoBatchInputDownloadButton.Visible = true; geoBatchFileNotReadyLabel.Visible = true; } if (!string.IsNullOrEmpty(geoBatchOutputFileLink)) { geoBatchOutputDownloadButton.Visible = true; geoBatchFileNotReadyLabel.Visible = true; } } else { return; } } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Error Processing Sample Request", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { client.Dispose(); } }
/// <summary> /// Invokes the Avalara for Communications REST API to process the sample request. /// </summary> private async Task ProcessSampleRequest() { // Get client for invoking Avalara for Communications REST API AfcRestClient client = await GetAfcRestClient(); if (client == null) { return; } // Get request from input CalcTaxesRequest request = GetSampleRequest(); if (request == null) { return; } try { CalcTaxesResponse response = await client.CalcTaxes(request); responseTextBox.Text = Newtonsoft.Json.JsonConvert.SerializeObject(response, Newtonsoft.Json.Formatting.Indented); } catch (AggregateException ex) { MessageBox.Show(this, ex.InnerException?.Message ?? ex.Message, "Error Processing Sample Request", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Error Processing Sample Request", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { client.Dispose(); } }
/// <summary> /// Example showing how to get the server time from the AFC REST service. /// </summary> private static void GetServerTime() { PrintSeparator(); Console.WriteLine($"API: GET {BaseAddress}/api/v1/Application/ServerTime"); Console.WriteLine(); using (var client = new AfcRestClient(BaseAddress, UserName, Password, ClientId, ClientProfileId)) { try { // Invoke REST API to get the server time DateTime serverTime = client.GetServerTime().Result; Console.WriteLine($"RESPONSE:"); Console.WriteLine(serverTime.ToString(UtcFormat)); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine($"ERROR: {ex.Message}"); } } }
/// <summary> /// Example showing how to calculate taxes on a transaction while overriding tax rate for a specific tax. /// </summary> private static void CalculateTaxesWithOverrides() { PrintSeparator(); Console.WriteLine($"API: POST {BaseAddress}/api/v1/CalculateWithOverrides/Taxes"); Console.WriteLine(); using (var client = new AfcRestClient(BaseAddress, UserName, Password, ClientId, ClientProfileId)) { try { // Create a CalculateWithOverridesRequest object to specify the trasaction and override data var request = new CalculateWithOverridesRequest { Overrides = new TaxRateOverrideInfo[] { new TaxRateOverrideInfo { Pcode = 4133800, // PCode for Seattle, WA LevelExemptible = true, Scope = 1, // State-level scope TaxLevel = 1, // State-level tax TaxType = 1, // Sales tax type BracketInfo = new [] { new TaxBracketInfo { MaxBase = int.MaxValue, // Use max value if bracket has no limit Rate = 0.0123 // Enter rate as decimal for percentage-based taxes } }.ToList() } }.ToList(), Transaction = new Transaction { BillToPCode = 4133800, // PCode for Seattle, WA BusinessClass = 0, // ILEC Charge = 25.95, CompanyIdentifier = "TST", CustomerType = 0, // Residential Date = DateTime.Today, FacilitiesBased = true, Franchise = true, Incorporated = true, Lifeline = false, Lines = 1, Locations = 0, Minutes = 0, OriginationPCode = 4133800, // PCode for Seattle, WA Regulated = false, Sale = true, ServiceClass = 1, // Primary long distance ServiceType = 15, // Product TerminationPCode = 4133800, // PCode for Seattle, WA TransactionType = 10 // Sales } }; Console.WriteLine($"REQUEST BODY:"); Console.WriteLine(request.ToJson()); Console.WriteLine(); // Invoke REST API to calculate taxes on the transaction using the specified tax rate override TaxData[] taxes = client.CalculateTaxesWithOverrides(request).Result; Console.WriteLine($"RESPONSE:"); Console.WriteLine(JsonConvert.SerializeObject(taxes, Formatting.Indented)); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine($"ERROR: {ex.Message}"); } } }
/// <summary> /// Example showing how to calculate taxes for a bridge conference transaction. /// </summary> private static void CalculateBridgeConferenceTaxes() { PrintSeparator(); Console.WriteLine($"API: POST {BaseAddress}/api/v1/BridgeConference/Taxes"); Console.WriteLine(); using (var client = new AfcRestClient(BaseAddress, UserName, Password, ClientId, ClientProfileId)) { try { // Create a BridgeConferenceTransaction object to specify the input data for the tax calculation var transaction = new BridgeConferenceTransaction { BillingAddress = new ZipAddress { CountryIso = "USA", State = "WA", County = "King", Locality = "Seattle", ZipCode = "98101" }, BridgeAddress = new ZipAddress { CountryIso = "USA", State = "KS", County = "Johnson", Locality = "Overland Park", ZipCode = "66212" }, BusinessClass = 0, // ILEC Charge = 25.95, CompanyIdentifier = "TST", CustomerType = 0, // Residential Date = DateTime.Today, FacilitiesBased = true, Franchise = true, HostAddress = new ZipAddress { CountryIso = "USA", State = "KS", County = "Johnson", Locality = "Overland Park", ZipCode = "66212" }, Incorporated = true, Lifeline = false, Lines = 1, Locations = 0, Minutes = 0, Participants = new [] { new BridgeConferenceParticipant { ParticipantAddress = new ZipAddress { CountryIso = "USA", State = "WA", County = "King", Locality = "Seattle", ZipCode = "98101" }, ParticipantRef = "1" }, new BridgeConferenceParticipant { ParticipantAddress = new ZipAddress { CountryIso = "USA", State = "CO", County = "Denver", Locality = "Denver", ZipCode = "80014" }, ParticipantRef = "2" } }.ToList(), ProcessInvalidParticipant = true, // Don't fail transaction if a participant fails to be processed Regulated = false, ReturnParticipantTaxes = true, // Return each individual participant's taxes in the response Sale = true, ServiceClass = 1 // Primary long distance }; Console.WriteLine($"REQUEST BODY:"); Console.WriteLine(transaction.ToJson()); Console.WriteLine(); // Invoke REST API to calculate taxes on the bridge conference transaction BridgeConferenceResults results = client.CalculateBridgeConferenceTaxes(transaction).Result; Console.WriteLine($"RESPONSE:"); Console.WriteLine(results.ToJson()); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine($"ERROR: {ex.Message}"); } } }
/// <summary> /// Example showing how to calculate tax adjustments for a Transaction. /// </summary> private static void CalculateAdjustments() { PrintSeparator(); Console.WriteLine($"API: POST {BaseAddress}/api/v1/CalculateAdjustments"); Console.WriteLine(); using (var client = new AfcRestClient(BaseAddress, UserName, Password, ClientId, ClientProfileId)) { try { // Create a Transaction object similarly as for processing a regular tax calculation var transaction = new Transaction { AdjustmentDiscount = 3, // Specify a discount type for adjustments AdjustmentMethod = 0, // Use default adjustment method to apply tax brackets normally BillToAddress = new ZipAddress { CountryIso = "USA", State = "WA", County = "King", Locality = "Seattle", ZipCode = "98101" }, BusinessClass = 0, // ILEC Charge = 25.95, // NOTE: Enter a positive amount for the amount being refunded CompanyIdentifier = "TST", CustomerType = 0, // Residential Date = DateTime.Today, // NOTE: For adjustments, use the same date as original transaction FacilitiesBased = true, Franchise = true, Incorporated = true, Lifeline = false, Lines = 1, Locations = 0, Minutes = 0, Regulated = false, Sale = true, ServiceClass = 1, // Primary long distance ServiceType = 6, // Access Charge TransactionType = 19 // VoIP }; transaction.OriginationAddress = transaction.TerminationAddress = transaction.BillToAddress; Console.WriteLine($"REQUEST BODY:"); Console.WriteLine(transaction.ToJson()); Console.WriteLine(); // Invoke REST API to calculate tax adjustments on the transaction TaxData[] taxes = client.CalculateAdjustments(transaction).Result; Console.WriteLine($"RESPONSE:"); Console.WriteLine(JsonConvert.SerializeObject(taxes, Formatting.Indented)); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine($"ERROR: {ex.Message}"); } } }
/// <summary> /// Example showing how to calculate taxes for a Transaction. /// </summary> private static void CalculateTaxes() { PrintSeparator(); Console.WriteLine($"API: POST {BaseAddress}/api/v1/CalculateTaxes"); Console.WriteLine(); using (var client = new AfcRestClient(BaseAddress, UserName, Password, ClientId, ClientProfileId)) { try { // Create a Transaction object to specify the input data for the tax calculation // NOTE: Bill-To, Origination, and Termination can be entered using PCodes, FIPS Codes, NPANXX, or ZipAddress. // NOTE: Use your Avalara-provided CompanyIdentifier to include this transaction and taxes in your compliance // reports or leave blank otherwise (for testing and calculating taxes on quotes). // NOTE: CustomerNumber, InvoiceNumber, and other optional fields may be specified for reporting purposes var transaction = new Transaction { BillToAddress = new ZipAddress { CountryIso = "USA", State = "WA", County = "King", Locality = "Seattle", ZipCode = "98101" }, BusinessClass = 0, // ILEC Charge = 25.95, CompanyIdentifier = "TST", CustomerType = 0, // Residential Date = DateTime.Today, FacilitiesBased = true, Franchise = true, Incorporated = true, Lifeline = false, Lines = 1, Locations = 0, Minutes = 0, Regulated = false, Sale = true, ServiceClass = 1, // Primary long distance ServiceType = 6, // Access Charge TransactionType = 19 // VoIP }; // NOTE: Depending on the transaction/service type, Origination and Termination may not apply // in which case the same jurisdiction as the bill-to location must be populated into these fields transaction.OriginationAddress = transaction.TerminationAddress = transaction.BillToAddress; Console.WriteLine($"REQUEST BODY:"); Console.WriteLine(transaction.ToJson()); Console.WriteLine(); // Invoke REST API to calculate taxes on the transaction TaxData[] taxes = client.CalculateTaxes(transaction).Result; Console.WriteLine($"RESPONSE:"); Console.WriteLine(JsonConvert.SerializeObject(taxes, Formatting.Indented)); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine($"ERROR: {ex.Message}"); } } }
/// <summary> /// Begins running the performance test for the Avalara for Communications REST service. /// </summary> private async Task StartPerformanceTest() { ClearPerformanceStats(); // Get client for invoking Avalara for Communications REST API AfcRestClient afcClient = await GetAfcRestClient(); if (afcClient == null) { EndPerformanceTest(false); return; } // Get request from input performanceTestRequest = GetSampleRequest(); if (performanceTestRequest == null) { EndPerformanceTest(false); return; } try { // Process request once to validate request body await afcClient.CalcTaxes(performanceTestRequest); maxThreads = int.Parse(maxThreadsComboBox.Text); maxMinutes = int.Parse(maxTimeComboBox.Text); // Create tasks for the specified number of threads var tasks = new List <Task>(); for (int threadCount = 0; threadCount < maxThreads; threadCount++) { tasks.Add(new Task(async() => { AfcRestClient client = afcClient; Stopwatch stopwatch = new Stopwatch(); try { while (!cancellationToken.IsCancellationRequested) { stopwatch.Restart(); var response = await client.CalcTaxes(performanceTestRequest); stopwatch.Stop(); responseStats.Enqueue((int)stopwatch.ElapsedMilliseconds); int taxCount = response.InvoiceResults?.Sum(inv => inv.ItemResults?.Sum(item => item.Taxes?.Count() ?? 0) ?? 0) ?? 0; Interlocked.Increment(ref requestCount); Interlocked.Add(ref taxesReturned, taxCount); } } catch { Interlocked.Increment(ref failedRequests); } })); } // Begin making calls to Avalara for Communications REST API startTime = DateTime.UtcNow; tasks.ForEach(t => t.Start()); } catch { afcClient.Dispose(); } }