Esempio n. 1
0
        public void Setup()
        {
            _bankHttpClientMock = new Mock <IBankHttpClient>();
            _paymentProcessWriteRepositoryMock = new Mock <IPaymentProcessWriteRepository>();
            _loggerMock            = new Mock <ILogger <PaymentProcessCommand> >();
            _paymentRuleEngineMock = new Mock <IPaymentRuleEngine>();
            _encryptionClientMock  = new Mock <IEncryptionClient>();

            _defaultRequest = new PaymentProcessRequest
            {
                Amount           = 5,
                Currency         = Currency.USD,
                CardNumber       = "4024007143288669",
                CardType         = CardType.Visa,
                ContextId        = Guid.NewGuid(),
                ExpirationMonth  = 12,
                ExpirationYear   = 2070,
                CardHolderName   = "Darth Vader",
                CVV              = "322",
                RegistrationTime = DateTime.Now
            };

            _processCommand = new PaymentProcessCommand(
                _paymentProcessWriteRepositoryMock.Object,
                _bankHttpClientMock.Object,
                _paymentRuleEngineMock.Object,
                _loggerMock.Object,
                _encryptionClientMock.Object);
        }
        public async Task Handle_ShouldSavePaymentWithAReason_WhenPaymentProcessFailed()
        {
            var request = new PaymentProcessCommand();
            var paymentProcessHandler = CreatePaymentProcessHandler();

            const string status           = "Failed";
            var          expectedResponse = new PaymentProcessErrorResponse
            {
                TransactionId = "123456789", Status = status, Reason = "Something went wrong"
            };

            mockBank.Setup(x => x.ProcessPayment(It.IsAny <Payment>()))
            .ReturnsAsync(expectedResponse);
            mockEfRepository.Setup(x => x.AddAsync(It.IsAny <Payment>())).ReturnsAsync(new Payment
            {
                Id                = Guid.NewGuid(),
                CardNumber        = "1234-1234-1234-4568",
                TransactionId     = expectedResponse.TransactionId,
                TransactionStatus = expectedResponse.Status,
                Reason            = expectedResponse.Reason
            });

            var response = await paymentProcessHandler.Handle(request, default);

            response.ShouldNotBeNull();
            var result = response.ShouldBeOfType <PaymentProcessResponse>();

            result.Status.ShouldBeSameAs(status);
            mockBank.Verify(x => x.ProcessPayment(It.IsAny <Payment>()), Times.AtLeastOnce());
            mockEfRepository.Verify(x => x.AddAsync(It.Is <Payment>(p => !string.IsNullOrWhiteSpace(p.Reason))),
                                    Times.AtLeastOnce);
        }
        public async Task Handle_ShouldSavePayment_WhenPaymentProcessCompleted()
        {
            var request = new PaymentProcessCommand();
            var paymentProcessHandler = CreatePaymentProcessHandler();

            var expectedResponse = new PaymentProcessResponse {
                TransactionId = "123456789", Status = "Completed"
            };

            mockBank.Setup(x => x.ProcessPayment(It.IsAny <Payment>()))
            .ReturnsAsync(expectedResponse);
            mockEfRepository.Setup(x => x.AddAsync(It.IsAny <Payment>())).ReturnsAsync(new Payment
            {
                Id                = Guid.NewGuid(),
                CardNumber        = "1234-1234-1234-4568",
                TransactionId     = expectedResponse.TransactionId,
                TransactionStatus = expectedResponse.Status
            });

            var response = await paymentProcessHandler.Handle(request, default);

            response.ShouldNotBeNull();
            response.ShouldBeOfType <PaymentProcessResponse>();
            mockBank.Verify(x => x.ProcessPayment(It.IsAny <Payment>()), Times.AtLeastOnce());
            mockEfRepository.Verify(x => x.AddAsync(It.IsAny <Payment>()),
                                    Times.AtLeastOnce);
        }
Esempio n. 4
0
        public async Task <IActionResult> Post([FromBody] PaymentProcessCommand command)
        {
            var result = await mediator.Send(command);

            using (logger.BeginScope(new Dictionary <string, object>
            {
                { "paymentId", result.PaymentId }
            }))
            {
                logger.LogInformation("Payment processed");
                return(Ok(result));
            }
        }