public void When_Making_Transaction_With_Credit_Card_Token_Should_Not_Throw_Exception()
        {
            //Arrange 
            UserCreditCard userCreditCard = new UserCreditCard
            {
                UserId = 1,
                AmountInCents = 1000,
                IsSavingCreditCardInToken = true,
                InstallmentCount = 1
            };

            Guid instantBuyKey = new Guid("33B92AFF-4F90-4F6A-A0FB-18FA50DBA364");

            CreditCardToken creditCardToken = new CreditCardToken
            {
                UserId = 1,
                CreditCardTokenId = instantBuyKey
            };

            var creditCardTokenRepositoryMock = new Mock<ICreditCardTokenRepository>();

            creditCardTokenRepositoryMock.Setup(x => x.GetCreditCardTokenByUserId(1)).Returns(creditCardToken);

            ICreditCardTokenService creditCardTokenService = new CreditCardTokenService(creditCardTokenRepositoryMock.Object);
            ICreditCardCompany mundipaggCompany = new MundipaggCompany(creditCardTokenService);

            //Act
            //Assert            
            mundipaggCompany.MakeTransactionWithCreditCardToken(userCreditCard);
        }
        public void When_Making_Transaction_Should_Not_Throw_Exception()
        {
            //Arrange 
            UserCreditCard userCreditCard = new UserCreditCard
            {
                UserId = 1,
                AmountInCents = 1000,
                IsSavingCreditCardInToken = false,
                CreditCardNumber = "4111111111111111",
                CreditCardBrand = CreditCardBrandEnum.Visa,
                ExpMonth = 10,
                ExpYear = 22,
                HolderName = "LUKE SKYWALKER",
                SecurityCode = "123",
                InstallmentCount = 1
            };

            var creditCardTokenRepositoryMock = new Mock<ICreditCardTokenRepository>();

            ICreditCardTokenService creditCardTokenService = new CreditCardTokenService(creditCardTokenRepositoryMock.Object);
            ICreditCardCompany mundipaggCompany = new MundipaggCompany(creditCardTokenService);

            //Act
            //Assert       
            mundipaggCompany.MakeTransaction(userCreditCard);
        }
        public void MakeTransactionWithCreditCardToken(UserCreditCard userCreditCardInfo)
        {
            Guid instantBuyKey = GetInstantBuyKeyByUserId(userCreditCardInfo.UserId);

            try
            {
                var transaction = CreateCreditCardTransactionWithToken(userCreditCardInfo, instantBuyKey);

                Collection<CreditCardTransaction> CreditCardTransactionCollection =
                    new Collection<CreditCardTransaction>(new CreditCardTransaction[] { transaction });

                var createSaleRequest = CreateSaleRequest(CreditCardTransactionCollection);

                var serviceClient = new GatewayServiceClient(_merchantKey, _hostUri);

                var httpResponse = serviceClient.Sale.Create(createSaleRequest);

                // API response code
                ResponseProcess(userCreditCardInfo, httpResponse);
            }
            catch (Exception)
            {
                throw;
            }

        }
        public UserCreditCard MapToCreditCardInfo(UserCreditCardViewModel model)
        {
            UserCreditCard creditCardInfo = new UserCreditCard
            {
                IsSavingCreditCardInToken = model.IsSavingCreditCardInToken,
                AmountInCents = model.AmountInCents,
                CreditCardNumber = model.CreditCardNumber,
                CreditCardBrand = (CreditCardBrandEnum)model.CreditCardBrand,
                ExpMonth = model.ExpMonth,
                ExpYear = model.ExpYear,
                SecurityCode = model.SecurityCode,
                HolderName = model.HolderName,
                InstallmentCount = model.InstallmentCount
            };

            return creditCardInfo;
        }
        public void When_Making_Transaction_With_Token_And_User_Does_Not_Have_A_Credit_Card_Saved_Should_Throw_CreditCardTokenException()
        {
            //Arrange 
            UserCreditCard userCreditCard = new UserCreditCard
            {
                UserId = 1
            };

            CreditCardToken creditCardToken = null;

            var creditCardTokenRepositoryMock = new Mock<ICreditCardTokenRepository>();
            
            creditCardTokenRepositoryMock.Setup(x => x.GetCreditCardTokenByUserId(1)).Returns(creditCardToken);

            ICreditCardTokenService creditCardTokenService = new CreditCardTokenService(creditCardTokenRepositoryMock.Object);
            ICreditCardCompany mundipaggCompany = new MundipaggCompany(creditCardTokenService);

            //Act
            //Assert            
            Assert.Throws<CreditCardTokenException>(() => mundipaggCompany.MakeTransactionWithCreditCardToken(userCreditCard));
        }
 private void ResponseProcess(UserCreditCard userCreditCardInfo, HttpResponse<CreateSaleResponse> httpResponse)
 {
     var createSaleResponse = httpResponse.Response;
     if (httpResponse.HttpStatusCode == HttpStatusCode.Created)
     {
         foreach (var creditCardTransaction in createSaleResponse.CreditCardTransactionResultCollection)
         {
             if (creditCardTransaction.CreditCardTransactionStatus == CreditCardTransactionStatusEnum.NotAuthorized)
             {
                 throw new TransactionNotAuthorizedException();
             }
                 
             if (userCreditCardInfo.IsSavingCreditCardInToken)
             {
                 Guid instantBuyKey = creditCardTransaction.CreditCard.InstantBuyKey;
                 SaveTokenCreditCard(instantBuyKey, userCreditCardInfo.UserId);
             }
         }
     }
     else
     {
         if (createSaleResponse.ErrorReport != null)
         {
             foreach (ErrorItem errorItem in createSaleResponse.ErrorReport.ErrorItemCollection)
             {
                 throw new ErrorTransactionException(errorItem.Description);
             }
         }
     }
 }
        private CreditCardTransaction CreateCreditCardTransactionWithToken(UserCreditCard userCreditCardInfo, Guid instantBuyKey)
        {
            var transaction = new CreditCardTransaction()
            {
                AmountInCents = userCreditCardInfo.AmountInCents * 100,
                CreditCard = new CreditCard()
                {
                    InstantBuyKey = instantBuyKey
                },
                InstallmentCount = userCreditCardInfo.InstallmentCount
            };

            return transaction;
        }
        private CreditCardTransaction CreateCreditCardTransaction(UserCreditCard userCreditCardInfo)
        {
            var transaction = new CreditCardTransaction()
            {
                AmountInCents = userCreditCardInfo.AmountInCents  * 100,
                CreditCard = new CreditCard()
                {
                    CreditCardNumber = userCreditCardInfo.CreditCardNumber,
                    CreditCardBrand = userCreditCardInfo.CreditCardBrand,
                    ExpMonth = userCreditCardInfo.ExpMonth,
                    ExpYear = userCreditCardInfo.ExpYear,
                    SecurityCode = userCreditCardInfo.SecurityCode,
                    HolderName = userCreditCardInfo.HolderName
                },
                InstallmentCount = userCreditCardInfo.InstallmentCount
            };

            return transaction;
        }