示例#1
0
        public async Task <CoinGateInvoiceReturnModel> GenerateInvoice(CoinGateInvoiceTransferModel model)
        {
            if (string.IsNullOrEmpty(model.CallbackUrl) || string.IsNullOrWhiteSpace(model.CallbackUrl))
            {
                model.CallbackUrl = _coinGateSettings.CallbackUrl;
            }

            if (string.IsNullOrEmpty(model.CancelUrl) || string.IsNullOrWhiteSpace(model.CancelUrl))
            {
                model.CancelUrl = _coinGateSettings.CallbackUrl;
            }

            if (string.IsNullOrEmpty(model.SuccessUrl) || string.IsNullOrWhiteSpace(model.SuccessUrl))
            {
                model.SuccessUrl = _coinGateSettings.CallbackUrl;
            }

            var content    = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _coinGateSettings.Token);
            var response = await httpClient.PostAsync(_coinGateSettings.Url + "/orders", content);

            var responseContent = await response.Content.ReadAsStringAsync();

            var responseObject = JsonConvert.DeserializeObject <CoinGateInvoiceReturnModel>(responseContent);

            return(responseObject);
        }
示例#2
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;
                }
            }
        }