public ActionResult CreatePurchase(FormCollection collection) { string nonceFromTheClient; int menuID; dynamic basketItems; int tableNumber; string comment = ""; //Validate Parameters try { //Get post fields nonceFromTheClient = collection["payment_method_nonce"]; menuID = Convert.ToInt32(collection["menu-id"]); basketItems = JsonConvert.DeserializeObject(collection["basket-items"]); tableNumber = Convert.ToInt32(collection["table-number"]); comment = Convert.ToString(collection["basket-notes"]); } catch { TempData["Error"] = "Missing Parameters"; return(RedirectToAction("Error")); } if (tableNumber < 1) { TempData["Error"] = "Invalid Table Number"; return(RedirectToAction("Error")); } if (menuID < 1) { TempData["Error"] = "Invalid Reference To Menu"; return(RedirectToAction("Error")); } if (comment.Length > 30) { comment = comment.Substring(0, 30); } //Is menu open var menuTimes = Helpers.MenuContentHelper.menuTimes(menuID); if (!menuTimes.isOpen) { TempData["Error"] = "We could not take your order because we are closed"; return(RedirectToAction("Error")); } //Create braintree object BrainTree brain = new BrainTree(Convert.ToInt32(collection["menu-id"])); //Find menu prices var PriceDictionary = Helpers.BrainTreeHelper.getPriceDictionary(menuID); if (!PriceDictionary.Success) { TempData["Error"] = "Unable to confirm prices, we were unable to complete the translation"; return(RedirectToAction("Error")); } //Check that pricing and item names are correct decimal trustedTotal = 0; List <OrderItemModel> trustedOrderItems = new List <OrderItemModel>(); try{ foreach (var item in basketItems) { var itemLookup = PriceDictionary.PriceDictionary[Convert.ToInt32(item.id)]; if (Convert.ToDecimal(Convert.ToString(item.price).Substring(1)) == itemLookup.price && item.name == itemLookup.name) { //Create new verifyed item for order OrderItemModel orderItemType = new OrderItemModel(); orderItemType.id = Convert.ToInt32(item.id); orderItemType.name = itemLookup.name; orderItemType.pricePerUnit = Convert.ToDecimal(Convert.ToString(item.price).Substring(1)); orderItemType.qty = Convert.ToInt32(item.qty); trustedOrderItems.Add(orderItemType); //Add to order total trustedTotal += Convert.ToDecimal(Convert.ToString(item.price).Substring(1)) * Convert.ToInt32(item.qty); } else { //return error when item info don't match server info TempData["Error"] = "Pricing error, we were unable to complete the translation"; return(RedirectToAction("Error")); } } } catch { TempData["Error"] = "Your basket items seem to be damaged, we were unable to complete the translation "; return(RedirectToAction("Error")); } var request = new TransactionRequest { Amount = trustedTotal, PaymentMethodNonce = nonceFromTheClient, Options = new TransactionOptionsRequest { SubmitForSettlement = true } }; var gateway = brain.CreateGateway(); Result <Transaction> result = gateway.Transaction.Sale(request); int newOrderID; if (result.IsSuccess()) { //Attempt to create order MySqlConnection connection = new MySqlConnection(Helpers.ConfigHelper.connectionString); try { connection.Open(); string query = "CALL createOrder(@transactionID,@menuID,@tableNumber,@itemsJSON)"; MySqlCommand command = new MySqlCommand(query, connection); command.Parameters.AddWithValue("@transactionID", result.Target.Id); command.Parameters.AddWithValue("@menuID", menuID); command.Parameters.AddWithValue("@tableNumber", tableNumber); command.Parameters.AddWithValue("@itemsJSON", JsonConvert.SerializeObject(trustedOrderItems)); newOrderID = Convert.ToInt32(command.ExecuteScalar()); connection.Close(); } catch { connection.Close(); //Attempt to create order again try//retry { connection.Open(); string query = "CALL createOrder(@transactionID,@menuID,@tableNumber,@itemsJSON,@comment)"; MySqlCommand command = new MySqlCommand(query, connection); command.Parameters.AddWithValue("@transactionID", result.Target.Id); command.Parameters.AddWithValue("@menuID", menuID); command.Parameters.AddWithValue("@tableNumber", tableNumber); command.Parameters.AddWithValue("@itemsJSON", JsonConvert.SerializeObject(trustedOrderItems)); command.Parameters.AddWithValue("@comment", comment); newOrderID = Convert.ToInt32(command.ExecuteScalar()); connection.Close(); } catch { //Could not create order TempData["Error"] = $"A Serious Error has occured, a transaction of £{trustedTotal} was made but your order was unable to be created. Please provide the transaction id {result.Target.Id} to a member of staff."; return(RedirectToAction("Error")); } } //Send order to valid kitchen order displays var OrderDisplayHub = GlobalHost.ConnectionManager.GetHubContext <OrderDisplayHub>(); foreach (WebSocketClientModel client in OrderDisplayClients.WebSocketClients) { if (client.menuID == menuID) //Only sent to displays of the same menuID { OrderDisplayHub.Clients.Client(client.connectionID).order(newOrderID, result.Target.Id, tableNumber, JsonConvert.SerializeObject(trustedOrderItems), comment); } } //Purchase successfull TempData["Success"] = "Transaction was successful, Transaction ID " + result.Target.Id + " Amount Charged : £" + result.Target.Amount; return(RedirectToAction("Success")); } TempData["Error"] = result.Target.ProcessorResponseText; return(RedirectToAction("Error")); }
public ActionResult Index(int content = -1, int table = -1) { //payment controls BrainTree brain = new BrainTree(content); if (content == -1) { TempData["Error"] = "The menu you are looking for doesn't exist"; return(View("MenuNotFound")); } var brainToken = brain.CreateClientToken(); if (!brainToken.success) { TempData["Alert"] = "You just need to connect your BrainTree Account to your menu to take payments"; TempData["Redirect"] = "/Keys?content=" + content; return(RedirectToAction("Alert", "Braintree")); } ViewData["ClientToken"] = brainToken.token; //menu componets ViewData["menuID"] = content; var menuComponents = MenuContentHelper.createMenuComponents(content); ViewData["title"] = menuComponents.title; ViewData["tags"] = menuComponents.tags; ViewData["menuSections"] = menuComponents.sections; ViewData["bannerImage"] = menuComponents.bannerImage; ViewData["menuNavigaton"] = menuComponents.menuNavigaton; ViewData["footer"] = menuComponents.footer; //editor button if (Session["id"] != null && AccountHelper.CanEditMenu(content, Convert.ToInt32(Session["id"]))) { ViewData["editButton"] = $@" <div class='nav-button btn-effect' id='btn-edit-menu' data-id='{content}'> <i class='fas fa-edit'></i> <span id='login-text'>Edit</span> </div> "; } //is menu taking orders var menuTimes = Helpers.MenuContentHelper.menuTimes(content); if (!menuTimes.isOpen) { ViewData["notOpenDisplay"] = "block"; } else { ViewData["notOpenDisplay"] = "none"; } ViewData["openMonday"] = menuTimes.menuTime.MondayOpen + "-" + menuTimes.menuTime.MondayClose; ViewData["openTuesday"] = menuTimes.menuTime.TuesdayOpen + "-" + menuTimes.menuTime.TuesdayClose; ViewData["openWednesday"] = menuTimes.menuTime.WednesdayOpen + "-" + menuTimes.menuTime.WednesdayClose; ViewData["openThursday"] = menuTimes.menuTime.ThursdayOpen + "-" + menuTimes.menuTime.ThursdayClose; ViewData["openFriday"] = menuTimes.menuTime.FridayOpen + "-" + menuTimes.menuTime.FridayClose; ViewData["openSaturday"] = menuTimes.menuTime.SaturdayOpen + "-" + menuTimes.menuTime.SaturdayClose; ViewData["openSunday"] = menuTimes.menuTime.SundayOpen + "-" + menuTimes.menuTime.SundayClose; return(View()); }