public IActionResult Checkout([FromBody] SearchCardCheckoutOption options) { var result = _card.Checkout(options); if (!result.IsSuccessful()) { return(result.ToActionResult()); } return(Ok()); }
public void Check_Card_Success() { SearchCardCheckoutOption option = new SearchCardCheckoutOption { Amount = 20, CardNumber = "7229291478892443", ExpirationMonth = 04, ExpirationYear = 2027 }; var cardResult = _cards.Checkout(option); Assert.Equal(ApiResultCode.Success, cardResult.Code); }
public ApiResult <Card> Checkout(SearchCardCheckoutOption options) { if (options == null) { return(ApiResult <Card> .UpdateFailed( code : ApiResultCode.BadRequest, errorText : "No valid checkout card info")); } // 1. card exists var cardResult = GetCardbyNumber(options.CardNumber); if (!cardResult.IsSuccessful()) { return(ApiResult <Card> .UpdateFailed( code : cardResult.Code, errorText : cardResult.ErrorText)); } var card = cardResult.Data; // get ref to card // 2. Expiration if (card.Expiration.ToString("MMyyyy") != $"{options.ExpirationMonth.ToString("00")}{options.ExpirationYear.ToString("0000")}") { return(new ApiResult <Card>() { Code = ApiResultCode.BadRequest, ErrorText = $"Expiration date {options.ExpirationMonth.ToString("00")}/{options.ExpirationYear.ToString("0000")} not much card expiration !" }); } // 3. Card Active if (!card.Active) { return(new ApiResult <Card>() { Code = ApiResultCode.BadRequest, ErrorText = $"Card {card.CardNumber} is inactive" }); } // 4. Account has balance. For simplicity use only the first account if (card.Accounts.Count == 0) { return(new ApiResult <Card>() { Code = ApiResultCode.BadRequest, ErrorText = $"Card {card.CardNumber} has no related Accounts" }); } var account = card.Accounts[0]; if (account.Balance < options.Amount) { return(new ApiResult <Card>() { Code = ApiResultCode.BadRequest, ErrorText = $"Not enough balance in your Account" }); } account.Balance -= options.Amount; _dbContext.Update(account); _dbContext.SaveChanges(); return(new ApiResult <Card>() { Data = card }); }