コード例 #1
0
        public ServiceResponse <bool> Create([FromBody] CreateTransactionModel createModel)
        {
            var response = new ServiceResponse <bool>();

            var creditCardModel = _creditCardService.GetByToken(createModel.Token);

            if (creditCardModel.IsSuccessed)
            {
                TransactionModel model = new TransactionConverter().ConvertCreateModel(createModel);
                decimal          amount;
                decimal.TryParse(createModel.Amount, out amount);

                if (_braintreeWarpper.Sale(amount, createModel.Token))
                {
                    model.IsActive     = true;
                    model.IsDeleted    = false;
                    model.CreditCardId = creditCardModel.Data.ID;

                    var validationResult = _transactionModelValidator.Validate(model);
                    if (validationResult.IsValid)
                    {
                        var entity          = new TransactionConverter().Convert(model);
                        var serviceResponse = _transactionService.Create(entity);
                        response.IsSuccessed = serviceResponse.IsSuccessed;
                        response.Errors      = serviceResponse.Errors;
                        response.Data        = true;
                    }
                    else
                    {
                        _logger.Error("{source} {template} {logtype} {ValidationError}", "controller", "TransactionEntity", "validationerror", validationResult.Errors);
                        response.IsSuccessed = false;
                        response.Errors      = new ValidationFailureConverter().Convert(validationResult.Errors.ToList());
                    }
                }
                else
                {
                    response.IsSuccessed = false;
                    response.Errors.Add(new ServiceError()
                    {
                        InnerMessage = "Payment Error.", Message = "There is an error with payment! Please contact.", Code = "2009062057"
                    });
                }
            }
            else
            {
                response.IsSuccessed = false;
                response.Errors.Add(new ServiceError()
                {
                    InnerMessage = "Token not found in the credit card table.", Message = "There is an error with payment! Please contact.", Code = "2009062304"
                });
            }

            return(response);
        }