public static async Task <ResultPay> SendPayAsync(ResultPay pay, string tokenCen) { try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, $"api/v1/operations/payments/create/"); string d = JsonConvert.SerializeObject(pay); wc.Headers[HttpRequestHeader.Authorization] = $"Token {tokenCen}"; NameValueCollection postData = new NameValueCollection() { { "data", d } }; byte[] res = await wc.UploadValuesTaskAsync(uri, WebRequestMethods.Http.Post, postData); // POST if (res != null) { string json = Encoding.UTF8.GetString(res); Pay p = JsonConvert.DeserializeObject <Pay>(json, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); if (p != null) { return(p.Result); } } } } catch (Exception) { throw; } return(null); }
public static async Task <string> GetTokenCenAsync(string userCEN, string passwordCEN, Uri url) { Dictionary <string, string> dic = new Dictionary <string, string> { { "username", userCEN }, { "password", passwordCEN } }; try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(url, "api/token-auth/"); wc.Headers[HttpRequestHeader.ContentType] = "application/json"; string res = await wc.UploadStringTaskAsync(uri, WebRequestMethods.Http.Post, JsonConvert.SerializeObject(dic, Formatting.Indented)); // POST if (res != null) { dic = JsonConvert.DeserializeObject <Dictionary <string, string> >(res); return(dic["token"]); } } } catch (Exception) { throw; } return(null); }
public static async Task <ResultPaymentExecution> GetPayId(ResultInstruction instruction) { try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, $"/api/v1/resources/payment-execution-payment-instructions/?instruction={instruction.Id}"); wc.Headers[HttpRequestHeader.ContentType] = "application/json"; string res = await wc.DownloadStringTaskAsync(uri); // GET if (res != null) { PaymentExecution p = JsonConvert.DeserializeObject <PaymentExecution>(res, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); if (p.Count == 1) { return(p.Results[0]); } } } } catch (Exception) { throw; } return(null); }
public static async Task <List <ResultPaymentMatrix> > GetPaymentMatrixByBillingWindowIdAsync(ResultBillingWindow window) { try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, $"api/v1/resources/payment-matrices/?billing_window={window.Id}"); wc.Headers[HttpRequestHeader.ContentType] = "application/json"; string res = await wc.DownloadStringTaskAsync(uri); // GET if (res != null) { PaymentMatrix p = JsonConvert.DeserializeObject <PaymentMatrix>(res, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); foreach (ResultPaymentMatrix item in p.Results) { item.BillingWindow = window; } return(p.Results); } } } catch (Exception) { throw; } return(null); }
public static async Task <ResultAgent> GetAgetByEmailAsync(string userCEN, Uri url) { try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(url, $"api/v1/resources/agents/?email={userCEN}"); wc.Headers[HttpRequestHeader.ContentType] = "application/json"; string res = await wc.DownloadStringTaskAsync(uri); // GET if (res != null) { Agent agent = JsonConvert.DeserializeObject <Agent>(res, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); if (agent.Results.Count == 1) { return(agent.Results[0]); } } } } catch (Exception) { throw; } return(null); }
public static async Task <List <ResultPaymentMatrix> > GetPaymentMatrixAsync(DateTime date) { DateTime createdBefore = date.AddMonths(1); try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, $"api/v1/resources/payment-matrices/?created_after={string.Format("{0:yyyy-MM-dd}", date)}&created_before={string.Format("{0:yyyy-MM-dd}", createdBefore)}"); wc.Headers[HttpRequestHeader.ContentType] = "application/json"; string res = await wc.DownloadStringTaskAsync(uri); // GET if (res != null) { PaymentMatrix p = JsonConvert.DeserializeObject <PaymentMatrix>(res, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return(p.Results); } } } catch (Exception) { throw; } return(null); }
public static async Task <List <ResultPaymentMatrix> > GetPaymentMatrixAsync(int year) { try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, $"api/v1/resources/payment-matrices/?created_after={year - 1}-12-31&created_before={year + 1}-01-01"); wc.Headers[HttpRequestHeader.ContentType] = "application/json"; string res = await wc.DownloadStringTaskAsync(uri); // GET if (res != null) { PaymentMatrix p = JsonConvert.DeserializeObject <PaymentMatrix>(res, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return(p.Results); } } } catch (Exception) { throw; } return(null); }
public static async Task <ResultParticipant> GetParticipantByRutAsync(string rut) { try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, $"api/v1/resources/participants/?rut={rut}"); wc.Headers[HttpRequestHeader.ContentType] = "application/json"; string res = await wc.DownloadStringTaskAsync(uri); // GET if (res != null) { Participant p = JsonConvert.DeserializeObject <Participant>(res, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); if (p.Count > 0) { return(p.Results[0]); } } } } catch (Exception) { throw; } return(null); }
public static async Task <ResultInstruction> GetInstructionDebtorAsync(ResultPaymentMatrix matrix, ResultParticipant participant, ResultParticipant userPart) { try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, $"api/v1/resources/instructions/?payment_matrix={matrix.Id}&creditor={participant.Id}&debtor={userPart.Id}&status=Publicado"); wc.Headers[HttpRequestHeader.ContentType] = "application/json"; string res = await wc.DownloadStringTaskAsync(uri); if (res != null) { Instruction instruction = JsonConvert.DeserializeObject <Instruction>(res, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); if (instruction.Count > 0) { instruction.Results[0].PaymentMatrix = matrix; instruction.Results[0].ParticipantCreditor = participant; instruction.Results[0].ParticipantDebtor = userPart; return(instruction.Results[0]); } } } } catch (Exception) { throw; } return(null); }
public static async Task <ResultBillingWindow> GetBillingWindowByIdAsync(ResultPaymentMatrix matrix) { try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, $"api/v1/resources/billing-windows/?id={matrix.BillingWindowId}"); wc.Headers[HttpRequestHeader.ContentType] = "application/json"; string res = await wc.DownloadStringTaskAsync(uri); // GET if (res != null) { BillingWindow b = JsonConvert.DeserializeObject <BillingWindow>(res, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return(b.Results[0]); } } } catch (Exception) { throw; } return(null); }
public static async Task <List <ResultBilingType> > GetBilinTypesAsync() { try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, $"api/v1/resources/billing-types"); wc.Headers[HttpRequestHeader.ContentType] = "application/json"; string res = await wc.DownloadStringTaskAsync(uri); // GET if (res != null) { BilingType bilingType = JsonConvert.DeserializeObject <BilingType>(res, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return(bilingType.Results); } } } catch (Exception) { throw; } return(null); }
public static async Task <ResultDte> GetDteAsync(Detalle detalle, bool isCreditor) { try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, $"api/v1/resources/dtes/?reported_by_creditor={isCreditor}&folio={detalle.Folio}&instruccion={detalle.Instruction.Id}&creditor={detalle.Instruction.Creditor}"); wc.Headers[HttpRequestHeader.ContentType] = "application/json"; string res = await wc.DownloadStringTaskAsync(uri); // GET if (res != null) { Dte p = JsonConvert.DeserializeObject <Dte>(res, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); if (p.Count == 1) { return(p.Results[0]); } } } } catch (Exception) { throw; } return(null); }
private static async Task <string> SendFileAsync(string tokenCen, string fileName, string doc) { try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, "api/v1/resources/auxiliary-files/"); wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; wc.Headers[HttpRequestHeader.Authorization] = $"Token {tokenCen}"; wc.Headers.Add("Content-Disposition", "attachment; filename=" + fileName + ".xml"); string res = await wc.UploadStringTaskAsync(uri, WebRequestMethods.Http.Put, doc); // PUT if (res != null) { Dictionary <string, string> dic = JsonConvert.DeserializeObject <Dictionary <string, string> >(res); return(dic["invoice_file_id"]); } } } catch (Exception) { throw; } return(null); }
public static async Task <ResultDte> SendDteCreditorAsync(Detalle detalle, string tokenCen, string doc) { string fileName = detalle.Folio + "_" + detalle.Instruction.Id; string idFile = await SendFileAsync(tokenCen, fileName, doc); if (!string.IsNullOrEmpty(idFile)) { ResultDte dte = new ResultDte { Folio = detalle.Folio, GrossAmount = detalle.MntTotal, Instruction = detalle.Instruction.Id, NetAmount = detalle.MntNeto, ReportedByCreditor = true, TypeSiiCode = 33, EmissionDt = string.Format("{0:yyyy-MM-dd}", Convert.ToDateTime(detalle.FechaEmision)), ReceptionDt = string.Format("{0:yyyy-MM-dd}", Convert.ToDateTime(detalle.FechaRecepcion)), EmissionFile = idFile }; try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, "api/v1/operations/dtes/create/"); string d = JsonConvert.SerializeObject(dte); wc.Headers[HttpRequestHeader.Authorization] = $"Token {tokenCen}"; NameValueCollection postData = new NameValueCollection() { { "data", d } }; byte[] res = await wc.UploadValuesTaskAsync(uri, WebRequestMethods.Http.Post, postData); // POST if (res != null) { string json = Encoding.UTF8.GetString(res); InsertDTe r = JsonConvert.DeserializeObject <InsertDTe>(json, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); if (r != null) { return(r.ResultDte); } } } } catch (Exception) { throw; } } return(null); }
public static async Task <ResultBillingWindow> GetBillingWindowByNaturalKeyAsync(DTEDefTypeDocumentoReferencia referencia) { TextInfo ti = CultureInfo.CurrentCulture.TextInfo; string r1 = referencia.RazonRef.Substring(0, referencia.RazonRef.IndexOf(']') + 1).TrimStart(); string r2 = referencia.RazonRef.Substring(0, referencia.RazonRef.IndexOf(']', referencia.RazonRef.IndexOf(']') + 1) + 1); r2 = r2.Substring(r2.IndexOf(']') + 1); // Controlling lower & upper string rznRef = ti.ToTitleCase(r2.ToLower()); try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, $"api/v1/resources/billing-windows/?natural_key={r1 + rznRef}"); wc.Headers[HttpRequestHeader.ContentType] = "application/json"; string res = await wc.DownloadStringTaskAsync(uri); if (res != null) { BillingWindow b = JsonConvert.DeserializeObject <BillingWindow>(res, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); if (b.Count > 0) { return(b.Results[0]); } } } } catch (Exception) { throw; } return(null); }
public static async Task <ResultDte> SendDteDebtorAsync(Detalle detalle, string tokenCen) { ResultDte dte = new ResultDte { Folio = detalle.Folio, GrossAmount = detalle.MntTotal, Instruction = detalle.Instruction.Id, NetAmount = detalle.MntNeto, ReportedByCreditor = false, TypeSiiCode = 33, AcceptanceDt = string.Format("{0:yyyy-MM-dd}", Convert.ToDateTime(detalle.FechaRecepcion)) }; switch (detalle.StatusDetalle) { case StatusDetalle.Accepted: dte.AcceptanceStatus = 1; break; case StatusDetalle.Rejected: dte.AcceptanceStatus = 2; break; case StatusDetalle.Pending: break; default: break; } try { using (CustomWebClient wc = new CustomWebClient()) { Uri uri = new Uri(Properties.Settings.Default.UrlCen, "api/v1/operations/dtes/create/"); string d = JsonConvert.SerializeObject(dte); wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; wc.Headers[HttpRequestHeader.Authorization] = $"Token {tokenCen}"; NameValueCollection postData = new NameValueCollection() { { "data", d } }; byte[] res = await wc.UploadValuesTaskAsync(uri, WebRequestMethods.Http.Post, postData); // POST if (res != null) { string json = Encoding.UTF8.GetString(res); InsertDTe r = JsonConvert.DeserializeObject <InsertDTe>(json, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); if (r != null && r.Errors.Count == 0) { return(r.ResultDte); } } } } catch (Exception) { throw; } return(null); }