public async Task ProcessPayment(Guid userId, decimal amount, WalletTransactionType type) { if (type != WalletTransactionType.ServiceFee) { throw new NotImplementedException(); } using (var transaction = await context.Database.BeginTransactionAsync()) { var wallet = await context.Wallets.FirstOrDefaultAsync(w => w.UserId == userId && w.Currency == Currency.SPX); if (wallet == null) { wallet = new Wallet() { UserId = userId, Amount = 0, Currency = Currency.SPX, }; context.Wallets.Add(wallet); await context.SaveChangesAsync(); } context.Add(new WalletTransaction { Id = Guid.NewGuid(), Amount = amount, DateCreated = DateTime.UtcNow, Currency = wallet.Currency, Type = type, Status = WalletTransactionStatus.Compleated, Wallet = wallet, WalletId = wallet.Id }); wallet.Amount += amount; await context.SaveChangesAsync(); transaction.Commit(); } }
private void ProcessPayment(Wallet wallet, BlockchainTransaction blockchainTransaction) { wallet.Amount += blockchainTransaction.Amount; if (blockchainTransaction.Status == BlockchainTransactionStatus.ConfirmedAndValidated) { context.Add(new WalletTransaction { Id = Guid.NewGuid(), Amount = blockchainTransaction.Amount, DateCreated = DateTime.UtcNow, Currency = wallet.Currency, BlockchainTransaction = blockchainTransaction, BlockchainTransactionId = blockchainTransaction.Id, Type = WalletTransactionType.WalletDeposit, Status = WalletTransactionStatus.Compleated, Wallet = wallet, WalletId = wallet.Id }); } }
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); }