public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var parsed = HttpUtility.ParseQueryString(requestBody); var countryCode = parsed["text"].Substring(0, 2); var vatNumber = parsed["text"].Substring(2).Replace(" ", string.Empty); var xmlContent = await euVatChecker.PostXMLToEU(countryCode, vatNumber); var soapResponse = XDocument.Parse(xmlContent.ToString()); var valParam = euVatChecker.GetValidEUParam(soapResponse); var responseToSlack = $"{valParam.cCode}{valParam.vatNum}\n{valParam.name}\n{valParam.address}"; return(new OkObjectResult(responseToSlack)); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { string clientId = null, contactId = null, fullVatNumber = null, info = null; try { // For details about the structure of the HTTP request sent // from Billomat see https://www.billomat.com/api/webhooks/. (clientId, contactId) = await GetClientIdFromRequestBody(req.Body); if (string.IsNullOrEmpty(clientId) && string.IsNullOrEmpty(contactId)) { log.LogError("Cannot find client/contact id in JSON body."); return(new BadRequestResult()); } info = clientId; if (!string.IsNullOrWhiteSpace(contactId)) { info = $"{clientId} - {contactId}"; } log.LogInformation($"Got ids: {info}"); string clientAddress = null, countryCode = null, clientName = null, vatNumber = null; var billomatClient = await GetClientFromBillomat(clientId, log); fullVatNumber = billomatClient?.vat_number ?? string.Empty; if (!string.IsNullOrWhiteSpace(fullVatNumber) && fullVatNumber.Length >= 2) { vatNumber = fullVatNumber.Substring(2).Replace(" ", string.Empty); info = $"{fullVatNumber} - {info}"; } if (!string.IsNullOrEmpty(contactId)) { log.LogInformation($"Using contact info instead of client."); var billomatContact = await GetContactFromBillomat(contactId, log); countryCode = billomatContact.country_code; clientName = billomatContact.name; var street = billomatContact.street; var zip = billomatContact.zip; var city = billomatContact.city; clientAddress = street + " " + countryCode + "-" + zip + " " + city; } else { countryCode = billomatClient.country_code; clientName = billomatClient.name; var street = billomatClient.street; var zip = billomatClient.zip; var city = billomatClient.city; clientAddress = street + " " + countryCode + "-" + zip + " " + city; } var xmlContent = await euVatChecker.PostXMLToEU(countryCode, vatNumber); var soapResponse = XDocument.Parse(xmlContent.ToString()); var valParam = euVatChecker.GetValidEUParam(soapResponse); // UST_ID Validation if (string.IsNullOrEmpty(valParam.valid)) { log.LogError("Cannot find vat information of company" + clientName); // do not NotFoundObjectResult here => disables weebhook in billomat return(new OkObjectResult("Cannot find vat information of company" + clientName)); } var sendSlackMessageOnSuccess = Environment.GetEnvironmentVariable("SENDMESSAGEONSUCCESS", EnvironmentVariableTarget.Process) == "true"; (var userResponse, var foundError) = ValidateVatInformation(countryCode, vatNumber, clientName, clientAddress, valParam, sendSlackMessageOnSuccess); if (foundError || sendSlackMessageOnSuccess) { await PostToSlack(userResponse, log); } return(new OkObjectResult(userResponse)); } catch (Exception ex) { var sendError = $"Error while checking VAT ID ({info}): {ex.Message}"; log.LogError(ex, sendError); await PostToSlack(sendError, log); return(new OkResult()); } }