Esempio n. 1
0
        public IHttpActionResult DocumentCallBack([FromBody] CallBackResponse request)
        {
            IRequestRecordLog requestRecordLog = new RequestRecordLog();

            actionDocument = new Document();

            try
            {
                RequestRecord record = new RequestRecord()
                {
                    clave             = request.clave,
                    docDatetime       = DateTime.Parse(request.fecha),
                    indState          = request.indEstado,
                    responseXML       = request.respuestaXml,
                    callBacakDatetime = DateTime.Now
                };

                requestRecordLog.UpdateRequestRecord(record);
                //actionDocument.StoreCallBackResponse(request);
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                LogManager.RegisterCallBackFailToDataBase(JsonConvert.SerializeObject(request));
            }

            return(Ok());
        }
Esempio n. 2
0
        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);
        }