Example #1
0
        public async Task <ActionResult> Purchase(PurchaseCouponViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index", model));
            }

            var getOffer = _context.Offers.FirstOrDefault(c => c.OfferId == model.OfferId);

            var getRandomProductionCode = _context.ProductionCodes.Where(c => c.IsActive == false && c.IsUsed == false && c.OfferId == null).OrderBy(c => Guid.NewGuid()).FirstOrDefault();

            if (getRandomProductionCode != null)
            {
                getRandomProductionCode.OfferId  = model.OfferId;
                getRandomProductionCode.IsActive = true;
                _context.SaveChanges();
            }

            var details = new GenCouponViewModel()
            {
                OfferDetails = getOffer,
                CouponCode   = getRandomProductionCode.CouponCode
            };

            //return View();
            return(GenerateCoupon(details));
        }
        public ActionResult GetCoupon(int OfferId)//, string stripeToken)
        {
            //if (!ModelState.IsValid)
            //1.Return View if something is wrong
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }

            //2.Get the Offer Select
            var getOffer = _context.Offers.Include(c => c.Merchant).FirstOrDefault(c => c.OfferId == OfferId);

            //2. Charge the customer
            //var charge = Payment.Charge(stripeToken, getOffer);

            //Check if the money was paid
            //Return the user
            //if (!charge)
            //    return RedirectToAction("Index");

            //3.Gets a random code from Production Table to assign to the Coupon
            var getRandomProductionCode = _context.ProductionCodes.Where(c => c.IsActive == false && c.IsUsed == false && c.OfferId == null).OrderBy(c => Guid.NewGuid()).FirstOrDefault();

            //Set coupon Purchase Time stamp
            var couponPurchaseTimeStamp = DateTime.Now;

            //sql DateTime object
            //SqlDateTime sqlDateTime = new SqlDateTime(couponPurchaseTimeStamp);
            //4.The Production Code that was receive
            //will be assigned to the Offer and label as Active.
            if (getRandomProductionCode != null)
            {
                getRandomProductionCode.OfferId  = OfferId;
                getRandomProductionCode.IsActive = true;
                //Sets the Date the coupon got bought
                getRandomProductionCode.PurchaseDate = DateTime.Now;
                //_DBFirstContext.SetCouponCodePurchaseDateTimeById(getRandomProductionCode.ProductionCodeID);
                //getRandomProductionCode.PurchaseDate = couponPurchaseTimeStamp;//.TryParse("yyyy-MM-dd HH:mm:ss.fff");//this will capture the date and time of purchase //DateTime.Now.Date;
                _context.SaveChanges();
            }

            var details = new GenCouponViewModel()
            {
                OfferDetails = getOffer,
                CouponCode   = getRandomProductionCode.CouponCode,
                CouponExpireDateTimeDisplay = couponPurchaseTimeStamp.AddHours(24)
            };

            //return RedirectToAction("Index");
            EmailCouponTOCustomer(details);
            return(PreviewCoupon(details));
        }
        //Sends Coupon to customer
        public ActionResult EmailCouponTOCustomer(GenCouponViewModel model)
        {
            try
            {
                var email = "";
                //1.Check if User is signed in
                //gets email address if user is, return
                if (User.Identity.IsAuthenticated)
                {
                    email = User.Identity.Name;
                }
                else
                {
                    return(RedirectToAction("Index"));
                }

                //renders the coupon html to string
                string body = System.IO.File.ReadAllText(Server.MapPath("~/Views/Home/_PreCoupon.html"));

                //Replace dummy Value in Coupon Email Template
                body = body.Replace("#Offerer", model.OfferDetails.Merchant.CompanyName);
                body = body.Replace("#OfferName", model.OfferDetails.OfferName);
                //body = body.Replace("#OfferEnds", model.OfferDetails.OfferEnds.ToString("MMM-dd-yyyy"));
                //var expire = DateTime.Now.AddMonths(model.OfferDetails.CouponDurationInMonths);
                var expire           = model.OfferDetails.CreationDate.AddHours(24);
                var expireTimeFormat = expire.ToString("MMM-dd-yyyy") + " at " + expire.ToString("hh:mm tt");
                //body = body.Replace("#OfferEnds", expire.ToString("MMM-dd-yyyy"));
                body = body.Replace("#OfferEnds", expireTimeFormat);
                body = body.Replace("#OfferDetails", model.OfferDetails.OfferDetails);
                body = body.Replace("#DiscountRate%", model.OfferDetails.DiscountRate.ToString() + "%");
                body = body.Replace("#CouponCode", model.CouponCode);

                MailMessage mail = new MailMessage();
                mail.From    = new MailAddress(WebConfigurationManager.AppSettings["fromEmail"]);
                mail.Subject = "Veme Coupon";
                mail.Body    = body;

                mail.IsBodyHtml = true;
                mail.Priority   = MailPriority.Normal;
                //mail.AlternateViews.Add(textView);

                string            smtpHost     = WebConfigurationManager.AppSettings["smtpHost"];
                string            smtpAcc      = WebConfigurationManager.AppSettings["smtpUser"];
                string            smtpPassword = WebConfigurationManager.AppSettings["smtpPassword"];
                int               smtpPort     = Convert.ToInt32(WebConfigurationManager.AppSettings["smtpPort"]);
                NetworkCredential cred         = new NetworkCredential(smtpAcc, smtpPassword);

                using (SmtpClient mailClient = new SmtpClient(smtpHost, smtpPort))
                {
                    mailClient.EnableSsl             = false;
                    mailClient.UseDefaultCredentials = false;
                    mailClient.Credentials           = cred;
                    mail.To.Add(email);
                    mailClient.Send(mail);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                throw;
            }
            //returns nothing
            return(null);
        }
 public ActionResult PreviewCoupon(GenCouponViewModel model)
 {
     return(View("PreviewCoupon", model));
 }
Example #5
0
 //Returns a Page with the generated Coupon
 public ActionResult GenerateCoupon(GenCouponViewModel model)
 {
     return(View("GenerateCoupon", model));
 }