public void CreatePaymentTest_Validate_Approve_Status_Balance_Check() { decimal initialBalance = InMemoryData.CurrentBalance; CreatePaymentRequest request = new CreatePaymentRequest { Amount = 10, PaymentDate = DateTime.Now }; PaymentValidatorService validatorService = new PaymentValidatorService(); PaymentService paymentService = new PaymentService(validatorService); CreatePaymentResponse response = paymentService.CreatePayment(request); if (response.IsValid && response.Payment != null && response.Payment.RunningBalance == initialBalance && response.ErrorMessages.Count == 0 && response.Payment.Status == "Pending") { ApprovePaymentRequest approvePaymentRequest = new ApprovePaymentRequest(); approvePaymentRequest.PaymentId = response.Payment.PaymentId; ApprovePaymentResponse approvePaymentResponse = paymentService.ApprovePayment(approvePaymentRequest); if (approvePaymentResponse.IsValid) { Assert.IsTrue(approvePaymentResponse.Payment.Status == "Processed"); Assert.IsTrue(approvePaymentResponse.Payment.RunningBalance == (initialBalance - request.Amount)); Assert.IsTrue(approvePaymentResponse.Payment.Amount == request.Amount); } } else { string msg = string.Empty; string delimiter = Environment.NewLine; response.ErrorMessages.ForEach(x => msg += x + delimiter); Assert.Fail(msg); } }
static async Task <string> ApprovePayment(Guid paymentId) { Console.WriteLine(); Console.WriteLine("******Approve payment test***********"); Console.WriteLine(); Uri u = new Uri(baseUrl + "approvepayment"); var response = string.Empty; using (var client = new HttpClient()) { ApprovePaymentRequest payload = new ApprovePaymentRequest(); payload.PaymentId = paymentId; HttpResponseMessage res = await client.PostAsJsonAsync(u, payload); if (res.IsSuccessStatusCode) { ApprovePaymentResponse reply = await res.Content.ReadAsAsync <ApprovePaymentResponse>(); if (reply.Payment.PaymentId == paymentId && reply.Payment.Status == "Processed") { Console.WriteLine("Approve payment failed for payment id " + paymentId); } return(JsonConvert.SerializeObject(reply)); } } return(response); }
public IPaymentResponse ExecuteAction(IRequest approvePaymentRequest) { ApprovePaymentResponse response = ValidatorService.ValidateApprovePayment((ApprovePaymentRequest)approvePaymentRequest); if (response.IsValid) { response.Payment.Status = "Processed"; InMemoryData.CurrentBalance -= response.Payment.Amount; response.Payment.RunningBalance = InMemoryData.CurrentBalance; return(response); } return(response); }
public ApprovePaymentResponse ValidateApprovePayment(ApprovePaymentRequest approvePaymentRequest) { ApprovePaymentResponse response = new ApprovePaymentResponse(); Payment payment = InMemoryData.Payments.FirstOrDefault(x => x.PaymentId == approvePaymentRequest.PaymentId); if (payment == null) { response.ErrorMessages.Add("Such a payment does not exists!!!!"); } else if (payment.Status == "Pending") { response.IsValid = true; response.Payment = payment; } else { response.ErrorMessages.Add("You cannot approve/cancel processed or closed payments"); } return(response); }
public void CreatePaymentTest_Disallow_Cancel_On_Processed_Payments() { decimal initialBalance = InMemoryData.CurrentBalance; CreatePaymentRequest request = new CreatePaymentRequest { Amount = 10, PaymentDate = DateTime.Now }; PaymentValidatorService validatorService = new PaymentValidatorService(); PaymentService paymentService = new PaymentService(validatorService); CreatePaymentResponse response = paymentService.CreatePayment(request); if (response.IsValid && response.Payment != null && response.Payment.RunningBalance == initialBalance && response.ErrorMessages.Count == 0 && response.Payment.Status == "Pending") { ApprovePaymentRequest approvePaymentRequest = new ApprovePaymentRequest(); approvePaymentRequest.PaymentId = response.Payment.PaymentId; ApprovePaymentResponse approvePaymentResponse = paymentService.ApprovePayment(approvePaymentRequest); if (approvePaymentResponse.IsValid) { CancelPaymentRequest tryCancelClosedPayment = new CancelPaymentRequest(); tryCancelClosedPayment.PaymentId = approvePaymentResponse.Payment.PaymentId; CancelPaymentResponse disAllowedAction = paymentService.CancelPayment(tryCancelClosedPayment); Assert.IsFalse(disAllowedAction.IsValid); Assert.IsTrue(disAllowedAction.ErrorMessages.Count == 1); Assert.IsTrue(disAllowedAction.ErrorMessages[0] == "Only pending payment can be cancelled"); } } else { string msg = string.Empty; string delimiter = Environment.NewLine; response.ErrorMessages.ForEach(x => msg += x + delimiter); Assert.Fail(msg); } }