public IHttpActionResult PostNewPayment(PaymentPostModel model) { if (ModelState.IsValid) { CreditCard card = DbContext.CreditCards.FirstOrDefault(p => p.IdentificationNumber == model.CreditCardIdNumber); if (card != null) { Payment payment = new Payment { CreditCard = card, CreditCardId = card.Id, Amount = model.Amount, SecurityCode = model.SecurityCode, CreditCardNumber = model.CreditCardNumber, NameOnCard = model.NameOnCard, Approved = FiftyPercentBool() }; DbContext.Payments.Add(payment); DbContext.SaveChanges(); if (payment.Approved) { PaymentViewModel viewModel = new PaymentViewModel { Id = payment.Id, SecurityCode = payment.SecurityCode, Amount = payment.Amount, CreditCardNumber = payment.CreditCardNumber, NameOnCard = payment.NameOnCard, Approved = payment.Approved, CreditCard = new CreditCardViewModel { Id = payment.CreditCard.Id, Brand = payment.CreditCard.Brand, IdentificationNumber = payment.CreditCard.IdentificationNumber } }; return(Ok(viewModel)); } else { return(BadRequest("The Payment has not been approved. This could be because we don't support all credit cards yet.")); } } else { return(BadRequest("Credit card Id Number is not valid.")); } } else { return(BadRequest(ModelState)); } }
public PaymentPostModel Build() { var paymentPostModel = new PaymentPostModel { Amount = 1, CardNumber = _cardNumber, Currency = Currency.NewZealandDollar, ExpiryYear = 2050, ExpiryMonth = 1, SecurityCode = "123" }; foreach (var modification in _modifications) { modification(paymentPostModel); } return(paymentPostModel); }
public async Task <Payment> PostPayment(Guid accountId, [FromBody] PaymentPostModel paymentPostModel, CancellationToken cancellationToken) { var account = await dbContext.Accounts.Include(x => x.Payments).FirstOrDefaultAsync(x => x.Id == accountId); var payment = new Payment { Amount = paymentPostModel.Amount, DateTime = DateTime.UtcNow }; account.Balance += paymentPostModel.Amount; account.Payments.Add(payment); dbContext.SaveChanges(); //********** YOU SHOULD NEVER PUBLISH EVENTS LIKE THIS **************// eventBus.Publish(new AccountBalanceUpdatedIntegrationEvent(account.Id, account.Balance)); eventBus.Publish(new PaymentCompletedIntegrationEvent(accountId, payment.Amount, payment.DateTime)); //********** YOU SHOULD NEVER PUBLISH EVENTS LIKE THIS **************// return(payment); }
public async Task WhenSendingAnInvalidRequest_ThenTheResponseHas500BadRequestCodeAndAPaymentCanNotBeRetrieved(PaymentPostModel model) { var paymentId = Guid.NewGuid(); _testOutputHelper.WriteLine($"Sending payment {paymentId} with body {JsonConvert.SerializeObject(model)}"); var response1 = await _apiClient.PostAsJsonAsync($"/v1/payment/{paymentId}", model); response1?.StatusCode.Should().Be(StatusCodes.Status400BadRequest); var response2 = await _apiClient.GetAsync($"/v1/payment/{paymentId}"); response2?.StatusCode.Should().Be(StatusCodes.Status404NotFound); }
public static void Main(string[] args) { var ctrl = new StudentsController(); DateTime date1 = new DateTime(2017, 7, 20); DateTime date2 = new DateTime(2018, 8, 22); DateTime date3 = new DateTime(2019, 2, 12); DateTime date4 = new DateTime(2015, 5, 9); PaymentPostModel payment1 = new PaymentPostModel() { Date = date1, Value = 100 }; PaymentPostModel payment2 = new PaymentPostModel() { Date = date2, Value = 120 }; PaymentPostModel payment3 = new PaymentPostModel() { Date = date3, Value = 110 }; PaymentPostModel payment4 = new PaymentPostModel() { Date = date4, Value = 90 }; List <PaymentPostModel> paymentList1 = new List <PaymentPostModel>() { payment1, payment2 }; List <PaymentPostModel> paymentList2 = new List <PaymentPostModel>() { payment3, payment4 }; List <PaymentPostModel> paymentList3 = new List <PaymentPostModel>() { payment1 }; List <PaymentPostModel> paymentList4 = new List <PaymentPostModel>() { payment4 }; StudentPostModel stToAdd_1 = new StudentPostModel() { FirstName = "Ivan", Lastname = "Ivanov", Age = 20, Payments = paymentList1 }; StudentPostModel stToAdd_2 = new StudentPostModel() { FirstName = "Petr", Lastname = "Petrov", Age = 20, Payments = paymentList2 }; StudentPostModel stToAdd_3 = new StudentPostModel() { FirstName = "Homer", Lastname = "Simson", Age = 45, Payments = paymentList3 }; StudentPostModel stToAdd_4 = new StudentPostModel() { FirstName = "Burt", Lastname = "Simson", Age = 20, Payments = paymentList4 }; ctrl.AddStudent(stToAdd_1); ctrl.AddStudent(stToAdd_2); ctrl.AddStudent(stToAdd_3); ctrl.AddStudent(stToAdd_4); int minimumTotal = 150; var stPayLessThan = ctrl.GetStudentWhichPayLessThan(minimumTotal); if (stPayLessThan != null) { Console.WriteLine($"Student(s), who paid less, than {minimumTotal}"); foreach (var item in stPayLessThan) { Console.WriteLine($"Student {item.FullName} paid in total {item.Total}"); } } Console.WriteLine("Finish work"); Console.ReadKey(); //var student = ctrl.GetById(1); }