using System; using System.Linq; using System.Text; using System.Collections.Specialized; using Newtonsoft.Json; using bdAsegasa.dbcontext; namespace bdAsegasa.db { public partial class pagostelematicos { public string CodigoRecibo => this.idReciboNavigation?.CodigoRecibo ?? ""; public static pagostelematicos GeneraPagoTelematico(tscgestionasegasa bd, OrigenPagoEnum origenPago, string referencia, string emailConfirmacion, string telefono, double importe, string dni = "", int? idRecibo = null) { try { if (idRecibo.HasValue && !bd.recibos.Any(x => x.idRecibo == idRecibo.Value)) throw new Exception("No se encuentra el recibo con id: " + idRecibo.Value); var random = new Random(); var np = new pagostelematicos(); np.DNI = dni?.Trim().ToUpper(); np.Referencia = referencia?.Trim().ToUpper(); np.EmailConfirmacionPago = emailConfirmacion?.Trim().ToLower(); np.Telefono = telefono; np.Importe = Math.Round(importe, 2); np.FechaCreacion = DateTime.Now; np.idRecibo = idRecibo; np.OrigenPago = (int)origenPago; np.CodigoConfirmacionPago = random.Next(100000000, 999999999).ToString(); bd.pagostelematicos.Add(np); bd.SaveChanges(); return np; } catch (Exception ex) { throw new Exception(ex.Message, ex); } } } public enum OrigenPagoEnum { PAGO_GENERICO = 0, PAGO_RECIBO = 1 } public class RespuestaRedsysPOST { public string Ds_SignatureVersion { get; set; } public string Ds_MerchantParameters { get; set; } public string Ds_Signature { get; set; } public ConfirmacionRedsys Confirmacion { get; set; } public RespuestaRedsysPOST() { } public RespuestaRedsysPOST(NameValueCollection datosPOST) { foreach (string key in datosPOST.AllKeys) { if (key == "Ds_SignatureVersion") this.Ds_SignatureVersion = datosPOST[key]; if (key == "Ds_MerchantParameters") this.Ds_MerchantParameters = datosPOST[key]; if (key == "Ds_Signature") this.Ds_Signature = datosPOST[key]; } if (!string.IsNullOrEmpty(Ds_MerchantParameters)) { byte[] bDatos = Convert.FromBase64String(Ds_MerchantParameters); string sDatos = Encoding.UTF8.GetString(bDatos); this.Confirmacion = JsonConvert.DeserializeObject(sDatos); } } } public class ConfirmacionRedsys { public string Ds_Date { get; set; } public string Ds_Hour { get; set; } public string Ds_Amount { get; set; } public string Ds_Currency { get; set; } public string Ds_Order { get; set; } public string Ds_MerchantCode { get; set; } public string Ds_Terminal { get; set; } public string Ds_Response { get; set; } public string Ds_MerchantData { get; set; } public string Ds_SecurePayment { get; set; } public string Ds_TransactionType { get; set; } public string Ds_Card_Country { get; set; } public string Ds_AuthorisationCode { get; set; } public string Ds_ConsumerLanguage { get; set; } public string Ds_Card_Type { get; set; } private const string SeparadorFecha = "%2F"; private const string SeparadorHora = "%3A"; public DateTime Ds_Date_ComoDate { get { var parts = Ds_Date.Split(new[] { SeparadorFecha }, StringSplitOptions.None); return new DateTime(int.Parse(parts[2]), int.Parse(parts[1]), int.Parse(parts[0])); } } public TimeSpan Ds_Hour_ComoTimeSpan { get { var parts = Ds_Hour.Split(new[] { SeparadorHora }, StringSplitOptions.None); return new TimeSpan(int.Parse(parts[0]), int.Parse(parts[1]), 0); } } public double Ds_Amount_ComoDouble => double.Parse(Ds_Amount) / 100.0; public bool Ds_Response_ComoBoolean { get { if (uint.TryParse(Ds_Response, out uint code)) return code >= 0 && code <= 99; return false; } } public bool Ds_SecurePayment_ComoBoolean => Ds_SecurePayment == "1"; public RedsysTipoTarjeta Ds_Card_Type_ComoEnum { get { if (Ds_Card_Type == "C") return RedsysTipoTarjeta.CREDITO; if (Ds_Card_Type == "D") return RedsysTipoTarjeta.DEBITO; return RedsysTipoTarjeta.NO_PROPORCIONADO; } } public DateTime FechaHora { get { var d = Ds_Date_ComoDate; var t = Ds_Hour_ComoTimeSpan; return new DateTime(d.Year, d.Month, d.Day, t.Hours, t.Minutes, 0); } } } public enum RedsysTipoTarjeta { NO_PROPORCIONADO = 0, CREDITO = 1, DEBITO = 2 } }