public async Task <ActionResult> Create(CreditCardCreateDTO CreditCard)
        {
            var result = await _CreditCardService.Create(CreditCard);

            return(CreatedAtAction(
                       "GetById",
                       new { id = result.CreditCardId },
                       result));
        }
        public ServiceResponse <string> Create([FromBody] CreateCreditCardModel createModel)
        {
            var response = new ServiceResponse <string>();

            int expirationYear;

            Int32.TryParse(createModel.ExpirationYear, out expirationYear);
            int expirationMonth;

            Int32.TryParse(createModel.ExpirationMonth, out expirationMonth);
            int cvv;

            Int32.TryParse(createModel.Cvv, out cvv);

            var model = new CreditCardModel();

            model.ExpirationMonth = expirationMonth;
            model.ExpirationYear  = expirationYear;
            model.BinNumber       = createModel.CardNumber.Substring(0, Math.Min(createModel.CardNumber.Length, 6));
            model.LastFour        = createModel.CardNumber.Substring(createModel.CardNumber.Length - Math.Min(createModel.CardNumber.Length, 4));
            model.UserId          = CurrentUser.ID;
            model.IsActive        = true;
            model.IsDeleted       = false;
            var validationResult = _creditCardModelValidator.Validate(model);

            if (validationResult.IsValid)
            {
                var token = _braintreeWarpper.Tokenize(CurrentUser.FirstName, CurrentUser.LastName, CurrentUser.UserName, createModel.CardNumber, expirationYear, expirationMonth, cvv);
                model.Token = token;

                if (!string.IsNullOrWhiteSpace(token))
                {
                    var entity          = new CreditCardConverter().Convert(model);
                    var serviceResponse = _creditCardService.Create(entity);
                    response.IsSuccessed = serviceResponse.IsSuccessed;
                    response.Errors      = serviceResponse.Errors;
                    response.Data        = model.Token;
                }
                else
                {
                    response.IsSuccessed = false;
                    response.Errors.Add(new ServiceError()
                    {
                        Code = "2009062323", InnerMessage = "Token can not created.", Message = "Credit card can not accepted!"
                    });
                }
            }
            else
            {
                _logger.Error("{source} {template} {logtype} {ValidationError}", "controller", "CreditCardEntity", "validationerror", validationResult.Errors);
                response.IsSuccessed = false;
                response.Errors      = new ValidationFailureConverter().Convert(validationResult.Errors.ToList());
            }

            return(response);
        }
 public ActionResult <CreditCard> Create(CreditCardCreateDto CreditCardCreateDto)
 {
     try
     {
         return(service.Create(CreditCardCreateDto));
     }
     catch (Exception error)
     {
         return(Conflict(error.Message));
     }
 }
Ejemplo n.º 4
0
        public async void Create()
        {
            var mock  = new ServiceMockFacade <ICreditCardRepository>();
            var model = new ApiCreditCardRequestModel();

            mock.RepositoryMock.Setup(x => x.Create(It.IsAny <CreditCard>())).Returns(Task.FromResult(new CreditCard()));
            var service = new CreditCardService(mock.LoggerMock.Object,
                                                mock.RepositoryMock.Object,
                                                mock.ModelValidatorMockFactory.CreditCardModelValidatorMock.Object,
                                                mock.BOLMapperMockFactory.BOLCreditCardMapperMock,
                                                mock.DALMapperMockFactory.DALCreditCardMapperMock,
                                                mock.BOLMapperMockFactory.BOLSalesOrderHeaderMapperMock,
                                                mock.DALMapperMockFactory.DALSalesOrderHeaderMapperMock);

            CreateResponse <ApiCreditCardResponseModel> response = await service.Create(model);

            response.Should().NotBeNull();
            mock.ModelValidatorMockFactory.CreditCardModelValidatorMock.Verify(x => x.ValidateCreateAsync(It.IsAny <ApiCreditCardRequestModel>()));
            mock.RepositoryMock.Verify(x => x.Create(It.IsAny <CreditCard>()));
        }