Ejemplo n.º 1
0
        public async Task <ActionResult <CreditCardItem> > PostCreditCardItem(CreditCardItem creditCardItem)
        {
            _logger.LogInformation("Create credit card info");
            if (!ModelState.IsValid)
            {
                _logger.LogDebug("Invalid data.");
                return(BadRequest("Invalid data."));
            }
            _context.CreditCardItems.Add(creditCardItem);

            await _context.SaveChangesAsync();

            return(NoContent());
        }
Ejemplo n.º 2
0
        private (bool isValid, string cardType) ValidateRule(CreditCardItem item)
        {
            String cardType         = "";
            int    CardNoLength     = 0;
            int    firstDigitCardNo = 0;
            bool   result           = false;
            int    expiryYear;

            CardNoLength     = item.CARD_NO.ToString().Length;
            firstDigitCardNo = Int32.Parse(item.CARD_NO.ToString().Substring(0, 1));
            expiryYear       = Int32.Parse(item.EXPIRE_DATE.ToString().Substring(2, 4));

            //get card type
            if (CardNoLength == 15 && firstDigitCardNo == 3)
            {
                cardType = "Amex";
                result   = true;
            }
            if (CardNoLength == 16)
            {
                if (firstDigitCardNo == 3)
                {
                    cardType = "JCB";
                    result   = true;
                }
                if (firstDigitCardNo == 4)
                {
                    cardType = "Visa";
                    result   = ValidateVisa(expiryYear);
                }
                if (firstDigitCardNo == 5)
                {
                    cardType = "MasterCard";
                    result   = ValidateMasterCard(expiryYear);
                }
                if (firstDigitCardNo != 3 && firstDigitCardNo != 4 && firstDigitCardNo != 5)
                {
                    cardType = "UnKnown";
                }
            }
            return(result, cardType);
        }
Ejemplo n.º 3
0
        private (bool validateresult, string errmsg) Validate(CreditCardItem item)
        {
            String errormsg     = "";
            int    CardNoLength = 0;
            int    expiryYear;
            int    expiryMonth;

            CardNoLength = item.CARD_NO.ToString().Length;
            expiryMonth  = Int32.Parse(item.EXPIRE_DATE.ToString().Substring(0, 2));
            expiryYear   = Int32.Parse(item.EXPIRE_DATE.ToString().Substring(2, 4));
            //validate length 15 or 16 digit
            if (CardNoLength != 15 && CardNoLength != 16)
            {
                errormsg = "Invalid credit card no";
                return(false, errormsg);
            }
            //validate expiry date  format
            if (item.EXPIRE_DATE.ToString().Length != 6)
            {
                errormsg = "Invalid expiry date";
                return(false, errormsg);
            }
            DateTime now = DateTime.Now;

            //check expiry month is valid
            if (expiryYear < now.Year)
            {
                errormsg = "Invalid expiry date";
                return(false, errormsg);
            }

            //check expiry month is valid
            if (expiryMonth < 0 || expiryMonth > 12)
            {
                errormsg = "Invalid expiry date";
                return(false, errormsg);
            }


            return(true, "");
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> PutCreditCardItem(int id, CreditCardItem creditCardItem)
        {
            _logger.LogInformation($"Modify credit card info for id: {id}");

            if (!ModelState.IsValid)
            {
                _logger.LogDebug("Invalid data.");
                return(BadRequest("Invalid data."));
            }

            if (id != creditCardItem.Id)
            {
                return(BadRequest());
            }

            _context.Entry(creditCardItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CreditCardItemExists(id))
                {
                    _logger.LogDebug($"Can't find credit card info for id : {id}");
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }


            return(NoContent());
        }
Ejemplo n.º 5
0
        public IActionResult CheckCreditCard(CreditCardItem item)
        {
            var    result        = Validate(item);
            bool   checkDBresult = true;
            string returnResult  = "";

            if (result.validateresult)
            {
                var ValidateRuleResult = ValidateRule(item);
                if (!ValidateRuleResult.isValid)
                {
                    returnResult = "Invalid";
                }

                //validate is true
                //set card_type to item
                if (ValidateRuleResult.cardType != "Unknown")
                {
                    item.CARD_TYPE = ValidateRuleResult.cardType;
                    //check data at DB
                    checkDBresult = _context.checkCreditCardOnDB(item);
                    if (!checkDBresult)
                    {
                        returnResult = "Does not exist";
                    }
                    else
                    {
                        returnResult = "Valid";
                    }
                }
            }
            else
            {
                return(BadRequest(returnResult));
            }
            return(Ok(new { result = returnResult, cardType = item.CARD_TYPE }));
        }