Exemplo n.º 1
0
        public void ResponseOk()
        {
            var mockApproveUser = University.GetUser("e101");

            //get Applications
            StickerController sc = new StickerController();

            sc.ControllerContext = new ControllerContext(MockAuthContext(mockApproveUser).Object, new RouteData(), sc);
            ViewResult indexResult = sc.Index() as ViewResult;

            Assert.IsNotNull(indexResult);
            Assert.IsInstanceOfType(indexResult.Model, typeof(List <StickerApplication>));

            //find application waiting for payment
            List <StickerApplication> listModel = indexResult.Model as List <StickerApplication>;

            Assert.AreNotEqual(0, listModel.Count);
            StickerApplication application = listModel.Find(m => m.Status == StickerApplicationStatus.WaitingForPayment);

            Assert.IsNotNull(application);
            Assert.AreEqual(StickerApplicationStatus.WaitingForPayment, application.Status);

            //pay
            PayController controller = new PayController();

            controller.ControllerContext = new ControllerContext(MockAuthContext(mockApproveUser).Object, new RouteData(), controller);

            PaymentResponseSuccess pr = new PaymentResponseSuccess()
            {
                amount    = application.Quota.StickerFee,
                TransId   = "TEST-Transaction-001",
                ReturnOid = application.ID.ToString() + "-TestOrder",
                mdStatus  = 4,
                Response  = "success"
            };

            RedirectToRouteResult result = controller.Ok(pr) as RedirectToRouteResult;

            Assert.IsNotNull(result);
            Assert.AreEqual("Sticker", result.RouteValues["controller"]);
            Assert.AreEqual("Index", result.RouteValues["action"]);
            Assert.IsNotNull(result.RouteValues["paymentok"]);

            //get updated list to be sure application status is not updated
            ViewResult indexResultR = sc.Index() as ViewResult;

            Assert.IsNotNull(indexResultR);
            Assert.IsInstanceOfType(indexResultR.Model, typeof(List <StickerApplication>));
            List <StickerApplication> listModelR = indexResultR.Model as List <StickerApplication>;

            Assert.AreNotEqual(0, listModelR.Count);
            StickerApplication applicationR = listModelR.Find(m => m.ID == application.ID);

            Assert.IsNotNull(applicationR);
            Assert.AreEqual(StickerApplicationStatus.WaitingForDelivery, applicationR.Status);
        }
Exemplo n.º 2
0
        public ActionResult Ok(PaymentResponseSuccess resp)
        {
            Trace.WriteLine("POST /Pay/Ok");
            using (DatabaseContext db = GetNewDBContext())
            {
                int id = int.Parse(resp.ApplicationId);

                if (id == 0)
                {
                    return(new HttpUnauthorizedResult());
                }

                var application = db.StickerApplications
                                  .Include(a => a.Owner)
                                  .Include(a => a.Payment)
                                  .Include(a => a.Quota.Term)
                                  .Include(a => a.Quota.Type)
                                  .Include(a => a.Sticker)
                                  .Include(a => a.User.Division)
                                  .Include(a => a.User.Category)
                                  .Include(a => a.Vehicle)
                                  .Where(a => a.ID == id)
                                  .FirstOrDefault();

                if (application == null)
                {
                    return(new HttpNotFoundResult());
                }

                var p = new Payment()
                {
                    Amount            = resp.amount,
                    Application       = application,
                    TransactionDate   = DateTime.Now,
                    TransactionNumber = resp.TransId
                };

                db.Payments.Add(p);
                if (db.SaveChanges() == 0)
                {
                    throw new InvalidOperationException("Payment could not be saved!!");
                }

                application.Status       = Models.StickerApplicationStatus.WaitingForDelivery;
                application.LastModified = DateTime.Now;
                if (db.SaveChanges() == 0)
                {
                    throw new InvalidOperationException("Application could not be updated!!");
                }
                return(RedirectToAction("Index", "Sticker", new { paymentok = "1" }));
            }
        }