public void Create_ShouldReturnError_WhenExpiryDateIsInThePast(int month, int year)
        {
            var expiry = ExpiryDate.Create(month, year);

            expiry.IsSuccess.Should().Be(false);
            expiry.Error.Should().Be(Errors.ExpiredCard);
        }
Beispiel #2
0
        public void Should_not_be_equal_to_another_instance_if_they_dont_have_same_date()
        {
            var expiryDateA = ExpiryDate.Create(08, 2019, _dateProvider);
            var expiryDateB = ExpiryDate.Create(09, 2019, _dateProvider);

            expiryDateA.Should().NotBe(expiryDateB);
        }
        public void Create_ShouldReturnError_WhenMonthIsNotBetween1And12(int month)
        {
            var expiry = ExpiryDate.Create(month, DateTime.UtcNow.Year + 1);

            expiry.IsSuccess.Should().BeFalse();
            expiry.Error.Should().Be(Errors.InvalidExpiryDate);
        }
        public void Create_ShouldReturnExpiryDate(int month, int year)
        {
            var expiry = ExpiryDate.Create(month, year);

            expiry.IsSuccess.Should().BeTrue();

            var value = expiry.GetValue();

            value.Month.Should().Be(month);
            value.Year.Should().Be(year);
        }
Beispiel #5
0
        public static Payment Create(IProvideDateTime dateProvider)
        {
            var expiryDate = ExpiryDate.Create(08, 2020, dateProvider);
            var cardNumber = CardNumber.Create("4111 1111 1111 1111");
            var ccv        = Ccv.Create(123);

            var paymentId  = PaymentId.Create();
            var creditCard = CreditCard.Create(cardNumber, expiryDate, ccv);
            var amount     = Money.Create(12, Currency.EUR);

            return(new Payment(paymentId, creditCard, amount));
        }
        public async Task <PaymentId> CreatePayment(string cardNumber, int ccv, int expiryMonthDate, int expiryYearDate, double amount, Currency currency)
        {
            var card = CreditCard.Create(CardNumber.Create(cardNumber),
                                         ExpiryDate.Create(expiryMonthDate, expiryYearDate, _dateProvider),
                                         Ccv.Create(ccv));
            var money   = Money.Create(amount, currency);
            var payment = new Payment(PaymentId.Create(), card, money);

            await _paymentRepository.Save(payment);

            return(payment.Id);
        }
Beispiel #7
0
        public CreditCardTests()
        {
            // Substitutes
            var today = new DateTimeOffset(new DateTime(2019, 08, 19, 0, 0, 0, DateTimeKind.Utc));

            _dateProvider = Substitute.For <IProvideDateTime>();
            _dateProvider.UtcNow().Returns(today);

            // Shared objects
            _cardNumber = CardNumber.Create("4111 1111 1111 1111");
            _ccv        = Ccv.Create(123);
            _expiryDate = ExpiryDate.Create(09, 2019, _dateProvider);
        }
Beispiel #8
0
        public void Should_be_equal_to_another_instance_if_they_do_not_have_same_details()
        {
            var cardNumberA = CardNumber.Create("4111 1111 1111 1111");
            var ccvA        = Ccv.Create(123);
            var expiryDateA = ExpiryDate.Create(09, 2019, _dateProvider);
            var creditCardA = CreditCard.Create(cardNumberA, expiryDateA, ccvA);


            var cardNumberB = CardNumber.Create("4111 1111 1111 1111");
            var ccvB        = Ccv.Create(123);
            var expiryDateB = ExpiryDate.Create(09, 2020, _dateProvider); // Diff is here
            var creditCardB = CreditCard.Create(cardNumberB, expiryDateB, ccvB);

            creditCardA.Should().NotBe(creditCardB);
        }
Beispiel #9
0
            public override async Task <Result <Guid> > Handle(Command command, CancellationToken token = default)
            {
                // This cannot fail since we validated the entire command before
                var price      = Price.Create(command.Currency, command.Amount).GetValue();
                var number     = CardNumber.Create(command.Card.Number).GetValue();
                var expiryDate = ExpiryDate.Create(command.Card.ExpiryMonth, command.Card.ExpiryYear).GetValue();
                var cvv        = Cvv.Create(command.Card.Cvv).GetValue();

                var card = Card.Create(number, expiryDate, cvv, command.Card.HolderName);

                // Starts the async job
                return(Ok(
                           await _bus.StartAsync(
                               new MakePayment(command.MerchantId, card.GetValue(), price, command.Description ?? string.Empty), token)));
            }
Beispiel #10
0
            public Validator()
            {
                RuleFor(x => Price.Create(x.Currency, x.Amount))
                .Must(x => x.IsSuccess)
                .WithState((x, res) => res.Error);

                RuleFor(x => CardNumber.Create(x.Card.Number))
                .Must(x => x.IsSuccess)
                .WithState((x, res) => res.Error);

                RuleFor(x => ExpiryDate.Create(x.Card.ExpiryMonth, x.Card.ExpiryYear))
                .Must(x => x.IsSuccess)
                .WithState((x, res) => res.Error);

                RuleFor(x => Cvv.Create(x.Card.Cvv))
                .Must(x => x.IsSuccess)
                .WithState((x, res) => res.Error);
            }
Beispiel #11
0
        public override async Task <string> Execute(ConsumeContext <MakePayment> context)
        {
            var command = context.Message;

            var response = await _processor.ProcessAsync(
                command.Currency,
                command.Amount,
                command.CardNumber,
                command.ExpiryMonth,
                command.ExpiryYear,
                command.Cvv,
                command.CardHolderName);

            // We assume command sent through the bus are ALWAYS valid. In other words, the code below should never fail.
            var payment = Payment.Create(
                response.PaymentId,
                command.MerchantId,
                Card.Create(
                    CardNumber.Create(command.CardNumber).GetValue(),
                    ExpiryDate.Create(command.ExpiryMonth, command.ExpiryYear).GetValue(),
                    Cvv.Create(command.Cvv).GetValue(),
                    command.CardHolderName).GetValue(),
                Price.Create(command.Currency, command.Amount).GetValue(),
                command.Description);

            // This is simplistic :)
            if (response.Status == PaymentStatus.Approved)
            {
                payment.Approve();
            }
            else
            {
                payment.Decline("Payment was declined");
            }

            // Persist the payment
            await _repository.AddAsync(payment);

            // We return the Id of the created entity!
            return(response.PaymentId);
        }
        public void Date_ShouldReturnLastDayOfExpiryMonth()
        {
            var expiry = ExpiryDate.Create(1, 2099).GetValue();

            expiry.Date.Should().Be(new DateTime(2099, 1, 31));
        }
Beispiel #13
0
        public void Should_create_an_instance_if_month_and_year_are_same_of_today()
        {
            var expiryDate = ExpiryDate.Create(08, 2019, _dateProvider);

            expiryDate.Should().NotBeNull();
        }
Beispiel #14
0
        public void Should_raise_an_error_if_month_and_year_are_before_today()
        {
            Action action = () => ExpiryDate.Create(07, 2019, _dateProvider);

            action.Should().Throw <ValidationException>();
        }
Beispiel #15
0
        public void Should_raise_an_error_if_year_is_before_current_year()
        {
            Action action = () => ExpiryDate.Create(08, 2018, _dateProvider);

            action.Should().Throw <ValidationException>();
        }