/// <summary> /// /// </summary> /// <param name="url"></param> /// <param name="xml"></param> /// <param name="id"></param> public static void PostRps(Uri url, string xml, string id) { var parameters = new NameValueCollection { { "numeroLote", id }, { "xml", xml } }; try { var webClient = new WebClient(); webClient.UploadValues(url, "POST", parameters); } catch (WebException ex) { TLog.Write("WebException trying to post the rps xml response at URL: '" + url + "' - xml: '" + xml + "' - WebException: " + ex); throw new ApplicationException( "Oops!! O tempo limite de conexão ao serviço de notificar o estado dos RPSs no Smart expirou. Por favor, verifique sua internet e tente novamente..." + Environment.NewLine + "Erro: " + ex.Message, ex); } catch (Exception ex) { TLog.Write("Exception trying to post the rps xml response at URL: '" + url + "' - xml: '" + xml + "' - Exception: " + ex); throw new ApplicationException( "Oops!! Ocorreu algum erro ao tentar conectar ao serviço de notificar o estado dos RPSs no Smart. Por favor, verifique sua internet e tente novamente..." + Environment.NewLine + "Erro: " + ex.Message, ex); } }
/// <summary> /// /// </summary> /// <param name="str"></param> /// <returns></returns> public static XDocument[] ConvertToXArray(string str) { string requestXml; var xRequests = new List <XDocument>(); var sr = new StringReader(str); while ((requestXml = sr.ReadLine()) != null) { if (String.IsNullOrWhiteSpace(requestXml)) { continue; } try { var xRequest = XDocument.Parse(requestXml); xRequests.Add(xRequest); } catch (Exception ex) { TLog.Write("Exception trying to load RPS to xml - exception: " + ex); } } return(xRequests.ToArray()); }
/// <summary> /// /// </summary> private void SetRpsNumber() { var rpsNumber = RequestNextRpsNumber(); var numberNodes = Rps.Descendants(FDefaultNamespace + "Numero"); if (numberNodes.Count() == 0) { TLog.Write("Xml tag 'numero' not found at Smart RPS - xml: " + Rps.ToString(SaveOptions.DisableFormatting)); throw new ApplicationException("O xml do Smart com o RPS para fazer a conversão para nota fiscal está inválido. Tag 'numero' não encontrada."); } var numberNode = numberNodes.First(); numberNode.SetValue(rpsNumber); }
/// <summary> /// /// </summary> /// <param name="url"></param> /// <param name="fileName"></param> public static void PostFile(Uri url, string fileName) { try { var webClient = new WebClient(); webClient.UploadFile(url, fileName); } catch (WebException ex) { TLog.Write("WebException trying to post the log file at URL: '" + url + "' - logFileName: '" + fileName + "' - WebException: " + ex); throw new ApplicationException( "Oops!! O tempo limite de conexão ao serviço para enviar o arquivo de log expirou. Por favor, verifique sua internet e tente novamente..." + Environment.NewLine + "Erro: " + ex.Message, ex); } catch (Exception ex) { TLog.Write("Exception trying to post the log file at URL: '" + url + "' - logFileName: '" + fileName + "' - Exception: " + ex); throw new ApplicationException( "Oops!! Ocorreu algum erro ao tentar conectar ao para enviar o arquivo de log. Por favor, verifique sua internet e tente novamente..." + Environment.NewLine + "Erro: " + ex.Message, ex); } }
/// <summary> /// /// </summary> /// <param name="xmlResponse"></param> /// <param name="message"></param> /// <returns></returns> private bool ParseXResponse(string xmlResponse, out string message) { XDocument xDocument; if (String.IsNullOrWhiteSpace(xmlResponse)) { message = "O serviço de converter o RPS em nota fiscal retornou branco."; TLog.Write("PedidoEnvioRPS xml response is blank. - xml sent: " + FRpsSent); return(false); } try { xDocument = XDocument.Parse(xmlResponse); } catch (Exception ex) { message = "O serviço de converter o RPS em nota fiscal retornou um XML inválido."; TLog.Write("Exception trying to parse the PedidoEnvioRPS xml response - xml: " + xmlResponse + " - xml sent: " + FRpsSent + " - Exception: " + ex + ""); return(false); } var successNode = xDocument.Descendants("Sucesso"); if (successNode.Count() == 0) { message = "O serviço de converter o RPS em nota fiscal retornou um XML inválido. Tag 'Sucesso' não encontrada."; TLog.Write("Xml tag 'Sucesso' not found at PedidoEnvioRPS xml response - xml: " + xmlResponse + " - xml sent: " + FRpsSent); return(false); } if (successNode.First().Value == "false") { var errorMessage = String.Empty; var messageNode = xDocument.Descendants("Descricao"); if (messageNode.Count() > 0) { errorMessage = messageNode.First().Value; } message = "O serviço de converter o RPS em nota fiscal retornou um XML retornou o erro: '" + errorMessage + "'"; TLog.Write("The PedidoEnvioRPS response xml, returned false - xml: " + xmlResponse + " - xml sent: " + FRpsSent); return(false); } var nfseNode = xDocument.Descendants("ChaveNFe"); if (nfseNode.Count() == 0) { message = "O serviço de converter o RPS em nota fiscal retornou um XML inválido. Tag 'ChaveNFe' não encontrada."; TLog.Write("Xml tag 'ChaveNFe' not found at PedidoEnvioRPS xml response - xml: " + xmlResponse + " - xml sent: " + FRpsSent); return(false); } var numberNode = nfseNode.Descendants("Numero"); if (numberNode.Count() == 0) { message = "O serviço de converter o RPS em nota fiscal retornou um XML inválido. Tag 'Numero' não encontrada."; TLog.Write("Xml tag 'Numero' not found at PedidoEnvioRPS xml response - xml: " + xmlResponse + " - xml sent: " + FRpsSent); return(false); } int number; var numberValue = numberNode.First().Value; if (String.IsNullOrWhiteSpace(numberValue)) { message = "O serviço de converter o RPS em nota fiscal retornou um valor em branco."; TLog.Write("The PedidoEnvioRPS xml response, returned a blank value - xml: " + xmlResponse + " - xml sent: " + FRpsSent); return(false); } if (!int.TryParse(numberValue, out number)) { message = "O serviço de converter o RPS em nota fiscal retornou um número inválido para a nota fiscal. Numero: " + numberValue; TLog.Write("The PedidoEnvioRPS xml response, returned an invalid number ChaveNFe/Numero - xml: " + xmlResponse + " - xml sent: " + FRpsSent); return(false); } message = number.ToString(); return(true); }
/// <summary> /// /// </summary> /// <param name="xml"></param> /// <returns></returns> private int ExtractRpsNumber(string xml) { XDocument xDocument; if (String.IsNullOrWhiteSpace(xml)) { TLog.Write("ConsultaRPSNumero xml is blank."); throw new ApplicationException("O serviço de buscar o número do próximo RPS retornou branco."); } try { xDocument = XDocument.Parse(xml); } catch (Exception ex) { TLog.Write("Exception trying to parse the ConsultaRPSNumero xml response - xml: " + xml + " - Exception: " + ex + ""); throw new ApplicationException("O serviço de buscar o número do próximo RPS retornou um XML inválido."); } var successNode = xDocument.Descendants("Sucesso"); if (successNode.Count() == 0) { TLog.Write("Xml tag 'Sucesso' not found at ConsultaRPSNumero xml response - xml: " + xml); throw new ApplicationException("O serviço de buscar o número do próximo RPS retornou um XML inválido. Tag 'Sucesso' não encontrada."); } if (successNode.First().Value == "false") { var errorMessage = String.Empty; var messageNode = xDocument.Descendants("Descricao"); if (messageNode.Count() > 0) { errorMessage = messageNode.First().Value; } TLog.Write("The ConsultaRPSNumero response xml, returned false - xml: " + xml); throw new ApplicationException("O serviço de buscar o número do próximo RPS retornou o erro: '" + errorMessage + "'"); } var numberNode = xDocument.Descendants("Numero"); if (numberNode.Count() == 0) { TLog.Write("Xml tag 'Numero' not found at ConsultaRPSNumero xml response - xml: " + xml); throw new ApplicationException("O serviço de buscar o número do próximo RPS retornou um XML inválido. Tag 'Numero' não encontrada."); } int number; var numberValue = numberNode.First().Value; if (String.IsNullOrWhiteSpace(numberValue)) { TLog.Write("The ConsultaRPSNumero xml response, returned a blank value - xml: " + xml); throw new ApplicationException("O serviço de buscar o número do próximo RPS retornou um valor em branco."); } if (!int.TryParse(numberValue, out number)) { TLog.Write("The ConsultaRPSNumero xml response, returned an invalid number - xml: " + xml); throw new ApplicationException("O serviço de buscar o número do próximo RPS retornou um número inválido. Numero: " + numberValue); } return(number); }