public async Task <(Guid requestId, string withdrawalCode)> CreateWithdrawalRequest(CreateWithdrawalRequestModel model) { logger.LogInformation($"{model} Starting..."); decimal amountCommission = 1m; var finalAmount = model.Amount - amountCommission; if (model.Amount <= 0 || finalAmount <= 0) { throw new ArgumentException("ErrorAmountMustBeGreaterZero"); } var wallet = await context.Wallets.FirstAsync(x => x.Currency == model.Currency && x.UserId == model.UserId); if (wallet.Amount < model.Amount) { throw new ArgumentException("ErrorNotEnoughMoney"); } if (wallet.Currency != Currency.SPX) { throw new ArgumentException($"Withdrawal requests from {wallet.Currency} not allowed"); } wallet.Amount -= model.Amount; var code = new PasswordGenerator(20).IncludeLowercase().IncludeUppercase().IncludeNumeric().Next(); var walletTransaction = new WalletTransaction { Id = Guid.NewGuid(), Amount = model.Amount, DateCreated = DateTime.UtcNow, Currency = Currency.SPX, Type = WalletTransactionType.WalletWithdraw, WithdrawAddress = model.Wallet, Status = WalletTransactionStatus.New, Wallet = wallet, }; context.Add(walletTransaction); var request = new WithdrawalRequest { Id = Guid.NewGuid(), Amount = model.Amount, AmountWithCommission = finalAmount, Currency = model.Currency, UserId = model.UserId, Status = WithdrawalRequestStatus.New, Wallet = model.Wallet, Code = code, DateCreate = DateTime.UtcNow, IsApprovedByManager = false, IsApprovedByUser = false, WalletTransactionId = walletTransaction.Id }; context.Add(request); await context.SaveChangesAsync(); return(request.Id, code); }
public async Task <IActionResult> CreateWithdrawalRequest([FromServices] IFinService finService, [FromBody] CreateWithdrawalRequestModel model) { model.UserId = CurrentUserId; var CurrentUser = GetCurrentUser(); if (!CurrentUser.TwoFactorEnabled) { return(BadRequest("Two-factor authentication required")); } var is2faTokenValid = await userManager.VerifyTwoFactorTokenAsync(CurrentUser, userManager.Options.Tokens.AuthenticatorTokenProvider, ClearCode(model.TwoFactorCode)); if (!is2faTokenValid) { return(BadRequest("Verification code is invalid")); } try { var(requestId, withdrawalCode) = await finService.CreateWithdrawalRequest(model); return(Ok(requestId)); } catch (ArgumentException e) { return(BadRequest(e.Message)); } //if (!result.IsSuccess) // return BadRequest(ErrorResult.GetResult(result)); //var callbackUrl = Url.ConfirmWithdrawalRequestCallbackLink(result.Data.Item1, result.Data.Item2); //var email = emailGenerator.GenerateConfirmWithdraw(callbackUrl, CurrentUser.GetFormattedName(), model.Amount, model.Currency, model.Wallet); //emailSender.SendEmail(CurrentUser.Email, email.Item1, email.Item2, email.Item2); //if (Constants.NotificationWithdrawalRequestsEmails.Any()) //{ // var text = $"<p>User: <strong>{CurrentUser.GetFormattedName()} ({CurrentUser.Email})</strong></p>" + // $"<p>Amount: <strong>{model.Amount} {model.Currency}</strong></p>" + // $"<p>Address: <strong>{model.Wallet}</strong></p>"; // foreach (var nEmail in Constants.NotificationWithdrawalRequestsEmails) // { // emailSender.SendEmail(nEmail, "GenesisMarkets - New withdrawal request", text, text); // } //} }