public IHttpActionResult DeleteOrder(Order or) { using (var context = new TasteContext()) { var order = context.Orders.FirstOrDefault(o => o.OrderId == or.OrderId); var items = context.OrderedDishes.Where(od => od.OrderId == or.OrderId).ToList(); if (order != null && !order.Paid) { context.Orders.Remove(order); context.SaveChanges(); context.OrderedDishes.RemoveRange(items); context.SaveChanges(); } return(Ok()); } }
public IHttpActionResult PayInCashOrCredit(PaymentRequestModel model) { using (var context = new TasteContext()) { var order = context.Orders.FirstOrDefault(o => o.OrderId == model.OrderId); if (order != null) { double total = 0; var orderItems = context.OrderedDishes.Where(o => o.OrderId == model.OrderId && o.UserId == model.UserId); foreach (var od in orderItems) { var item = context.Dishes.FirstOrDefault(it => it.Id == od.DishId); if (item == null) { continue; } total += item.Price * od.Quantity; } order.Paid = true; order.TotalInPennies = Convert.ToInt32(total * 100); order.TaxInPennies = Convert.ToInt32(order.TotalInPennies * 0.0925); order.TipInPennies = 0; context.SaveChanges(); } return(Ok()); } }
public IHttpActionResult AddDishInformation([FromBody] Dish dish) { using (var context = new TasteContext()) { if (dish == null) { return(Ok("dish is null")); } Dish d = new Dish { RestaurantId = dish.RestaurantId, CuisineId = dish.CuisineId, Name = dish.Name, Description = dish.Description, Flavors = dish.Flavors, Ingredients = dish.Ingredients, Category = dish.Category, Price = dish.Price, Image = dish.Image, Deleted = false }; context.SaveChanges(); return(Ok("Save success")); } }
public IHttpActionResult CreateLogin([FromBody] Signup signup) { Console.Write(signup); using (var context = new TasteContext()) { if (context.Admins.Where(a => a.Username.Equals(signup.username)).ToList().Count() > 0) { return(Unauthorized()); } context.Admins.Add(new Admins() { Username = signup.username, Password = signup.password, RestaurantName = signup.name }); var res = context.Restaurants.ToList(); context.Restaurants.Add(new Restaurant() { CloverId = signup.cloverId, Name = signup.name, Location = signup.location, Phone = signup.phone, Owner = signup.owner, Image = "", AccessToken = signup.accessToken, ExchangeRate = 6.5, IsSandbox = false }); context.SaveChanges(); } return(Json("Success")); }
public IHttpActionResult EditDishInformation([FromBody] Dish dish) { using (var context = new TasteContext()) { var retrievedDish = context.Dishes.FirstOrDefault(d => d.Id == dish.Id); if (retrievedDish == null) { return(Ok()); } //retrievedDish.RestaurantId = dish.RestaurantId; retrievedDish.CuisineId = dish.CuisineId; //retrievedDish.Name = dish.Name; retrievedDish.Description = dish.Description; retrievedDish.Flavors = dish.Flavors; retrievedDish.Ingredients = dish.Ingredients; retrievedDish.Category = dish.Category; //retrievedDish.Price = dish.Price; retrievedDish.Image = dish.Image; //retrievedDish.Deleted = dish.Deleted; //retrievedDish.CloverId = dish.CloverId; context.SaveChanges(); return(Ok("Save success")); } }
public IHttpActionResult LoginAccount(string username, string password, string fromClover = "false") { using (var context = new TasteContext()) { var user = context.Admins.FirstOrDefault(r => r.Username == username); if (user == null) { return(Unauthorized()); } if (fromClover.Equals("false")) { // check password, else just let it go if (password.Equals(user.Password)) { return(Ok("Success")); } else { return(Unauthorized()); } } else { // update password and restaurant's accesstoken user.Password = password; var res = context.Restaurants.FirstOrDefault(r => r.CloverId.Equals(username)); res.AccessToken = password; context.SaveChanges(); return(Ok("Success")); } } }
public IHttpActionResult AddDeposit([FromBody] Deposit deposits) { using (var context = new TasteContext()) { context.Deposits.Add(deposits); context.SaveChanges(); return(Ok("Save success")); } }
public async Task <IHttpActionResult> SyncWithClover() { using (var context = new TasteContext()) { var restaurants = context.Restaurants.ToList(); foreach (var restaurant in restaurants) { var restaurantId = restaurant.Id; var result = await CloverClient.GetItemsAsync(restaurant.AccessToken, restaurant.CloverId, restaurant.IsSandbox); var existingMenu = context.Dishes.Where(d => !d.Deleted && d.RestaurantId == restaurantId).ToList(); var ids = existingMenu.Select(d => d.CloverId).ToList(); ids.RemoveAll(s => s == null); if (result != null) { var newMenu = result.Select(r => r.Id).ToList(); var itemsToBeDeleted = ids.Where(m => !newMenu.Contains(m)).ToList(); var itemsToBeAdded = newMenu.Where(m => !ids.Contains(m)).ToList(); foreach (var i in itemsToBeAdded) { var r = result.First(re => re.Id == i); context.Dishes.Add(new Dish { Name = r.Name, Price = r.Price / 100.0, CuisineId = -1, RestaurantId = restaurantId, CloverId = r.Id, Deleted = false }); } foreach (var i in itemsToBeDeleted) { var r = existingMenu.First(re => re.CloverId == i); context.Dishes.Remove(r); } context.SaveChanges(); } else { // nothing } } } return(Ok("Success")); }
public IHttpActionResult PayTips(int restaurantId) { using (var context = new TasteContext()) { var orders = context.Orders.Where(o => o.RestaurantId == restaurantId).ToList(); foreach (var o in orders) { o.TipInPennies = 0; } context.SaveChanges(); } return(Json("Success")); }
public IHttpActionResult PayOutstandingBalance(int restaurantId, int amount) { using (var context = new TasteContext()) { var restaurant = context.Restaurants.FirstOrDefault(r => r.Id == restaurantId); if (restaurant == null) { return(NotFound()); } restaurant.OustandingBalance -= amount; context.SaveChanges(); return(Ok()); } }
public async Task <IHttpActionResult> CompletePayment(WechatPaymentNotificationModel notification) { if (notification == null || !notification.IsValid()) { return(Ok(GetErrorModel(ApiErrorCode.InvalidData))); } using (var context = new TasteContext()) { var payment = context.Payments.FirstOrDefault(o => o.TransactionId == notification.TransactionId); if (payment == null || payment.Success) { return(Ok()); } var order = context.Orders.FirstOrDefault(o => o.OrderId == payment.OrderId && o.UserId == payment.UserId); if (order == null) { return(Ok()); } var restaurant = context.Restaurants.FirstOrDefault(r => r.Id == order.RestaurantId); if (restaurant == null) { return(Ok()); } var merchantId = ConfigurationManager.AppSettings.Get("WeChatMerchantId"); var merchantKey = ConfigurationManager.AppSettings.Get("WeChatMerchantKey"); var paymentMade = await WechatPayHttpClient.CheckIfPaymentIsPaidAsync(payment.TransactionId, ConfigurationManager.AppSettings.Get("WeChatMiniProgramAppId"), merchantId, merchantKey); if (!paymentMade) { return(Ok(GetErrorModel(ApiErrorCode.PaymentNotMade))); } payment.Success = true; order.Paid = true; restaurant.OustandingBalance += Convert.ToInt32(notification.Total / restaurant.ExchangeRate); context.SaveChanges(); return(Ok()); } }
public IHttpActionResult EditRestaurantInfo([FromBody] Restaurant restaurant) { using (var context = new TasteContext()) { var res = context.Restaurants.FirstOrDefault(r => r.CloverId == restaurant.CloverId); if (res != null) { res.Location = restaurant.Location; res.Name = restaurant.Name; res.Owner = restaurant.Owner; res.Phone = restaurant.Phone; context.SaveChanges(); return(Json("Success")); } else { return(Ok(GetErrorModel(ApiErrorCode.RestaurantDoesNotExist))); } } }
public async Task <IHttpActionResult> Login(string code) { if (code.IsNullOrWhiteSpace()) { return(Ok(GetErrorModel(ApiErrorCode.SessionExpired))); } var session = await WeChatHttpClient.GetSessionAsync(code, ConfigurationManager.AppSettings.Get("WeChatMiniProgramAppId"), ConfigurationManager.AppSettings.Get("WeChatMiniProgramAppSecret")); using (var context = new TasteContext()) { var user = context.Users.FirstOrDefault(u => string.Equals(u.UserId, session.OpenId)); if (user != null) { return(Ok(user.UserId)); } user = context.Users.Add(new User { Name = session.OpenId, UserId = session.OpenId }); context.SaveChanges(); return(Ok(user.UserId)); } }
public async Task <IHttpActionResult> SubmitOrder(OrderRequestModel model) { using (var context = new TasteContext()) { var user = context.Users.FirstOrDefault(u => u.UserId == model.UserId); if (user == null) { return(Ok(GetErrorModel(ApiErrorCode.UserDoesNotExist))); } var restaurant = context.Restaurants.FirstOrDefault(r => r.Id == model.RestaurantId); if (restaurant == null) { return(Ok(GetErrorModel(ApiErrorCode.RestaurantDoesNotExist))); } string orderId; var order = context.Orders.FirstOrDefault(o => !o.Paid && o.RestaurantId == model.RestaurantId && o.UserId == model.UserId); if (order != null) { orderId = order.OrderId; } else { orderId = $"{user.UserId} {DateTime.Now}"; context.Orders.Add(new Order { Datetime = DateTime.Now, OrderId = orderId, Paid = false, RestaurantId = model.RestaurantId, TableName = model.Table.ToString(), UserId = user.UserId, Details = "" }); } var cloverLineItems = new List <CloverLineItemModel>(); foreach (var i in model.Items) { var dish = context.Dishes.FirstOrDefault(d => d.Id == i.DishId); if (dish == null) { continue; } cloverLineItems.Add(new CloverLineItemModel { PriceInPennies = Convert.ToInt32(dish.Price * 100), Name = dish.Name, Printed = true, UnitQuantity = i.Quantity }); context.OrderedDishes.Add(new OrderedDish { DishId = i.DishId, Quantity = i.Quantity, UserId = user.UserId, OrderId = orderId, CuisineId = dish.CuisineId.GetValueOrDefault() }); var pref = context.Preferences.FirstOrDefault(p => p.UserId == user.UserId && p.CuisineId == dish.CuisineId); if (pref != null) { pref.Count += i.Quantity; } else { context.Preferences.Add(new Preference { Count = i.Quantity, CuisineId = dish.CuisineId, UserId = user.UserId }); } } //send printing request to clover if (!restaurant.AccessToken.IsNullOrWhiteSpace()) { await CloverClient.CreateOrderAsync(cloverLineItems, restaurant.AccessToken, restaurant.CloverId, restaurant.IsSandbox); } context.SaveChanges(); return(Ok(orderId)); } }
public async Task <IHttpActionResult> InitializePayment(PaymentRequestModel model) { using (var context = new TasteContext()) { var order = context.Orders.FirstOrDefault(o => o.OrderId == model.OrderId && o.UserId == model.UserId); if (order == null || order.Paid) { return(Ok(GetErrorModel(ApiErrorCode.OrderDoesNotExist))); } var restaurant = context.Restaurants.FirstOrDefault(r => r.Id == order.RestaurantId); if (restaurant == null) { return(Ok(GetErrorModel(ApiErrorCode.RestaurantDoesNotExist))); } const string body = "豪吃"; const string signType = "MD5"; var appId = ConfigurationManager.AppSettings.Get("WeChatMiniProgramAppId"); var merchantId = ConfigurationManager.AppSettings.Get("WeChatMerchantId"); var merchantKey = ConfigurationManager.AppSettings.Get("WeChatMerchantKey"); var notifyUrl = $"{RequestBaseUrl}/api/v1/payment/complete"; var userAddress = HttpContext.Current.Request.UserHostAddress; var nonce = Guid.NewGuid().ToString().Replace("-", ""); var timeStamp = DateTime.Now.SecondsSinceEpoch().ToString(); var transactionId = Guid.NewGuid().ToString().Replace("-", ""); double total = 0; var orderItems = context.OrderedDishes.Where(o => o.OrderId == model.OrderId && o.UserId == model.UserId); foreach (var od in orderItems) { var item = context.Dishes.FirstOrDefault(it => it.Id == od.DishId); if (item == null) { continue; } total += item.Price * od.Quantity; } context.Payments.Add(new Payment { Success = false, TransactionId = transactionId, CreationDate = DateTimeExtensions.BeiJingNow(), UserId = model.UserId, OrderId = model.OrderId }); order.TipInPennies = model.TipInPennies; order.TotalInPennies = Convert.ToInt32(total * 100); order.TaxInPennies = Convert.ToInt32(order.TotalInPennies * 0.0925); context.SaveChanges(); var subTotalRmb = Convert.ToInt32(order.TotalInPennies * restaurant.ExchangeRate); var tipRmb = Convert.ToInt32(model.TipInPennies * restaurant.ExchangeRate); var taxInPenniesRmb = Convert.ToInt32(order.TaxInPennies * restaurant.ExchangeRate); var orderTotalRmb = subTotalRmb + tipRmb + taxInPenniesRmb; var result = await WechatPayHttpClient.GetPaymentInfo(userAddress, notifyUrl, appId, orderTotalRmb, WechatTradeType.JSAPI, transactionId, body, merchantId, model.UserId, merchantKey); var paymentData = new WechatPaymentModel { Body = body, NotifyUrl = notifyUrl, TransactionId = transactionId, TotalAmountInPennies = orderTotalRmb, PrepayId = result.PrepayId, PaySign = WechatMd5SignGenerator.GetPaymentSignMd5Hash(appId, timeStamp, nonce, result.PrepayId, signType, merchantKey), Nonce = nonce, SignType = signType, TimeStamp = timeStamp, WebUrl = result.WebUrl }; return(Ok(paymentData)); } }