public string OnFailure_non_async(int customerId, decimal moneyAmount)
        {
            var paymentGateway = new PaymentGateway();
            var database       = new Database();

            return(GetById(customerId)
                   .ToResult("Customer with such Id is not found: " + customerId)
                   .Tap(customer => customer.AddBalance(moneyAmount))
                   .Check(customer => paymentGateway.ChargePayment(customer, moneyAmount))
                   .Bind(
                       customer => database.Save(customer)
                       .OnFailure(() => paymentGateway.RollbackLastTransaction()))
                   .Finally(result => result.IsSuccess ? "OK" : result.Error));
        }
Пример #2
0
        public async Task <string> OnFailure_async(int customerId, decimal moneyAmount)
        {
            var paymentGateway = new PaymentGateway();
            var database       = new Database();

            return(await GetById(customerId)
                   .ToResult("Customer with such Id is not found: " + customerId)
                   .OnSuccess(customer => customer.AddBalance(moneyAmount))
                   .OnSuccess(customer => paymentGateway.ChargePayment(customer, moneyAmount).Map(() => customer))
                   .OnSuccess(
                       customer => database.Save(customer)
                       .OnFailure(() => paymentGateway.RollbackLastTransactionAsync()))
                   .OnBoth(result => result.IsSuccess ? "OK" : result.Error));
        }
Пример #3
0
        private void TakeMoney(decimal amount)
        {
            string error = _atm.CanTakeMoney(amount);

            if (error != string.Empty)
            {
                NotifyClient(error);
            }
            decimal amountWithCommision = _atm.CalculateAmountWithCommision(amount);

            _paymentGateway.ChargePayment(amountWithCommision);
            _atm.TakeMoney(amount);
            _repository.Save(_atm);
            NotifyClient("You have taken " + amount.ToString("C2"));
        }
Пример #4
0
        private void TakeMoney(decimal amount)
        {
            string error = _atm.CanTakeMoney(amount);

            if (error != string.Empty)
            {
                NotifyClient(error);
                return;
            }

            decimal amountWithCommission = _atm.CalculateAmountWithComission(amount);

            _paymentGateway.ChargePayment(amountWithCommission);
            _atm.TakeMoney(amount);
            _repository.Save(_atm);

            //HeadOffice headOffice = GetHeadOfficeInstance();
            //headOffice.Balance += amountWithCommission;
            //_officeRepository.Save(headOffice);

            NotifyClient("You have taken " + amount.ToString("C2"));
        }