Scroll

Custom Function: Get VAT

This function takes one or two arguments. With one argument, returns the VAT rate for the specified country code. With two arguments, returns the converted amount for the specified country code.

Call with country code for VAT rates; call with country code and value for converted amount.

Arguments (Min - Max): 1 - 2

References:

  • System.dll;
  • System.Web.Extensions.dll;
  • mscorlib.dll

Namespaces:

  • System.IO;
  • System.Net;
  • System.Web.Script.Serialization;
  • System.Collections.Generic

Usage

GetVat(x)

GetVat(x, y)

  • x is the country code
  • Optional: y is an amount to add VAT

Code

// convert the first argument to country
string country = args[0].ToString().ToUpper(); // VAT API URL first variable
double amount = 1; // VAT API URL second variable
string url = null; // URL path for VAT API string property = null; // which JSON property to return // number of arguments determines whether we get rate or converted amount // if one argument, return the rate if (args.Length == 1) { property = "standard_rate"; } // if two arguments, return the converted amount if (args.Length == 2) { if (!double.TryParse(args[1].ToString(), out amount)) return "Error: '" + args[1] + "' is not a number."; property = "result"; } // set URL pattern to get the rates for the input country url = string.Format("https://euvat.ga/api/calc/{0}/{1}", country, amount); // get the JSON from the API string response = new WebClient().DownloadString(url); // deserialize the JSON var ser = new System.Web.Script.Serialization.JavaScriptSerializer(); Dictionary<string, object> json; json = (Dictionary<string, object>)ser.DeserializeObject(response); // return the appropriate JSON property return json[property].ToString();

Hidden Article Information

Article Author
Exago Development
created 2017-07-18 21:45:23 UTC
updated 2017-07-18 22:03:01 UTC

Labels
functions, value, example, custom function, code, tax, rate, country,
Have more questions? Submit a request