Ejemplo n.º 1
0
        public void ProcessPaymentThrowsHttpExceptionWhenBankingApiRespondsUnsuccessfully()
        {
            //Arrange
            var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            this._handler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(httpResponseMessage);

            var bankDto = new BankProcessPaymentRequestDto()
            {
                CardNumber      = "5500000000000004",
                CardHolder      = "Test Account",
                CardType        = CardType.MasterCard,
                ExpirationMonth = DateTime.Now.ToString("MM"),
                ExpirationYear  = DateTime.Now.AddYears(1).ToString("yy"),
                PaymentAmount   = 100.00M,
                Currency        = SupportedCurrencies.GBP,
                Cvv             = "123"
            };

            //Act - see assertion.

            //Assert
            var ex = Assert.ThrowsAsync <HttpException>(() => this._bankingService.ProcessPayment(bankDto));

            Assert.AreEqual(HttpStatusCode.BadGateway, ex.StatusCode);
            Assert.AreEqual(string.Format(Resources.ErrorMessage_BankingApiUnsuccesfulResponse, "500", "Internal Server Error"), ex.Message);
            Assert.AreEqual(Resources.ErrorCode_BankingApiUnsuccesfulResponse, ex.ErrorCode);
        }
Ejemplo n.º 2
0
        public async Task <BankProcessPaymentResponseDto> ProcessPayment(BankProcessPaymentRequestDto bankDto)
        {
            var           endpoint = this._configuration["bankingApi:paymentsEndpointPost"];
            StringContent content  = new StringContent(JsonConvert.SerializeObject(bankDto), Encoding.UTF8, "application/json");

            return(await this.SendHttpRequest <BankProcessPaymentResponseDto>(HttpVerbs.Post, endpoint, content));
        }
Ejemplo n.º 3
0
        public BankProcessPaymentRequestDto MapProcessPaymentRequestModelToBankDto(ProcessPaymentRequestDto model)
        {
            if (model == null)
            {
                this._logger.LogError(Resources.Logging_DtoMapperNullInput, (typeof(ProcessPaymentRequestDto).Name));

                throw new HttpException(HttpStatusCode.InternalServerError,
                                        Resources.ErrorCode_MappingError_PaymentApiToBankApi,
                                        Resources.ErrorMessage_MappingError_PaymentApiToBankApi);
            }

            var bankDto = new BankProcessPaymentRequestDto()
            {
                CardNumber      = model.CardNumber,
                CardHolder      = model.CardHolder,
                CardType        = model.CardType,
                ExpirationMonth = model.ExpirationMonth,
                ExpirationYear  = model.ExpirationYear,
                PaymentAmount   = model.PaymentAmount,
                Currency        = model.Currency,
                Cvv             = model.Cvv
            };

            return(bankDto);
        }
Ejemplo n.º 4
0
        public async Task ProcessPaymentReturnsProcessPaymentResponse()
        {
            //Arrange
            var model = new ProcessPaymentRequestDto()
            {
                CardNumber      = "5500000000000004",
                CardHolder      = "Test Account",
                CardType        = CardType.MasterCard,
                ExpirationMonth = DateTime.Now.ToString("MM"),
                ExpirationYear  = DateTime.Now.AddYears(1).ToString("yy"),
                PaymentAmount   = 100.00M,
                Currency        = SupportedCurrencies.GBP,
                Cvv             = "123"
            };

            var bankingRequestDto = new BankProcessPaymentRequestDto()
            {
                CardNumber      = "5500000000000004",
                CardHolder      = "Test Account",
                CardType        = CardType.MasterCard,
                ExpirationMonth = DateTime.Now.ToString("MM"),
                ExpirationYear  = DateTime.Now.AddYears(1).ToString("yy"),
                PaymentAmount   = 100.00M,
                Currency        = SupportedCurrencies.GBP,
                Cvv             = "123"
            };

            var transactionId      = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
            var bankingResponseDto = new BankProcessPaymentResponseDto()
            {
                TransactionId = transactionId,
                PaymentStatus = PaymentStatus.Success
            };

            var guid = new Guid(transactionId);
            var processPaymentResponse = new ProcessPaymentResponse()
            {
                TransactionId = guid,
                PaymentStatus = PaymentStatus.Success
            };

            this._dtoMapper.Setup(x => x.MapProcessPaymentRequestModelToBankDto(It.IsAny <ProcessPaymentRequestDto>())).Returns(bankingRequestDto);
            this._bankingService.Setup(x => x.ProcessPayment(It.IsAny <BankProcessPaymentRequestDto>())).ReturnsAsync(bankingResponseDto);
            this._dtoMapper.Setup(x => x.MapBankApiPostResponseToDomainResponse(It.IsAny <BankProcessPaymentResponseDto>())).Returns(processPaymentResponse);

            //Act
            var result = await this._paymentProcessingService.ProcessPayment(model);

            //Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOf <ProcessPaymentResponse>(result);
            Assert.AreEqual(guid, result.TransactionId);
            Assert.AreEqual(PaymentStatus.Success, result.PaymentStatus);
        }
Ejemplo n.º 5
0
        public async Task ProcessPaymentReturnsResponseWhenBankingApiRespondsSuccessfully()
        {
            //Arrange
            var transactionId       = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
            var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
            var data = new BankProcessPaymentResponseDto()
            {
                TransactionId = transactionId,
                PaymentStatus = PaymentStatus.Success
            };

            httpResponseMessage.Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");

            this._handler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(httpResponseMessage);

            var bankDto = new BankProcessPaymentRequestDto()
            {
                CardNumber      = "5500000000000004",
                CardHolder      = "Test Account",
                CardType        = CardType.MasterCard,
                ExpirationMonth = DateTime.Now.ToString("MM"),
                ExpirationYear  = DateTime.Now.AddYears(1).ToString("yy"),
                PaymentAmount   = 100.00M,
                Currency        = SupportedCurrencies.GBP,
                Cvv             = "123"
            };

            //Act
            var result = await this._bankingService.ProcessPayment(bankDto);

            //Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOf <BankProcessPaymentResponseDto>(result);
            Assert.AreEqual(transactionId, result.TransactionId);
            Assert.AreEqual(PaymentStatus.Success, result.PaymentStatus);
        }
Ejemplo n.º 6
0
        public void ProcessPaymentThrowsHttpExceptionWhenBankingApiCallEncountersAnUnpectedError()
        {
            //Arrange
            var bankDto = new BankProcessPaymentRequestDto()
            {
                CardNumber      = "5500000000000004",
                CardHolder      = "Test Account",
                CardType        = CardType.MasterCard,
                ExpirationMonth = DateTime.Now.ToString("MM"),
                ExpirationYear  = DateTime.Now.AddYears(1).ToString("yy"),
                PaymentAmount   = 100.00M,
                Currency        = SupportedCurrencies.GBP,
                Cvv             = "123"
            };

            var httpClient = new HttpClient(this._handler.Object)
            {
                BaseAddress = null
            };

            this._bankingService = new BankingService(this._bankingApiConfiguration.Object, httpClient, this._logger.Object);

            //Act - see assertion.

            //Assert
            var ex = Assert.ThrowsAsync <HttpException>(() => this._bankingService.ProcessPayment(bankDto));

            Assert.AreEqual(HttpStatusCode.InternalServerError, ex.StatusCode);
            Assert.AreEqual(Resources.ErrorMessage_BankingApiUnexpectedError, ex.Message);
            Assert.AreEqual(Resources.ErrorCode_BankingApiUnexpectedError, ex.ErrorCode);

            //Verify logging took place
            this._logger.Verify(x => x.Log(LogLevel.Error,
                                           It.IsAny <EventId>(),
                                           It.IsAny <It.IsAnyType>(),
                                           It.IsAny <Exception>(),
                                           (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Once);
        }