public ActionResult Create(Subscription subscription)
 {
     if(ModelState.IsValid)
     {
         _subRepo.CreateSubscription(subscription);
         return RedirectToAction("Index");
     }
     var vet = _repo.GetByUsername(HttpContext.User.Identity.Name);
     ViewBag.VetId = vet.Id;
     ViewBag.UserList = new SelectList(_userRepo.GetVetSubscribedUsers(vet.Id), "Id", "FullName");
     ViewBag.AddedSubscriptions = vet.Subscriptions.Where(r => !r.Sent);
     return View(subscription);
 }
        public ActionResult Create(Subscription subscription)
        {
            if (ModelState.IsValid)
            {
                db.Subscriptions.Add(subscription);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.UserId = new SelectList(db.Users, "Id", "FirstName", subscription.UserId);
            ViewBag.PetId = new SelectList(db.Pets, "Id", "Name", subscription.PetId);
            ViewBag.ProductId = new SelectList(db.Products, "Id", "Name", subscription.ProductId);
            ViewBag.VetId = new SelectList(db.Vets, "Id", "Name", subscription.VetId);
            return View(subscription);
        }
 public ActionResult Deliver(int id)
 {
     var subscription = db.Subscriptions.SingleOrDefault(r => r.Id == id);
     if(subscription.Sent)
     {
         subscription.Sent = false;
         db.Entry(subscription).State = EntityState.Modified;
         db.SaveChanges();
     }
     else
     {
         var newSub = new Subscription
                          {
                              PetId = subscription.PetId,
                              ProductId = subscription.ProductId,
                              UserId = subscription.UserId,
                              VetId = subscription.VetId,
                              DateSubscribed = subscription.DateSubscribed
                          };
         var deliveryMonth = subscription.NextDeliveryDate.Month + 1;
         var deliveryYear = subscription.NextDeliveryDate.Year;
         const int deliveryDay = 1;
         var deliveryDate = new DateTime(deliveryYear, deliveryMonth, deliveryDay);
         while (deliveryDate.DayOfWeek != DayOfWeek.Monday)
         {
             deliveryDate = deliveryDate.AddDays(1);
         }
         newSub.NextDeliveryDate = deliveryDate;
         subscription.Sent = true;
         db.Entry(subscription).State = EntityState.Modified;
         db.Subscriptions.Add(newSub);
         db.SaveChanges();
     }
     return RedirectToAction("Index");
 }
 public ActionResult Edit(Subscription subscription)
 {
     if (ModelState.IsValid)
     {
         db.Entry(subscription).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.UserId = new SelectList(db.Users, "Id", "FirstName", subscription.UserId);
     ViewBag.PetId = new SelectList(db.Pets, "Id", "Name", subscription.PetId);
     ViewBag.ProductId = new SelectList(db.Products, "Id", "Name", subscription.ProductId);
     ViewBag.VetId = new SelectList(db.Vets, "Id", "Name", subscription.VetId);
     return View(subscription);
 }
        public Subscription SubscribePet(VMSubscription subscription)
        {
            var user = _db.Users.SingleOrDefault(r => r.Id == subscription.UserId);
            var product = _db.Products.SingleOrDefault(r => r.Id == subscription.ProductId);
            var price = ((double) product.Price*100).ToString(CultureInfo.InvariantCulture);

            var day = DateTime.Now.Day;
            var month = DateTime.Now.Month;
            var year = DateTime.Now.Year;

            var rebillIntDate = string.Format("{0}/{1}/{2}", (day + 2).ToString("00"), month.ToString("00"), year);
            var rebillStartDate = string.Format("{0}/{1}/{2}", 1.ToString("00"), (month + 1).ToString("00"), year);
            var rebillEndDate = string.Format("{0}/{1}/{2}", day.ToString("00"), month.ToString("00"), year + 2);

            var clientRecurring = new eWay.Rebill.Test.manageRebill_testSoapClient();
            var ewayHeader = GetRebillEwayHeader();
            var status =
                clientRecurring.CreateRebillEvent(ref ewayHeader, user.RebillCustomerID, "Felix and Rover Subscription", "Product Subscription: " + product.DisplayName, subscription.NameOnCard,
                                                  subscription.CCNumber, subscription.CCExpMonth, subscription.CCExpYear,
                                                  price, rebillIntDate, price, rebillStartDate, "1", "3",
                                                  rebillEndDate);

            if (status.Result.ToLower() == "success")
            {
                var sub = new Subscription()
                              {
                                  UserId = subscription.UserId,
                                  PetId = subscription.PetId,
                                  ProductId = subscription.ProductId,
                                  VetId = subscription.VetId,
                                  RebillID = status.RebillID
                              };
                var newSub = _subRepo.CreateSubscription(sub);

                //var client = new managedCreditCardPaymentTestSoapClient();
                //var success = client.ProcessPayment(GetEwayHeader(), Convert.ToInt64(user.TokenCustomerID), ((int) product.Price * 100),
                //                                    "Initial Payment for Subscription", "Subscribed to Product: " + product.Name).ewayTrxnStatus;
                //string RebillCustomerID, string RebillInvRef, string RebillInvDes, string RebillCCName, string RebillCCNumber,
                //string RebillCCExpMonth, string RebillCCExpYear, string RebillInitAmt, string RebillInitDate, string RebillRecurAmt,
                //string RebillStartDate, string RebillInterval, string RebillIntervalType, string RebillEndDate
                //var successRecurring = clientRecurring.CreateRebillEvent(ref ewayHeader, user.RebillCustomerID, )

                try
                {
                    var body = "<h1>Subscription Successful</h1>";
                    body += "<p>Thank you for subscribing to felix and rover</p>";
                    body += "<p>The pet owner " + newSub.User.FullName + " and pet named " + newSub.Pet.Name +
                            " will now receive the following product monthly:</p>";
                    body += "<p>Product name: " + newSub.Product.DisplayName + "</p>";
                    body += "<p>Amount: $" + newSub.Product.Price + "</p>";
                    body += "<p>Thank you,</p>";
                    body += "<p>Felix and Rover Team ^.^</p>";
                    EmailHelper.SendEmail(newSub.User.Email, "Felix and Rover (Product Subscription)", body);
                }
                catch (Exception)
                {
                }
                return newSub;
            }

            return null;
        }