Example #1
0
        public IActionResult Add([FromBody] CreditCardInfo cardInfo)
        {
            try
            {
                if (cardInfo.CardNumber > 9999999999999999)
                {
                    return(BadRequest("Card number should have maximum of 16 characters"));
                }

                if (cardInfo.CVV > 99999)
                {
                    return(BadRequest("CVV should have maximum of 5 characters"));
                }

                IssuedToken issuedToken = new IssuedToken()
                {
                    RegistrationDate = DateTime.UtcNow
                };
                issuedToken.Token = string.Format("{0}{1:yyyy}{1:MM}{1:dd}{1:HH}{1:mm}", cardInfo.CardNumber, issuedToken.RegistrationDate);
                issuedToken.Token = Transformations.Rotate(Transformations.AbsoluteDifference(issuedToken.Token, 5), cardInfo.CVV);

                _dbContext.IssuedTokens.Add(issuedToken);
                _dbContext.SaveChanges();

                return(new JsonResult(new { registration_date = issuedToken.RegistrationDate, token = issuedToken.Token }));
            }
            catch (Exception ex)
            {
                /* log ex message */
                return(BadRequest("Error processing credit card."));
            }
        }