Пример #1
0
        public async Task <CoinGateBillModel> CreateUserBilling(long userId, long txId)
        {
            using (var transaction = await _context.Database.BeginTransactionAsync())
            {
                try
                {
                    var tokenTransaction = await _context.UserTokenTransactions
                                           .Include(tt => tt.TokenBillings)
                                           .FirstOrDefaultAsync(tt => tt.Id == txId && tt.CustomerId == userId && tt.IsPurchaseRequest);

                    if (tokenTransaction.TokenBillings.Where(tb => tb.InvoiceFullyPaid).LongCount() > 0 &&
                        tokenTransaction.ConfirmedDate >= tokenTransaction.AddedDate)
                    {
                        throw new TokenTransactionAlreadyConfirmedException();
                    }

                    if (tokenTransaction.TokenBillings.Where(tb => tb.CancelledDate < tb.CreatedDate &&
                                                             !tb.InvoiceFullyPaid).LongCount() > 0)
                    {
                        throw new PendingTokenBillingExistsException();
                    }

                    if (tokenTransaction.CancelledDate >= tokenTransaction.AddedDate)
                    {
                        throw new TokenTransactionAlreadyCancelledException();
                    }

                    var coinGateInvoiceModel = new CoinGateInvoiceTransferModel
                    {
                        OrderId         = tokenTransaction.Id.ToString(),
                        PriceAmount     = tokenTransaction.Amount * TokenRate,
                        PriceCurrency   = "USD",
                        ReceiveCurrency = "BTC",
                        Title           = "Pumpk1n Token Purchase Invoice",
                        Description     = $"Receipt for purchase of {tokenTransaction.Amount} PKN"
                    };

                    var coinGateInvoiceReturnModel = await _tokenHelper.GenerateInvoice(coinGateInvoiceModel);

                    var tokenBilling =
                        _mapper.Map <CoinGateInvoiceReturnModel, TokenBilling>(coinGateInvoiceReturnModel);
                    tokenBilling.CreatedDate            = DateTime.UtcNow;
                    tokenBilling.UserTokenTransactionId = tokenTransaction.Id;
                    tokenBilling.Name        = coinGateInvoiceModel.Title;
                    tokenBilling.Description = coinGateInvoiceModel.Description;

                    _context.TokenBillings.Add(tokenBilling);
                    await _context.SaveChangesAsync();

                    transaction.Commit();

                    var tokenBillingModel = _mapper.Map <TokenBilling, CoinGateBillModel>(tokenBilling);
                    return(tokenBillingModel);
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }