Exemplo n.º 1
0
        public void Given_TenValidApiValuesAndFiveInternalFailingValues_When_SubmittedToPaymentApiForProcessing_Then_PaymentTenSuccessfulPaymentsAndFiveFailedPaymentsRecorded()
        {
            //Setup Test Data:
            MakePaymentApplicationCommand makePaymentApplicationCommand = new MakePaymentApplicationCommand();

            makePaymentApplicationCommand.FileName = "sample.csv";

            //10 valid payments
            for (int i = 0; i < 10; i++)
            {
                string code      = "sampleCode" + i.ToString();
                string name      = "sampleName" + i.ToString();
                string reference = "sampleReference" + i.ToString();
                double amount    = Convert.ToDouble(i + 10);
                makePaymentApplicationCommand.PaymentRequestValueObjects.Add(new PaymentRequestValueObject()
                {
                    Code = code, Name = name, Reference = reference, Amount = amount
                });
            }

            // 5 invalid internal validation failing payment values
            for (int i = 0; i < 5; i++)
            {
                string code      = "sampleCode" + i.ToString();
                string name      = "sampleName" + i.ToString();
                string reference = "sampleReference" + i.ToString();
                double amount    = Convert.ToDouble(0.13 * i);
                makePaymentApplicationCommand.PaymentRequestValueObjects.Add(new PaymentRequestValueObject()
                {
                    Code = code, Name = name, Reference = reference, Amount = amount
                });
            }

            //Setup endpoint values:
            string baseAddress     = "https://localhost:44399";
            string endpointAddress = "/api/Payment";

            HttpClient httpClient = new HttpClient
            {
                BaseAddress = new Uri(baseAddress)
            };

            //submit request:
            var content = new StringContent(JsonConvert.SerializeObject(makePaymentApplicationCommand), Encoding.UTF8, "application/json");
            var result  = httpClient.PostAsync(endpointAddress, content).Result;

            //check request:
            var response = httpClient.GetAsync(endpointAddress).Result;

            //Read response content result into string variable
            var strJson = response.Content.ReadAsStringAsync().Result;
            //Deserialize the string (Json format) to strongly typed object
            var paymentRequestsForUserReadModel = JsonConvert.DeserializeObject <PaymentRequestsForUserReadModel>(strJson);

            Assert.True(
                paymentRequestsForUserReadModel.PaymentRequestsForUser.First().SuccessfulPayments.Count() == 10 &&
                paymentRequestsForUserReadModel.PaymentRequestsForUser.First().FailedPayments.Count() == 5
                );
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Post(List <IFormFile> files)
        {
            long size = files.Sum(f => f.Length);

            // full path to file in temp location
            var filePath = Path.GetTempFileName();

            List <KeyValuePair <string, string> > dataFileContentList = new List <KeyValuePair <string, string> >();
            List <string> dataFileNameList = new List <string>();

            foreach (var formFile in files)
            {
                var fileName = formFile.FileName.Split("\\").Last();
                if (formFile.Length > 0)
                {
                    dataFileContentList.Add(/*await*/ new KeyValuePair <string, string>(fileName, ReadFileData(formFile)));
                }
            }

            foreach (var dataFileContent in dataFileContentList)
            {
                var dataFileContentRecords = CleanFileData(dataFileContent.Value);

                //validations
                ///ToDo: Add file data validation (not business logic) to make sure that there are no unexpected values in the serialized data, for example letters in the amount field throughout all records, throw exception if we have received incorrect data.

                MakePaymentApplicationCommand makePaymentApplicationCommand = new MakePaymentApplicationCommand();
                makePaymentApplicationCommand.FileName = dataFileContent.Key;

                foreach (var dataFileContentRecord in dataFileContentRecords)
                {
                    var    dataFileContentRecordValues = dataFileContentRecord.Split(",");
                    string code      = dataFileContentRecordValues[0].ToString();
                    string name      = dataFileContentRecordValues[1].ToString();
                    string reference = dataFileContentRecordValues[2].ToString();
                    double amount    = Convert.ToDouble(dataFileContentRecordValues[3]);
                    makePaymentApplicationCommand.PaymentRequestValueObjects.Add(new PaymentRequestValueObject()
                    {
                        Code = code, Name = name, Reference = reference, Amount = amount
                    });
                }

                //Setup endpoint values
                string baseAddress     = "https://localhost:44399";
                string endpointAddress = "/api/Payment";

                HttpClient httpClient = new HttpClient
                {
                    BaseAddress = new Uri(baseAddress)
                };
                await httpClient.PostAsJsonAsync(endpointAddress, makePaymentApplicationCommand);
            }
            //return Ok(new { count = files.Count, size, filePath });
            return(RedirectToAction("Index"));
        }
        public static ProcessPaymentDomainCommand CreateFrom(MakePaymentApplicationCommand makePaymentApplicationCommand)
        {
            var paymentRequestValueObjects = makePaymentApplicationCommand.PaymentRequestValueObjects.ToList().Select(x => new PaymentRequestValueObject()
            {
                Code = x.Code, Name = x.Name, Reference = x.Reference, Amount = x.Amount
            });

            return(new ProcessPaymentDomainCommand(makePaymentApplicationCommand.FileName, paymentRequestValueObjects)
            {
            });
        }
Exemplo n.º 4
0
        public void Pay(MakePaymentApplicationCommand makePaymentApplicationCommand)
        {
            //Create Domain Command from Application Command
            var processPaymentDomainCommand = ProcessPaymentDomainCommand.CreateFrom(makePaymentApplicationCommand);

            //Initialize Domain Model
            PaymentRequest paymentRequestDomainModel = new PaymentRequest();

            //Populate Domain Values, Validate Business Logic on Domain Model using Domain Command
            paymentRequestDomainModel.ProcessPaymentRequest(processPaymentDomainCommand);

            //Use third party or internal api to transfer money between accounts
            ///var successfullyProcessedPayments = _transferMoneyService.ProcessPayments(new PaymentsDto(){...})

            //update Domain Model state
            paymentRequestDomainModel.AddProcessPaymentEvent(/*successfullyProcessedPayments*/);

            //Save Domain Model state to Database
            _paymentRequestRepository.Create(paymentRequestDomainModel);
        }
Exemplo n.º 5
0
 private void MakePayment(MakePaymentApplicationCommand makePaymentCommand)
 {
     _processBacsPaymentService.Pay(makePaymentCommand);
 }
Exemplo n.º 6
0
 public ActionResult Post([FromBody] MakePaymentApplicationCommand makePaymentApplicationCommand)
 {
     MakePayment(makePaymentApplicationCommand);
     return(Ok());
 }