public ActionResult Notification()
        {
            string topic = Request["topic"];
            string id    = Request["id"];

            MP mp = new MP(ConfigurationManager.AppSettings["MPClientID"], ConfigurationManager.AppSettings["MPSecret"]);

            mp.sandboxMode(bool.Parse(ConfigurationManager.AppSettings["MPSandbox"]));

            Hashtable paymentInfo = mp.getPaymentInfo(id);

            //NotifyUserOrderStatus();
            //NotifyBuyerOrderStatus();

            //NotifyUserOrderStatus();

            string Status = ((Hashtable)((Hashtable)paymentInfo["response"])["collection"])["status"].ToString();


            return(Json(new { status = "OK" }, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 2
0
        public ActionResult PaymentNotification(string topic, string id)
        {
            // URL Notificacion
            // http://localhost:64615/Sell/PaymentNotification/?topic=payment&id=4205580523

            try
            {
                if (topic == "payment")
                {
                    MP mp = new MP(srvConfig.MP_client_id(), srvConfig.MP_client_secret());

                    // get info pago --> "Payment"
                    Hashtable Payment = mp.getPaymentInfo(id);
                    Payment = ((Hashtable)Payment["response"]);
                    string orderID = ((Hashtable)Payment["collection"])["order_id"].ToString();
                    if (((Hashtable)Payment["collection"])["status"].ToString() == "approved")
                    {
                        // get info order --> "Order"
                        // https://api.mercadopago.com/merchant_orders/857112399?access_token=APP_USR-3825884689807039-100215-028a54b7ff4b3017ab7830f35784a693-357350759
                        string baseUrl = "https://api.mercadopago.com";
                        string url     = "merchant_orders/" + orderID + "?access_token=" + srvConfig.MP_client_access_token();
                        using (var client = new HttpClient())
                        {
                            client.BaseAddress = new Uri(baseUrl);
                            var responseTask = client.GetAsync(url);
                            responseTask.Wait();
                            var result = responseTask.Result;
                            if (result.IsSuccessStatusCode)
                            {
                                var readTask = result.Content.ReadAsStringAsync();
                                readTask.Wait();
                                JObject Order = JObject.Parse(readTask.Result);

                                if (Order["status"].ToString() == "closed")
                                {
                                    Venta oVenta = new Venta();
                                    //oVenta.emailComprador
                                    oVenta.MP_state_shipments = "";
                                    oVenta.MP_order_id        = Order["id"].ToString();
                                    oVenta.MP_state_order     = Order["status"].ToString();
                                    oVenta.fecha      = DateTime.Now;
                                    oVenta.montoTotal = Convert.ToInt32(Order["total_amount"].ToString());

                                    if (Order["shipments"].ToString() != null)
                                    {
                                        JToken Shipment         = Order["shipments"].First;
                                        JToken receiver_address = Shipment["receiver_address"];
                                        JToken city             = receiver_address["city"];
                                        JToken state            = receiver_address["state"];
                                        oVenta.direccionShipments = receiver_address["address_line"].ToString() + ", " + city["name"].ToString() + ", " + state["name"].ToString() + ", CP:" + receiver_address["zip_code"].ToString();
                                        oVenta.MP_state_shipments = Shipment["status"].ToString();
                                        // oVenta.idLocalidad = city["name"].ToString();
                                    }

                                    DetalleVenta oDetalleVenta;
                                    JToken       items = Order["items"];
                                    foreach (JToken item in items)
                                    {
                                        oDetalleVenta                = new DetalleVenta();
                                        oDetalleVenta.idProducto     = Convert.ToInt32(item["id"]);
                                        oDetalleVenta.cantidad       = Convert.ToInt32(item["quantity"]);
                                        oDetalleVenta.precioUnitario = Convert.ToInt32(item["unit_price"]);
                                        oDetalleVenta.subTotal       = oDetalleVenta.cantidad * oDetalleVenta.precioUnitario;
                                        oDetalleVenta.descuento      = Convert.ToInt32(item["category_id"]);
                                        oVenta.DetalleVenta.Add(oDetalleVenta);
                                    }
                                    // Guardar
                                    srvVentas sVenta = new srvVentas();
                                    sVenta.GuardarVenta(oVenta);
                                }
                            }
                        }
                    }
                }
                return(View());
            }
            catch (Exception)
            {
                throw new Exception();
            }
        }
Exemplo n.º 3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            MP mp = new MP("7071654091217780", "F4SUQfv2CA4YUvPj0VsFROGywMkcYvyC");

            mp.sandboxMode(false); // cambiar por mp.sandboxMode(false);
            string Nombre_de_archivo = Request["id"].ToString();

            JObject payment_info = mp.getPaymentInfo(Nombre_de_archivo);

            if (Nombre_de_archivo.Length == 9) // trato de capturar la respuesta y sacarla del juego enviada desde MP
            {
                Response.Write(payment_info["response"]);
                return;
            }
            else // trato de capturar la primera respuesta y trabajarla
            {

                //Preparando para enviar al correo electrónico
                MailMessage correo = new MailMessage();
                correo.From = new MailAddress("*****@*****.**");
                correo.To.Add("*****@*****.**");

                SmtpClient smtp = new SmtpClient("smtp.gmail.com");
                smtp.Port = 587;

                smtp.Credentials = new NetworkCredential("*****@*****.**", "qsoiqzuliwweyeog");

                smtp.EnableSsl = true;

                string cuerpo = "";

                string valorar = "";

                string mensaje = payment_info["response"].ToString();

                string reemplazo_llave_abierto = mensaje.Replace("{", "");
                string reemplazo_llave_cerrado = reemplazo_llave_abierto.Replace("}", "");
                string reemplazo_comillas = reemplazo_llave_cerrado.Replace("\"", "");
                string reemplazo_espacios = reemplazo_comillas.Replace(" ", "");

                string[] descomponer = reemplazo_comillas.Split(',');

                string[] Archivo = reemplazo_espacios.Split(':');

                string[] lista = Archivo[3].Split(',');

                for (int i = 0; i <= descomponer.Length - 1; i++)
                {

                    string linea = descomponer[i];

                    string[] dividir = linea.Split(':');

                    string[] temporal = new string[3];

                    if (dividir != null)
                    {

                        Array.Copy(dividir, temporal, Math.Min(dividir.Length, temporal.Length));

                    }

                    dividir = temporal;

                    cuerpo = cuerpo + dividir[0] + ":\t" + dividir[1] + ":\t" + dividir[2] + "\n";

                    if (i == 3)
                    {
                        valorar = temporal[2];
                    }

                }

                try
                {

                    if (valorar == null) // no confirmado el pago
                    {
                        correo.Subject = "NO Confirmado MercadoPago - DUDA - " + DateTime.Now;
                        smtp.Send("*****@*****.**", "*****@*****.**", correo.Subject, "MercadoPago no aprovó: " + Nombre_de_archivo + "\n" + cuerpo);

                        StreamWriter web = File.CreateText(Server.MapPath("~/desaprovado/" + Nombre_de_archivo + ".html"));

                        web.Write(reemplazo_comillas);

                        web.Flush();
                        web.Close();
                    }
                    else  // confirmado el pago
                    {
                        correo.Subject = "Confirmacion MercadoPago - Bien - " + DateTime.Now;
                        smtp.Send("*****@*****.**", "*****@*****.**", correo.Subject, "MercadoPago está OK: " + Nombre_de_archivo + "\n" + cuerpo);

                        StreamWriter web = File.CreateText(Server.MapPath("~/OK/" + Nombre_de_archivo + ".html"));

                        web.Write(reemplazo_comillas);

                        web.Flush();
                        web.Close();

                    }

                }
                catch (Exception)
                {

                    return;

                }

            }
        }
Exemplo n.º 4
0
        public ActionResult NotificacionPago(string topic, string id)
        {
            srvTest sTest = new srvTest();

            // URL Notificacion
            // http://localhost:64615/Home/NotificacionPago/?topic=payment&id=4205580523



            test oTest = new test();

            oTest.result = "topic:" + topic + " - id:" + id;
            sTest.AddTest(oTest);
            try
            {
                if (topic == "payment")
                {
                    MP        mp      = new MP("3825884689807039", "2aLAWWtUxSs4ZbXjSXQRVilQCG1RdSlz");
                    Hashtable Payment = mp.getPaymentInfo(id);
                    Payment = ((Hashtable)Payment["response"]);
                    string orderID = ((Hashtable)Payment["collection"])["order_id"].ToString();
                    //if (((Hashtable)Payment["collection"])["status"].ToString() == "status correcto")
                    //{



                    oTest        = new test();
                    oTest.result = "order_id:" + orderID + " - status:" + ((Hashtable)Payment["collection"])["status"].ToString();
                    sTest.AddTest(oTest);



                    string baseUrl = "https://api.mercadopago.com";
                    string url     = "merchant_orders/" + orderID + "?access_token=APP_USR-3825884689807039-100215-028a54b7ff4b3017ab7830f35784a693-357350759";
                    using (var client = new HttpClient())
                    {
                        client.BaseAddress = new Uri(baseUrl);
                        var responseTask = client.GetAsync(url);
                        responseTask.Wait();
                        var result = responseTask.Result;
                        if (result.IsSuccessStatusCode)
                        {
                            var readTask = result.Content.ReadAsStringAsync();
                            readTask.Wait();

                            JObject order = JObject.Parse(readTask.Result);
                            // verificar status de la orden
                            //if(order.GetValue("status").ToString() == "status correcto")
                            //{
                            Newtonsoft.Json.Linq.JToken items = order.GetValue("items");

                            oTest        = new test();
                            oTest.result = "items:" + items + " - status:" + order.GetValue("status").ToString() + " - total_amount:" + order.GetValue("total_amount").ToString();
                            sTest.AddTest(oTest);

                            //}
                        }
                    }

                    //}
                }
                // return new HttpStatusCodeResult(HttpStatusCode.OK);
                return(View());
            }
            catch (Exception)
            {
                // return new HttpStatusCodeResult(HttpStatusCode.OK);
                return(View());
            }
        }