public ActionResult Create(CreateVoucherViewModel model) { if (!ModelState.IsValid) { return(View(model)); } using (ITransaction transaction = _session.BeginTransaction()) { var voucher = Voucher.CreateVoucher(model.CustomerId, new Money(model.Amount), _codeGenerator); _voucherRepository.Add(voucher); transaction.Commit(); } return(RedirectToAction("Index")); }
public bool checkout([Microsoft.AspNetCore.Mvc.FromBody] JObject data) { string products = data["products"].ToString(); string user_id = data["user_id"].ToString(); bool use_discount = data["use_discount"].ToObject <bool>(); string sign = data["sign"].ToString(); string voucher_id_string = ""; if (data.ContainsKey("voucher_id")) { voucher_id_string = data["voucher_id"].ToString(); Logger.LogInfo(voucher_id_string, "Root"); bool valid = Voucher.isVoucherAvailable(voucher_id_string); if (!valid) { HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.Unauthorized); message.Content = new StringContent("Invalid Coupon"); throw new HttpResponseException(message); } } List <Product> products_list = new List <Product>(); string id_string = ""; float final_price = 0.0f; JArray prods = JArray.Parse(products); for (int i = 0; i < prods.Count; ++i) { string product_id = prods.ElementAt(i).ToString(); id_string += product_id; Product product = Product.GetProduct(product_id); if (product == null) { HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.NotFound); message.Content = new StringContent("Invalid Product ID"); throw new HttpResponseException(message); } float price = product.GetPriceEuros() + product.GetPriceCents() / 100.0f; final_price += price; products_list.Add(product); } string user_key = Client.GetUserPublicKey(user_id); if (user_key == null) { HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.NotFound); message.Content = new StringContent("Invalid User ID"); throw new HttpResponseException(message); } /* * RSAEncrypter encrypter = RSAEncrypter.GetRSAEncrypter(); * if (!encrypter.Verify(id_string, sign, user_key)) * { * HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.Unauthorized); * message.Content = new StringContent("Invalid Content Signature"); * throw new HttpResponseException(message); * }*/ // figure out if voucher should be generated bool generateVoucher = false; int vouchersToGenerate = 0; KeyValuePair <int, int> spent = Client.GetTotalAmmountSpent(user_id); float current_ammount_spent = (float)spent.Key + spent.Value / 100.0f; float hundredsSpent = MathF.Truncate(current_ammount_spent / 100.0f); float totalAmmountSpent = current_ammount_spent + final_price; float newHundredsSpent = MathF.Truncate(totalAmmountSpent / 100.0f); if (newHundredsSpent > hundredsSpent) { generateVoucher = true; vouchersToGenerate = (int)(newHundredsSpent - hundredsSpent); } float account_delta = 0; KeyValuePair <int, int> accumulated = Client.GetAccumulatedDiscount(user_id); float totalAccumulated = accumulated.Key + accumulated.Value / 100.0f; if (use_discount) { if (totalAccumulated >= final_price) { account_delta = totalAccumulated - final_price; } else { account_delta = -totalAccumulated; } } if (voucher_id_string != "") { Voucher.Use(voucher_id_string); account_delta += 0.15f * final_price; } // generate transaction if (voucher_id_string != "") { Transaction.RegisterTransaction(user_id, voucher_id_string, use_discount, products_list); } else { Transaction.RegisterTransaction(user_id, "NULL", use_discount, products_list); } // update current spent int spent_euros = (int)MathF.Truncate(totalAmmountSpent); int spent_cents = (int)(totalAmmountSpent - spent_euros); Client.UpdateTotalSpent(user_id, spent_euros, spent_cents); // update ammount in account totalAccumulated += account_delta; int accumulated_euros = (int)MathF.Truncate(totalAccumulated); int accumulated_cents = (int)(totalAccumulated - accumulated_euros); Client.UpdateTotalAccumulated(user_id, accumulated_euros, accumulated_cents); if (generateVoucher) { // add voucher to user account for (int i = 0; i < vouchersToGenerate; ++i) { Voucher.CreateVoucher(user_id); } } return(true); }