public void Create_ShouldReturnError_WhenCardNumberIsInvalid(string number)
        {
            var cardNumber = CardNumber.Create(number);

            cardNumber.IsSuccess.Should().BeFalse();
            cardNumber.Error.Should().Be(Errors.InvalidCardNumber);
        }
        public void Should_not_be_equal_to_another_instance_if_they_dont_have_same_number()
        {
            var cardNumberA = CardNumber.Create("5500 0000 0000 0004");
            var cardNumberB = CardNumber.Create("4111 1111 1111 1111");

            cardNumberA.Should().NotBe(cardNumberB);
        }
        public void Mask_ShouldReturnMaskedCardNumber()
        {
            var cardNumber = CardNumber.Create("4111 1111 1111 1111");

            var value = cardNumber.GetValue();

            value.Mask(3).Should().Be("*************111");
        }
        public void Mask_ShouldReturnEntirelyMaskedCardNumber_WhenNumberOfDigitsShownIsGreaterThanCardNumberLength()
        {
            var cardNumber = CardNumber.Create("4111 1111 1111 1111");

            var value = cardNumber.GetValue();

            value.Mask(30).Should().Be("****************");
        }
        public void Should_mask_a_card_number()
        {
            var cardNumber = CardNumber.Create("4111 1111 1111 1111");

            var masked = cardNumber.MaskCardNumber();

            masked.Should().Be("XXXX XXXX XXXX 1111");
        }
        public void Mask_ShouldThrowArgumentException_WhenNumberOfDigitsShownIsLowerOrEqualTo0(int number)
        {
            var cardNumber = CardNumber.Create("4111 1111 1111 1111");
            var value      = cardNumber.GetValue();

            Action action = () => value.Mask(number);

            action.Should().Throw <ArgumentException>().WithMessage("Number of digits to show must be greater than 0.");
        }
        public void Create_ShouldReturnCardNumberWithOnlyDigits()
        {
            var cardNumber = CardNumber.Create("4111 1111 1111 1111");

            cardNumber.IsSuccess.Should().BeTrue();

            var value = cardNumber.GetValue();

            value.Value.Should().Be("4111111111111111");
        }
        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);
        }
Exemple #9
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));
        }
Exemple #10
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);
        }
Exemple #11
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);
        }
Exemple #12
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)));
            }
Exemple #13
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);
            }
Exemple #14
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 Create_ShouldThrowArgumentNullException_WhenCardNumberIsNull()
        {
            Action action = () => CardNumber.Create(null);

            action.Should().Throw <ArgumentNullException>().WithMessage("*number*");
        }
        public void Should_create_an_instance_when_card_number_is_correct()
        {
            var cardNumber = CardNumber.Create("4111111111111111");

            cardNumber.Should().NotBeNull();
        }
        public void Should_raise_an_error_when_the_card_number_has_alpha_char()
        {
            Action action = () => CardNumber.Create("4111 A111 1111 1111");

            action.Should().Throw <ValidationException>();
        }
        public void Should_replace_spaces_in_credit_card_number()
        {
            var cardNumber = CardNumber.Create("4111 1111 1111 1111");

            cardNumber.Value.Should().NotContain(" ");
        }