public async Task <ReceivedDigitalDocument> Post(object request, string functionToCall, string domain = "", string accountId = "", string oAuthToken = "") { ReceivedDigitalDocument InvoiceResponse = new ReceivedDigitalDocument(); HttpResponseMessage response; try { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(Constants.Constants.auth_header_bearer, string.IsNullOrEmpty(oAuthToken) ? await Authentication.Instance().AuthenticationMHById(accountId) : oAuthToken); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(string.Format(Constants.Constants.RequestApiFormat_2, (string.IsNullOrEmpty(domain) ? domain : ConfigurationManager.AppSettings[domain]), ConfigurationManager.AppSettings[functionToCall].ToString())); response = await client.PostAsJsonAsync(stringBuilder.ToString(), request); #region Evaluate response if (response != null) { InvoiceResponse.reasonPhrase = response.ReasonPhrase; InvoiceResponse.statusCode = Convert.ToInt32(response.StatusCode); if (response.StatusCode != HttpStatusCode.Accepted) { InvoiceResponse.x_Error_Cause = Convert.ToString(response.Headers.GetValues(Constants.Constants.header_errorCause).FirstOrDefault()); #region Otros Datos Header - de momento no se usan //headerContainer.headerResponse.Breadcrumbid = Convert.ToString(responseMessage.Headers.GetValues(Constants.Constants.header_groupId).FirstOrDefault()); //headerContainer.headerResponse.X_Ratelimit_Limit = Convert.ToInt32(responseMessage.Headers.GetValues(Constants.Constants.header_rateLimit).FirstOrDefault()); //headerContainer.headerResponse.X_Ratelimit_Remaining = Convert.ToInt32(responseMessage.Headers.GetValues(Constants.Constants.header_reteRemaining).FirstOrDefault()); //headerContainer.headerResponse.X_Ratelimit_Reset = Convert.ToInt32(responseMessage.Headers.GetValues(Constants.Constants.header_rateReset).FirstOrDefault()); #endregion } else { InvoiceResponse.fileLocation = response.Headers.GetValues(Constants.Constants.header_location).FirstOrDefault(); } } else { throw new Exception(Constants.Constants.fail_send_electronic_invoice); } #endregion } catch (Exception ex) { throw ex; } return(InvoiceResponse); }
public async Task <ReceivedDigitalDocument> SendAceptDenyDocument(AceptaRechazaDocumento documento, CredentialData credential, RequestAdditionalInfo additionalInfo) { ReceivedDigitalDocument InvoiceResponse = new ReceivedDigitalDocument(); string respuesta = string.Empty; X509Certificate2 cert; AuthenticationResponse oAuthResponse; XmlDocument xmlDocInvoice; object newRequest = null; try { if (!string.IsNullOrEmpty(credential.certificate) && !string.IsNullOrEmpty(credential.certificatePassword)) { cert = new X509Certificate2(Convert.FromBase64String(credential.certificate), credential.certificatePassword); } else { throw new Exception(Constants.Constants.fail_CertificateInfo_incomplete); } xmlDocInvoice = Utils.GetXmlFromObject(documento); CustomSignature.SignXML(documento.FechaEmisionDoc, ref xmlDocInvoice, null, cert); oAuthResponse = await Authentication.Instance().AuthenticationMH(credential.atvUser, credential.atvPassword); if (oAuthResponse != null) { newRequest = new { clave = documento.Clave, fecha = documento.FechaEmisionDoc, emisor = new { additionalInfo.Emisor.tipoIdentificacion, additionalInfo.Emisor.numeroIdentificacion }, receptor = new { additionalInfo.Receptor.tipoIdentificacion, additionalInfo.Receptor.numeroIdentificacion }, consecutivoReceptor = additionalInfo.ConsecutivoReceptor, comprobanteXml = Convert.ToBase64String(Encoding.UTF8.GetBytes(xmlDocInvoice.OuterXml)), callbackUrl = string.Format(Constants.Constants.RequestApiFormat_2, ConfigurationManager.AppSettings[Constants.Constants.serverDomain], ConfigurationManager.AppSettings[Constants.Constants.CallBackUrl]) }; } else { throw new Exception(Constants.Constants.fail_communication_oauth_hacienda); } try { HttpCustomClient client = new HttpCustomClient(); InvoiceResponse = await client.Post(newRequest, Constants.Constants.mhActionRecepcion, Constants.Constants.mhEndpoint, null, oAuthResponse.access_token); InvoiceResponse.xmlInvoice = Convert.ToBase64String(Encoding.UTF8.GetBytes(xmlDocInvoice.OuterXml)); InvoiceResponse.pdfInvoice = ""; } catch (Exception ex) { throw ex; } } catch (Exception ex) { ex.Message.ToString(); } return(InvoiceResponse); }
public async Task <ReceivedDigitalDocument> SendElectronicInvoiceToMH(FacturaElectronica payload, string accountId) { ReceivedDigitalDocument InvoiceResponse = new ReceivedDigitalDocument(); IRequestRecordLog requestRecordLog; BuildFiles buildFiles; HttpResponseMessage responseMessage; XmlDocument xmlDocInvoice; string pdfInvoice; object newRequest = null; try { buildFiles = new BuildFiles(); xmlDocInvoice = new XmlDocument(); requestRecordLog = new RequestRecordLog(); xmlDocInvoice = Utils.GetXmlFromObject(payload); #region Sign XML and Validate against XSD CustomSignature.SignXML(payload.FechaEmision, ref xmlDocInvoice, accountId); //IList<IError> listErros = DataValidator.Instance().ValidateXML(xmlDocInvoice.OuterXml, xsdDocument.FacturaElectronica); //if(listErros.Count > 0) //{ // throw new Exception(string.Format(Constants.Constants.RequestApiFormat_2, Constants.Constants.xml_has_errors, Utils.ConcatElements(listErros)) ); //} #endregion #region Crear PDF pdfInvoice = buildFiles.GetPDFInvoice(payload, accountId); #endregion #region Crea el request de Factura Digital para Hacienda if (payload.Receptor != null) { newRequest = new { clave = payload.Clave, fecha = payload.FechaEmision, emisor = new { tipoIdentificacion = payload.Emisor.Identificacion.Tipo, numeroIdentificacion = payload.Emisor.Identificacion.Numero }, comprobanteXml = Convert.ToBase64String(Encoding.UTF8.GetBytes(xmlDocInvoice.OuterXml)), callbackUrl = string.Format(Constants.Constants.RequestApiFormat_2, ConfigurationManager.AppSettings[Constants.Constants.serverDomain], ConfigurationManager.AppSettings[Constants.Constants.CallBackUrl]) }; } else { newRequest = new { clave = payload.Clave, fecha = payload.FechaEmision, emisor = new { tipoIdentificacion = payload.Emisor.Identificacion.Tipo, numeroIdentificacion = payload.Emisor.Identificacion.Numero }, receptor = new { tipoIdentificacion = payload.Receptor.Identificacion.Tipo, numeroIdentificacion = payload.Receptor.Identificacion.Tipo }, comprobanteXml = Convert.ToBase64String(Encoding.UTF8.GetBytes(xmlDocInvoice.OuterXml)), callbackUrl = string.Format(Constants.Constants.RequestApiFormat_2, ConfigurationManager.AppSettings[Constants.Constants.serverDomain], ConfigurationManager.AppSettings[Constants.Constants.CallBackUrl]) }; } #endregion try { HttpCustomClient client = new HttpCustomClient(); InvoiceResponse = await client.Post(newRequest, Constants.Constants.mhActionRecepcion, Constants.Constants.mhEndpoint, accountId); InvoiceResponse.xmlInvoice = Convert.ToBase64String(Encoding.UTF8.GetBytes(xmlDocInvoice.OuterXml)); InvoiceResponse.pdfInvoice = pdfInvoice; } catch (Exception ex) { throw ex; } } catch (CryptographicException e) { throw e; } return(InvoiceResponse); }