public async Task <ActionResult> PayByCreditCard(PaymentInputInfo info) { var paymentSession = await context.PaymentSessions.FirstOrDefaultAsync(session => session.SessionId == info.SessionId); if (paymentSession.SessionRegistrationTime.AddMinutes(paymentSession.LifeSpanInMinute) < DateTime.Now) { return(BadRequest("Payment session is out of time")); } Receipt receipt = new Receipt { Id = Guid.NewGuid(), CustomerName = info.Card.HolderName, Product = paymentSession.PaymentAppointment, OperationTime = DateTime.Now, Seller = info.Seller, Cost = paymentSession.Cost }; context.PaymentSessions.Remove(paymentSession); context.Receipts.Add(receipt); await context.SaveChangesAsync(); await SendNotificationToShop(receipt); return(Ok($"Congratulations, {info.Card.HolderName}, you are a happy owner of {paymentSession.PaymentAppointment}!")); }
public async Task Unsuccessful_Payment_Session_Is_Over() { //Arrange var DatabaseName = Guid.NewGuid().ToString(); var context = ContextBuilder.BuildContext(DatabaseName); context.PaymentSessions.Add(new PaymentSession { Cost = 200, PaymentAppointment = "Item1", LifeSpanInMinute = 60, SessionRegistrationTime = DateTime.Now.AddMinutes(-90), SessionId = new Guid("ebd21f99-46a5-438f-8d6c-7e0a259b278e") }); await context.SaveChangesAsync(); var controller = new PaymentByCreditCardController(context); CreditCard card = new CreditCard { Number = "1111 2222 3333 4444", CVV = "123", HolderName = "Alex", ExpirationDate = new DateTime(2021, 10, 1) }; var paymentInfo = new PaymentInputInfo { Card = card, SessionId = new Guid("ebd21f99-46a5-438f-8d6c-7e0a259b278e"), }; //Act var response = await controller.PayByCreditCard(paymentInfo); var count = context.Receipts.Count(); //Assert Assert.AreEqual(0, count); Assert.NotNull(response is BadRequestObjectResult); }