public string RefillBalance(int customerId, decimal moneyAmount) { //if(!IsMoneyAmountValid(moneyAmount)) //{ // _logger.Log("Money amount is invalid"); // return "Money amount is invalid"; //} //Customer customer = _database.Get(customerId); //if(customer == null) //{ // _logger.Log("Customer is not found"); // return "Customer is not found"; //} //customer.Balance += moneyAmount; //try //{ // _paymentGateway.ChargePayment(customer.BillingInfo, moneyAmount); //} //catch(ChargePaymentException e) //{ // _logger.Log("Unable to charge the credit card"); // return "Unable to connect to the database"; //} //try //{ // _database.Save(customer); //} //catch (SqlException e) //{ // _paymentGateway.RollbackLastTransaction(); // _logger.Log("Unable to charge the credit card"); // return "Unable to connect to the database"; //} //_logger.Log("Ok"); //return "OK"; var moneyToCharge = MoneyToCharge.Create(moneyAmount); var customer = _database.Get(customerId).ToResult("Customer is not found"); return(Result.Combine(moneyToCharge, customer) .OnSuccess(() => customer.Value.AddBalance(moneyToCharge.Value)) .OnSuccess(() => _paymentGateway.ChargePayment(customer.Value.BillingInfo, moneyToCharge.Value)) .OnSuccess(() => _database.Save(customer.Value).OnFailure(() => _paymentGateway.RollbackLastTransaction())) .OnBoth(result => Log(result)) .OnBoth(result => result.IsSuccess ? "Ok" : result.ErrorMessage)); }
public void AddBalance(MoneyToCharge amount) { Balance += amount; }