//Will determine which reward is being redeemed then create a new reward object associated to that account public async Task AddRewardToAccount(int id, Account account) { if (ModelState.IsValid) { Reward newReward = new Reward(); newReward.AccountId = account.AccountId; newReward.DateReceived = DateTime.Now; newReward.RewardCode = RandomString(); int oldPointBalance = (int)account.PointBalance; int newPointBalance = 0; if (id == 1) { newReward.RewardType = "-25% Discount Code"; newReward.PointCost = 50; newPointBalance = oldPointBalance - 50; } else if (id == 2) { newReward.RewardType = "-50% Discount Code"; newReward.PointCost = 100; newPointBalance = oldPointBalance - 100; } else if (id == 3) { newReward.RewardType = "-75% Discount Code"; newReward.PointCost = 150; newPointBalance = oldPointBalance - 150; } else { ModelState.AddModelError("", "Invalid Reward Id"); } _context.Add(newReward); await _context.SaveChangesAsync(); AccountsController accountsController = new AccountsController(_context, _hostEnvironment); await accountsController.EditPoints(account, newPointBalance); HttpContext.Session.SetInt32("pointsBalance", newPointBalance); } }
//Checkout - THIS IS WHERE PAYMENT PORTAL //public async Task<IActionResult> CheckOut() public async Task <IActionResult> CheckOut(string stripeEmail, string stripeToken, bool charged, string userId) { var customers = new CustomerService(); var charges = new ChargeService(); var customer = customers.Create(new CustomerCreateOptions { Email = stripeEmail, Source = stripeToken }); // Find the cart associated with this account var cartContext = await _context.Cart .Where(s => s.TransactionComplete == false) .FirstOrDefaultAsync(s => s.AccountId.ToString() == userId); // Find all the items associated with this cart var cartItems = _context.ItemsForCart .Include(s => s.SellListing) .Include(s => s.SellListing.Seller) .Include(s => s.SellListing.Seller.Account) .Include(s => s.Cart) .Where(s => s.SellListingId == s.SellListing.SellListingId) .Where(s => s.CartId == cartContext.CartId); var model = await cartItems.ToListAsync(); long sAmount = (long)StripeSubTotal(model); long tax = (long)StripeTax(sAmount); long amount = (long)TotalCost(sAmount, tax); //Amount is being charged correctly with the decimal places, but doesn't want to display them // (ex: 5082 should display $50.82 in email but only displays $50 instead) var charge = charges.Create(new ChargeCreateOptions { Amount = amount, Description = "Purchase off of Geekium", Currency = "cad", Customer = customer.Id, ReceiptEmail = stripeEmail, Metadata = new Dictionary <string, string>() { { "OrderId", "111" }, { "OrderFrom", "Geekium" } } }); if (charge.Status == "succeeded") { string BalanceTransactionId = charge.BalanceTransactionId; customerNotifEmail(model, charge); sellerNotifEmail(model, charge); //Set session objects for reward type and reward code to null to avoid redundency when //a new cart object is created RemoveRewardSessionObjects(); //Find account object with previous point balance var account = await _context.Accounts .FirstOrDefaultAsync(m => m.AccountId == int.Parse(userId)); //Determing new points balance for the current account that is making the purchase double newPointBalance = (double)(account.PointBalance + amount); //Save the new point balance to the buyer account AccountsController accountsController = new AccountsController(_context, _hostEnvironment); await accountsController.EditPoints(account, (int)newPointBalance); //Save the new purchase item to the AccountPurchases Controller double purchasePrice = SubTotal(cartItems.ToList()); int pointGain = (int)purchasePrice; AccountPurchasesController accountPurchases = new AccountPurchasesController(_context); await accountPurchases.AddPurchase(cartContext, purchasePrice, pointGain, int.Parse(userId)); //Update Cart Transaction Status await ChangeCartTransactionStatus(cartContext); return(RedirectToAction("Index", "AccountPurchases", new { area = "" })); } else { return(View("Unsuccessful")); } }