Пример #1
0
        public async System.Threading.Tasks.Task <IHttpActionResult> PostAsync(string id_order)
        {
            try
            {
                var order = _context.Orders.Find(id_order);
                if (order == null)
                {
                    return(NotFound());
                }
                JWTManagement jwtObject = new JWTManagement();
                var           token     = jwtObject.getToken();
                string        uri       = "https://sandbox-api.baokim.vn/payment/api/v4/order/detail?mrc_order_id=" + id_order + "&jwt=" + token;

                HttpClient          client   = new HttpClient();
                HttpResponseMessage response = await client.GetAsync(uri);

                HttpContent content = response.Content;
                string      data    = await content.ReadAsStringAsync();

                dynamic d    = JObject.Parse(data);
                var     stat = d.stat;

                bool rs = false;
                if (stat == "c")
                {
                    rs = true;
                    var trans = _context.Transactions.Where(x => x.id_order == id_order).ToList();
                    if (trans.Count != 0)
                    {
                        return(Ok(trans));
                    }

                    byte[] buffer   = Guid.NewGuid().ToByteArray();
                    var    trans_id = BitConverter.ToInt64(buffer, 0).ToString();
                    if (trans.Count == 0)
                    {
                        _context.Transactions.Add(new Transaction
                        {
                            id_transaction = trans_id,
                            id_order       = id_order,
                            money          = order.total_amount,
                            created_time   = DateTime.Now,
                        });
                        _context.SaveChanges();
                        return(Ok(trans));
                    }
                }


                if (rs == false)
                {
                    return(NotFound());
                }
                return(Ok());
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Пример #2
0
        public IHttpActionResult Post(OrderVM model)
        {
            JWTManagement jwtObject = new JWTManagement();
            var           _token    = jwtObject.getToken();

            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                if (model == null)
                {
                    return(NotFound());
                }

                var           address_market   = _context.Addresses.FirstOrDefault(x => x.id_address == model.id_address);
                var           address_customer = _context.Addresses.FirstOrDefault(s => s.id_address == model.customer_address_id);
                GeoCoordinate sCoor            = new GeoCoordinate(address_market.lat ?? 0, address_market.@long ?? 0);
                GeoCoordinate cCoor            = new GeoCoordinate(address_customer.lat ?? 0, address_customer.@long ?? 0);
                var           distance_s_to_m  = sCoor.GetDistanceTo(cCoor);
                double        ship_cost        = 0;
                double        _point           = 0;
                if (distance_s_to_m > 1000)
                {
                    ship_cost = distance_s_to_m * 10;
                }
                else
                {
                    ship_cost = 10000;
                }
                var stime = new TimeSpan(17, 00, 00);
                var etime = new TimeSpan(19, 00, 00);
                if ((stime.CompareTo(model.time_to_ship ?? new TimeSpan(00, 00, 00)) < 0) && (etime.CompareTo(model.time_to_ship ?? new TimeSpan(00, 00, 00)) > 0))
                {
                    ship_cost = ship_cost + 10000;
                    _point   += 2;
                }
                byte[] buffer     = Guid.NewGuid().ToByteArray();
                var    order_code = BitConverter.ToInt64(buffer, 0).ToString().Substring(9);


                if (distance_s_to_m > 1000)
                {
                    _point += distance_s_to_m / 100;
                }
                else
                {
                    _point += 10;
                }

                ship_cost = (int)ship_cost;
                int nguyen = (int)ship_cost / 1000;
                int du     = (int)ship_cost % 1000;
                if (du < 500 && du > 0)
                {
                    ship_cost = (nguyen * 1000) + 500;
                }
                else
                {
                    ship_cost = (nguyen * 1000) + 1000;
                }

                distance_s_to_m = distance_s_to_m / 1000;
                distance_s_to_m = double.Parse(distance_s_to_m.ToString("0.0"));

                var    list_order_details = model.OrderDetails.ToList();
                double total = 0;
                foreach (var item in list_order_details)
                {
                    total += item.price * item.quanlity ?? 0;
                }
                total = total + ship_cost + 10000;
                DateTime dt    = DateTime.Now;
                var      order = _context.Orders.Add(new Models.Order
                {
                    id_order            = model.id_order,
                    created_date        = dt,
                    id_customer         = model.id_customer,
                    id_shipper          = model.id_shipper,
                    state               = 0,
                    id_group_image      = model.id_group_image,
                    dis_cus_to_market   = distance_s_to_m,
                    id_address          = model.id_address,
                    point               = (int)_point,
                    shipping_cost       = ship_cost,
                    time_to_ship        = model.time_to_ship.Value,
                    customer_address_id = model.customer_address_id,
                    phone               = model.phone,
                    name             = model.name,
                    taking_time      = null,
                    done_time        = null,
                    order_code       = order_code,
                    customer_comment = model.customer_comment,
                    system_cost      = 10000,
                    total_amount     = total,
                    token            = _token,
                    OrderDetails     = model.OrderDetails.Select(x => new OrderDetail
                    {
                        id_orderdetail = x.id_orderdetail,
                        id_order       = x.id_order,
                        id_product     = x.id_product,
                        id_market      = x.id_market,
                        price          = x.price,
                        quanlity       = x.quanlity,
                        priority       = x.priority
                    }).ToList()
                });
                _context.SaveChanges();
                return(Ok(order));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Пример #3
0
        public IHttpActionResult PostCost(CalculateVM model)
        {
            JWTManagement jwtObject = new JWTManagement();
            var           _token    = jwtObject.getToken();

            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                if (model == null)
                {
                    return(NotFound());
                }

                var           address_market   = _context.Addresses.FirstOrDefault(x => x.id_address == model.id_address);
                var           address_customer = _context.Addresses.FirstOrDefault(s => s.id_address == model.customer_address_id);
                GeoCoordinate sCoor            = new GeoCoordinate(address_market.lat ?? 0, address_market.@long ?? 0);
                GeoCoordinate cCoor            = new GeoCoordinate(address_customer.lat ?? 0, address_customer.@long ?? 0);
                var           distance_s_to_m  = sCoor.GetDistanceTo(cCoor);
                double        ship_cost        = 0;
                double        _point           = 0;
                if (distance_s_to_m > 1000)
                {
                    ship_cost = distance_s_to_m * 10;
                }
                else
                {
                    ship_cost = 10000;
                }
                var stime = new TimeSpan(17, 00, 00);
                var etime = new TimeSpan(19, 00, 00);
                if ((stime.CompareTo(model.time_to_ship ?? new TimeSpan(00, 00, 00)) < 0) && (etime.CompareTo(model.time_to_ship ?? new TimeSpan(00, 00, 00)) > 0))
                {
                    ship_cost = ship_cost + 10000;
                    _point   += 2;
                }
                byte[] buffer     = Guid.NewGuid().ToByteArray();
                var    order_code = BitConverter.ToInt64(buffer, 0).ToString().Substring(9);


                if (distance_s_to_m > 1000)
                {
                    _point += distance_s_to_m / 100;
                }
                else
                {
                    _point += 10;
                }

                ship_cost = (int)ship_cost;
                int nguyen = (int)ship_cost / 1000;
                int du     = (int)ship_cost % 1000;
                if (du < 500 && du > 0)
                {
                    ship_cost = (nguyen * 1000) + 500;
                }
                else
                {
                    ship_cost = (nguyen * 1000) + 1000;
                }

                distance_s_to_m = distance_s_to_m / 1000;
                distance_s_to_m = double.Parse(distance_s_to_m.ToString("0.0"));

                var    list_order_details = model.OrderDetails.ToList();
                double total = 0;
                foreach (var item in list_order_details)
                {
                    total += item.price * item.quanlity ?? 0;
                }
                double total_amount = 0;
                total_amount = total + ship_cost + 10000;

                DateTime dt = DateTime.Now;

                var CostObject = new
                {
                    shippingcost     = ship_cost,
                    system_cost      = 10000,
                    total            = total,
                    total_amount_all = total_amount,
                };
                return(Ok(CostObject));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }