public TransactionDto Pay(CreditCardDto from, BankAccountDto to, double amountDollar)
        {
            if (!IsValid(from))
            {
                return(null);
            }
            var         builder     = new TransactionBuilder();
            Transaction transaction = builder
                                      .From(from.CardNumber)
                                      .To(to.Id)
                                      .AmountDollar(amountDollar)
                                      .TimeUtcNow()
                                      .Build();

            using (var db = new BankSystemContext())
            {
                Transaction        saved    = db.Transactions.Add(transaction);
                BankAccountDetails fromBank = db.BankAccounts.FirstOrDefault(bank => bank.CreditCards.Any(card => card.CardNumber == from.CardNumber));
                if (fromBank == null)
                {
                    return(null);
                }
                fromBank.Balance -= transaction.AmountDollar;
                BankAccountDetails toBank = db.BankAccounts.Find(to.Id);
                toBank.Balance += transaction.AmountDollar;

                db.SaveChanges();

                return(mapper.Map <TransactionDto>(transaction));
            }
        }
        public List <TransactionDto> GetAllTransactions()
        {
            using (var db = new BankSystemContext())
            {
                var query = from transaction in db.Transactions
                            select transaction;

                return(mapper.Map <List <TransactionDto> >(query.ToList()));
            }
        }
        public List <BankAccountDto> GetAllBankAccounts()
        {
            using (var db = new BankSystemContext())
            {
                var query = from bank in db.BankAccounts
                            select bank;

                return(mapper.Map <List <BankAccountDto> >(query.ToList()));
            }
        }
        public List <TransactionDto> TransactionHistoryOf(BankAccountDto bank)
        {
            using (var db = new BankSystemContext())
            {
                var query = from transaction in db.Transactions
                            where transaction.From.BankAccount.Id.Equals(bank.Id) || transaction.To.Id.Equals(bank.Id)
                            select transaction;

                return(mapper.Map <List <TransactionDto> >(query.ToList()));
            }
        }
        public BankAccountDto GetBankAccount(int id)
        {
            using (var db = new BankSystemContext())
            {
                var query = from bank in db.BankAccounts
                            where bank.Id == id
                            select bank;

                return(mapper.Map <BankAccountDto>(query.FirstOrDefault()));
            }
        }
        public List <CreditCardDto> GetCardsForAccount(int accountId)
        {
            using (var db = new BankSystemContext())
            {
                var query = from card in db.CreditCards
                            where card.BankAccountId.Equals(accountId)
                            select card;

                return(mapper.Map <List <CreditCardDto> >(query.ToList()));
            }
        }
        public BankAccountDto CreateEmptyBankAccount(string name)
        {
            BankAccountDetails details = new BankAccountDetails
            {
                HolderName = name,
            };

            using (var db = new BankSystemContext())
            {
                db.BankAccounts.Add(details);
                db.SaveChanges();
            }

            return(mapper.Map <BankAccountDto>(details));
        }
        public BankAccountDto DepositMoney(int bankId, double amountDollar)
        {
            if (amountDollar > 10000 || amountDollar <= 0)
            {
                return(null);
            }

            using (var db = new BankSystemContext())
            {
                BankAccountDetails bank = db.BankAccounts.Find(bankId);
                if (bank == null)
                {
                    return(null);
                }

                bank.Balance += amountDollar;

                db.SaveChanges();

                return(mapper.Map <BankAccountDto>(bank));
            }
        }
        private CreditCardDetails GenerateRandomCard(string prefix, int length, BankAccountDetails bank)
        {
            string ccnumber = prefix;

            while (ccnumber.Length < (length - 1))
            {
                double rnd = (random.NextDouble() * 1.0f - 0f);

                ccnumber += Math.Floor(rnd * 10);
            }


            // reverse number and convert to int
            var reversedCCnumberstring = ccnumber.ToCharArray().Reverse();

            var reversedCCnumberList = reversedCCnumberstring.Select(c => Convert.ToInt32(c.ToString()));

            // calculate sum //Luhn Algorithm
            int sum = 0;
            int pos = 0;

            int[] reversedCCnumber = reversedCCnumberList.ToArray();

            while (pos < length - 1)
            {
                int odd = reversedCCnumber[pos] * 2;

                if (odd > 9)
                {
                    odd -= 9;
                }

                sum += odd;

                if (pos != (length - 2))
                {
                    sum += reversedCCnumber[pos + 1];
                }

                pos += 2;
            }

            // calculate check digit
            int checkdigit =
                Convert.ToInt32((Math.Floor((decimal)sum / 10) + 1) * 10 - sum) % 10;

            ccnumber += checkdigit;

            var date = DateTime.Now.AddYears(6).AddMonths(6);


            using (var db = new BankSystemContext())
            {
                var card = new CreditCardDetails
                {
                    BankAccountId         = bank.Id,
                    CardNumber            = ccnumber,
                    CardVerificationValue = (random.Next(1000) + "").PadLeft(3),
                    Month = date.Month,
                    Year  = date.Year
                };

                db.CreditCards.Add(card);
                db.SaveChanges();

                return(card);
            }
        }
Пример #10
0
        /// <summary>
        /// checks if credit card details are valid
        /// </summary>
        /// <returns>true if this card is valid</returns>
        public bool IsValid()
        {
            var len = CardNumber.Length;

            if (len == 0)
            {
                return(false);
            }


            // check for all numbers
            if (!CardNumber.All(char.IsDigit))
            {
                return(false);
            }
            if (!CardVerificationValue.All(char.IsDigit))
            {
                return(false);
            }

            if (CardVerificationValue.Length != 3)
            {
                return(false);
            }



            // Check card not expired

            if (Month > 12)
            {
                return(false);
            }

            var time = DateTime.Now;

            if (time.Year > Year)
            {
                return(false);
            }
            if (time.Year == Year && time.Month > Month)
            {
                return(false);
            }


            using (var db = new BankSystemContext())
            {
                var query = from card in db.CreditCards
                            where card.Month == this.Month &&
                            card.Year == this.Year &&
                            card.CardNumber == this.CardNumber &&
                            card.CardVerificationValue == this.CardVerificationValue
                            select card.BankAccount;

                BankAccountDetails found = query.FirstOrDefault();

                this.BankAccount = found;

                return(found != null);
            }
        }