Exemplo n.º 1
0
        public void PostShouldValidateParametersAndThrowException(decimal amount, long cardNumber, int ccv, int merchantId)
        {
            var paymentRequest = new MerchantPaymentDetailsRequest()
            {
                MerchantId = merchantId,
                Payment    = new PaymentDetails()
                {
                    Amount     = amount,
                    CardNumber = cardNumber,
                    CCV        = ccv,
                    Timestamp  = DateTime.MaxValue
                }
            };

            var payment = new MerchantPaymentDetails(paymentRequest);

            var paymentService = new Mock <IPaymentService>();

            paymentService.Setup(ps => ps.ProcessPayment(It.IsAny <MerchantPaymentDetails>()))
            .Returns(Task.FromResult <MerchantTransactionPayment>(null));

            var sut = new Controllers.PaymentController(paymentService.Object);

            Assert.Throws <AggregateException>(() =>
            {
                var result = sut.Post(paymentRequest).Result;
            });

            paymentService.Verify(ps => ps.ProcessPayment(
                                      It.IsAny <MerchantPaymentDetails>()), Times.Never);
        }
Exemplo n.º 2
0
        public async Task <TransactionPayment> ProcessPayment(MerchantPaymentDetails details)
        {
            var httpRequest = new HttpRequestMessage()
            {
                RequestUri = new Uri(new Uri(baseUrl), $"api/payment/{SupplierId}")
            };

            httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            httpRequest.Method = HttpMethod.Post;

            var content = new PaymentDetails(details);

            var contentStr = JsonConvert.SerializeObject(content);

            httpRequest.Content = new StringContent(
                contentStr,
                System.Text.Encoding.UTF8,
                "application/json");

            var httpResponse = await client.SendAsync(httpRequest);

            var response = JsonConvert.DeserializeObject <TransactionPayment>(
                await httpResponse.Content.ReadAsStringAsync());

            return(response);
        }
Exemplo n.º 3
0
        public async Task <MerchantTransactionPayment> Post(MerchantPaymentDetailsRequest payment)
        {
            if (payment == null || payment.MerchantId == 0 || ValidatePaymentDetails(payment.Payment))
            {
                throw new ArgumentException("Invalid data.");
            }

            var data = new MerchantPaymentDetails(payment);

            var processedPayment = await paymentService.ProcessPayment(data);

            return(processedPayment);
        }
Exemplo n.º 4
0
        public void ProcessPaymentShouldCallApiAndStoreResult()
        {
            var requestPayment = new MerchantPaymentDetails()
            {
                Amount     = 1,
                CardNumber = 1,
                CCV        = 1,
                MerchantId = 1,
                Timestamp  = DateTime.MaxValue
            };

            var expectedTransaction = new TransactionPayment()
            {
                Status        = PaymentStatus.Success,
                TransactionId = 1,
                Payment       = new TransactionPaymentDetails()
                {
                    Amount     = requestPayment.Amount,
                    CardNumber = requestPayment.CardNumber,
                    CCV        = requestPayment.CCV,
                    Timestamp  = requestPayment.Timestamp
                }
            };

            var mockApi = new Mock <IPaymentApi>();

            mockApi.Setup(a => a.ProcessPayment(It.IsAny <MerchantPaymentDetails>()))
            .Returns(Task.FromResult(expectedTransaction));

            var mockRepo = new Mock <IPaymentRepository>();

            mockRepo.Setup(a => a.Insert(
                               It.IsAny <TransactionPayment>(),
                               It.IsAny <int>()))
            .Returns(Task.CompletedTask);

            var sut = new Services.PaymentService(mockApi.Object, mockRepo.Object);

            var transaction = sut.ProcessPayment(requestPayment).Result;

            mockApi.Verify(a => a.ProcessPayment(
                               It.Is <MerchantPaymentDetails>(p =>
                                                              requestPayment.Equals(p)
                                                              )), Times.Once);

            mockRepo.Verify(a => a.Insert(
                                It.Is <TransactionPayment>(t =>
                                                           expectedTransaction.Equals(t)),
                                It.Is <int>(i => i == requestPayment.MerchantId)
                                ), Times.Once);
        }
Exemplo n.º 5
0
        public async Task <MerchantTransactionPayment> ProcessPayment(MerchantPaymentDetails details)
        {
            var transaction = await paymentApi.ProcessPayment(details);

            var merchantId = details.MerchantId;

            await repository.Insert(transaction, merchantId);

            var merchantTransaction = new MerchantTransactionPayment(transaction);

            merchantTransaction.Payment.MerchantId = details.MerchantId;

            return(merchantTransaction);
        }
Exemplo n.º 6
0
        public void ShouldParseApiResponse()
        {
            var transactionPaymentDetails = new TransactionPaymentDetails()
            {
                Amount     = 1,
                CardNumber = 1,
                CCV        = 1,
                SupplierId = SupplierId,
                Timestamp  = DateTime.MaxValue
            };

            var paymentDetails = new MerchantPaymentDetails()
            {
                Amount     = 1,
                CardNumber = 1,
                CCV        = 1,
                MerchantId = 1,
                Timestamp  = DateTime.MaxValue
            };

            var contentStr = $"{{ \"transactionId\": 1, \"status\": 1, " +
                             $"\"payment\": " +
                             JsonConvert.SerializeObject(transactionPaymentDetails) +
                             $"}}";

            var response = new HttpResponseMessage()
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Content    = new StringContent(contentStr)
            };

            var mockHandler = PaymentApiHelper.CreateMockMessageHandler(response);

            var client = new HttpClient(mockHandler.Object);
            var sut    = new External.PaymentApi(client, DummyUrl);

            var result = sut.ProcessPayment(paymentDetails).Result;

            Assert.NotNull(result);

            var transaction = result as TransactionPayment;

            Assert.NotNull(transaction);

            Assert.Equal(1, transaction.TransactionId);
            Assert.Equal(PaymentStatus.Success, transaction.Status);
            Assert.Equal(SupplierId, transaction.Payment.SupplierId);
        }
Exemplo n.º 7
0
        public void ShouldCallProcessPaymentAsAPostHttpMethod()
        {
            var response = new HttpResponseMessage()
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Content    = new StringContent("")
            };

            var mockHandler = PaymentApiHelper.CreateMockMessageHandler(response);

            var client = new HttpClient(mockHandler.Object);
            var sut    = new External.PaymentApi(client, DummyUrl);

            var paymentDetails = new MerchantPaymentDetails();

            var result = sut.ProcessPayment(paymentDetails).Result;

            mockHandler.VerifyMockIsCalledWithMethod(HttpMethod.Post);
        }
Exemplo n.º 8
0
        public void PostShouldCallPaymentServiceProcessPayment()
        {
            var paymentRequest = new MerchantPaymentDetailsRequest()
            {
                MerchantId = 123456,
                Payment    = new PaymentDetails()
                {
                    Amount     = 1,
                    CardNumber = 1234123412341234,
                    CCV        = 123,
                    Timestamp  = DateTime.MaxValue
                }
            };

            var payment = new MerchantPaymentDetails(paymentRequest);

            var transaction = new MerchantTransactionPayment()
            {
                Payment       = payment,
                Status        = PaymentStatus.Success,
                TransactionId = 1
            };

            var paymentService = new Mock <IPaymentService>();

            paymentService.Setup(ps => ps.ProcessPayment(It.IsAny <MerchantPaymentDetails>()))
            .Returns(Task.FromResult(transaction));

            var sut = new Controllers.PaymentController(paymentService.Object);

            var result = sut.Post(paymentRequest).Result;

            Assert.NotNull(result);
            paymentService.Verify(ps => ps.ProcessPayment(
                                      It.Is <MerchantPaymentDetails>(pd =>
                                                                     payment.CardNumber == pd.CardNumber &&
                                                                     payment.CCV == pd.CCV &&
                                                                     payment.Amount == pd.Amount &&
                                                                     payment.MerchantId == pd.MerchantId &&
                                                                     payment.Timestamp == pd.Timestamp
                                                                     )), Times.Once);
        }
Exemplo n.º 9
0
        public void ShouldCallApiWithRightUrl()
        {
            var response = new HttpResponseMessage()
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Content    = new StringContent("")
            };

            var mockHandler = PaymentApiHelper.CreateMockMessageHandler(response);

            var client = new HttpClient(mockHandler.Object);
            var sut    = new External.PaymentApi(client, DummyUrl);

            var paymentDetails = new MerchantPaymentDetails();

            var result = sut.ProcessPayment(paymentDetails).Result;

            //Todo: figure out why is failing
            //mockHandler.VerifyMockIsCalledWithRightRelativePath(DummyUrl + $"api/payment/{SupplierId}");
        }