Example #1
0
        public void GivenANullPaymentProcessingRequest_WhenTryingToConvertToPaymentRecord_ThenItThrowsAnArgumentException()
        {
            // When

            var exception = Assert.Throws <ArgumentException>(() => PaymentRecord.Convert(null));


            // Then

            exception.Should().NotBeNull();
            exception.Should().BeOfType <ArgumentException>();
            exception.Message.Should().Be("PaymentProcessingRequest cannot be null.");
        }
Example #2
0
        public async Task <PaymentProcessingResponse> Handle(PaymentProcessingRequest paymentProcessingRequest)
        {
            var paymentRecord = PaymentRecord.Convert(paymentProcessingRequest);

            try
            {
                await _paymentRepository.Upsert(paymentRecord);

                var bankResponse = await _bankHandler.Handle(paymentProcessingRequest);

                paymentRecord.BankTransactionId = bankResponse.TransactionId;
                paymentRecord.PaymentStatus     = bankResponse.Success ? PaymentStatus.Success : PaymentStatus.Failed;

                await _paymentRepository.Upsert(paymentRecord);
            }
            catch (BankingException bankingException)
            {
                _logger.LogError(bankingException.ToString());

                throw new PaymentProcessingException($"There was an issue when processing the transaction with the bank. Details: {bankingException.Message}");
            }
            catch (PaymentRepositoryException paymentRepositoryException)
            {
                _logger.LogError(paymentRepositoryException.ToString());

                throw new PaymentProcessingException($"There was an issue when saving the request. Details: {paymentRepositoryException.Message}");
            }
            catch (Exception exception)
            {
                _logger.LogError(exception.ToString());

                throw new PaymentProcessingException($"An unhandled error has occurred. Details: {exception.Message}");
            }

            var response = new PaymentProcessingResponse
            {
                PaymentGatewayId = paymentRecord.PaymentGatewayId,
                Success          = paymentRecord.PaymentStatus == PaymentStatus.Success
            };

            return(response);
        }
Example #3
0
        public void GivenAValidPaymentProcessingRequest_WhenTryingToConvertToPaymentRecord_ThenItReturnsAPaymentRecord()
        {
            // Given

            const string cardNumber      = "1234567812345678";
            const int    expiryMonth     = 12;
            const int    expiryYear      = 2025;
            const int    cvv             = 123;
            const double amount          = 123.45;
            const string currencyIsoCode = "GBP";

            var request = new PaymentProcessingRequest
            {
                CardNumber      = cardNumber,
                ExpiryMonth     = expiryMonth,
                ExpiryYear      = expiryYear,
                Cvv             = cvv,
                Amount          = amount,
                CurrencyIsoCode = currencyIsoCode
            };

            // When

            var response = PaymentRecord.Convert(request);


            // Then

            response.Should().NotBeNull();
            response.Should().BeOfType <PaymentRecord>();
            response.CardNumber.Should().Be(cardNumber);
            response.ExpiryMonth.Should().Be(expiryMonth);
            response.ExpiryYear.Should().Be(expiryYear);
            response.Cvv.Should().Be(cvv);
            response.Amount.Should().Be(amount);
            response.CurrencyIsoCode.Should().Be(currencyIsoCode);
            response.PaymentStatus.Should().Be(PaymentStatus.Pending);
            response.PaymentGatewayId.Should().NotBe(Guid.Empty);
        }