//Render the Page for Coupon Validation public ActionResult RedeemCoupon() { var userId = User.Identity.GetUserId(); var getUser = _context.Users.Include(c => c.Merchant).FirstOrDefault(c => c.Id == userId); var viewModel = new MerchantRedeemViewModel() { MerchantId = getUser.Merchant.MerchantID }; return(View(viewModel)); }
//Method called via ajax to validate coupon public ActionResult Validate(MerchantRedeemViewModel viewModel) { dynamic showMessage = string.Empty; var getResults = ValidateCoupon(viewModel); showMessage = new { statusCode = 200, Message = getResults, Code = viewModel.CouponCode }; return(Json(data: showMessage, behavior: JsonRequestBehavior.AllowGet)); }
private string ValidateCoupon(MerchantRedeemViewModel model) { //1.Check if coupon code exist for this merchant var checkCoupon = _context.ProductionCodes.Include(c => c.Offers) .FirstOrDefault(c => c.CouponCode.Replace("-", "") == model.CouponCode.Replace("-", "") && c.IsUsed == false && c.IsActive == true && c.Offers.MerchantID == model.MerchantId); //check if service pack var getMerchantId = User.Identity.GetUserId(); var getMerchant = _context.Merchants.Include(p => p.Package).FirstOrDefault(c => c.MerchantID == getMerchantId); if (getMerchant.Package == null) { return("You need to purchase a service pack."); } else { //Check if all validatation calls have been used if (getMerchant.ValidationCallsMade >= getMerchant.Package.Quantity) { return("Insufficient Validation Calls."); } } if (checkCoupon == null) { return("Invalid Coupon"); } //Check if max coupons used if (checkCoupon.Offers.TotalOffer == checkCoupon.Offers.CouponUsed) { return("Offer Coupon Exhausted"); } //Check if coupon expires var getPurchaseDate = checkCoupon.PurchaseDate; //var addOneDay = checkCoupon.PurchaseDate.Value.AddHours(24); //add five minutes for testing var addOneDay = checkCoupon.PurchaseDate.Value.AddMinutes(5); if (addOneDay < DateTime.Now) { return("Coupon Expired."); } //Increment coupons used checkCoupon.Offers.CouponUsed += 1; checkCoupon.IsUsed = true; //Increment Calls Made if (getMerchant.ValidationCallsMade == null) { getMerchant.ValidationCallsMade = 1; } else { getMerchant.ValidationCallsMade += 1; } //insert redeemedCoupons _context.RedeemedCoupons.Add(new RedeemedCoupon { MerchantId = model.MerchantId, OfferId = checkCoupon.OfferId.Value, RedeemDate = DateTime.Now }); //save changes to the database _context.SaveChanges(); return("Valid Coupon"); }