public async Task <IActionResult> Sale([FromBody] GatewaySaleParams model) { _logger.LogInformation("API Sale: {@params}", model); var res = await _paymentService.Sale(model); return(Ok(res)); }
async Task <Payment> GraftSale(GatewaySaleParams model, Terminal terminal) { var graftRate = await _rateCache.GetRateToUsd("GRFT"); var graftAmount = model.SaleAmount / graftRate; var payment = new Payment { Id = Guid.NewGuid().ToString(), TransactionDate = DateTime.UtcNow, Status = PaymentStatus.New, TerminalId = terminal.Id, StoreId = terminal.StoreId, ServiceProviderId = terminal.ServiceProviderId, SaleAmount = model.SaleAmount, SaleCurrency = model.SaleCurrency, PayToSaleRate = graftRate, GraftToSaleRate = graftRate, PayCurrency = model.PayCurrency, PayAmount = graftAmount, ServiceProviderFee = 0, ExchangeBrokerFee = 0, MerchantAmount = graftAmount, SaleDetails = model.SaleDetails }; var dapiParams = new DapiSaleParams { PaymentId = payment.Id, SaleDetails = model.SaleDetails, Address = terminal.Merchant.WalletAddress, Amount = GraftConvert.ToAtomicUnits(payment.PayAmount) }; //todo //var dapiResult = await _dapi.Sale(dapiParams); //payment.Status = PaymentStatus.Waiting; //payment.BlockNumber = dapiResult.BlockNumber; //payment.PayWalletAddress = $"{payment.Id};{terminal.Merchant.WalletAddress};{payment.PayAmount:N8};{dapiResult.BlockNumber}"; return(payment); }
async Task <Payment> AltcoinSale(GatewaySaleParams model, Terminal terminal) { var brokerParams = new BrokerExchangeParams { FiatCurrency = model.SaleCurrency, SellFiatAmount = model.SaleAmount, SellCurrency = model.PayCurrency, BuyCurrency = "GRFT", WalletAddress = terminal.ServiceProvider.WalletAddress }; var brokerResult = await _broker.Exchange(brokerParams); var payment = new Payment() { Id = brokerResult.ExchangeId, TransactionDate = DateTime.UtcNow, Status = PaymentStatus.New, TerminalId = terminal.Id, StoreId = terminal.StoreId, ServiceProviderId = terminal.ServiceProviderId, SaleAmount = model.SaleAmount, SaleCurrency = model.SaleCurrency, PayToSaleRate = brokerResult.SellToUsdRate, GraftToSaleRate = brokerResult.GraftToUsdRate, PayCurrency = brokerResult.SellCurrency, PayAmount = brokerResult.SellAmount, PayWalletAddress = brokerResult.PayWalletAddress, //ServiceProviderFee = brokerResult.ServiceProviderFee, ExchangeBrokerFee = brokerResult.ExchangeBrokerFeeAmount, //MerchantAmount = brokerResult.MerchantAmount, SaleDetails = model.SaleDetails, }; return(payment); }
public async Task <GatewaySaleResult> Sale(GatewaySaleParams model) { _logger.LogInformation("API Sale: {@params}", model); if (model.SaleAmount <= 0) { throw new ApiException(ErrorCode.InvalidAmount); } if (string.IsNullOrWhiteSpace(model.SaleCurrency)) { throw new ApiException(ErrorCode.SaleCurrencyEmpty); } if (string.IsNullOrWhiteSpace(model.PayCurrency)) { throw new ApiException(ErrorCode.PayCurrencyEmpty); } var terminal = _db.Terminal .Where(t => t.SerialNumber == model.PosSn) .Include(t => t.ServiceProvider) .Include(t => t.Store).ThenInclude(t => t.Merchant) .FirstOrDefault(); if (terminal == null) { throw new ApiException(ErrorCode.TerminalNotFound); } if (terminal.Store == null) { throw new ApiException(ErrorCode.StoreNotFound); } if (terminal.Store.Merchant == null) { throw new ApiException(ErrorCode.MerchantNotFound); } if (terminal.ServiceProvider == null) { throw new ApiException(ErrorCode.ServiceProviderNotFound); } if (terminal.Status == TerminalStatus.Disabled) { throw new ApiException(ErrorCode.TerminalDisabled); } if (terminal.Status == TerminalStatus.DisabledByServiceProvider) { throw new ApiException(ErrorCode.TerminalDisabledByServiceProvider); } if (terminal.Store.Status == StoreStatus.Disabled) { throw new ApiException(ErrorCode.StoreDisabled); } if (terminal.Merchant.Status == MerchantStatus.Disabled) { throw new ApiException(ErrorCode.MerchantDisabled); } if (terminal.ServiceProvider.Status == ServiceProviderStatus.Disabled) { throw new ApiException(ErrorCode.ServiceProviderDisabled); } Payment payment = null; if (model.PayCurrency == "GRFT") { payment = await GraftSale(model, terminal); } else { payment = await AltcoinSale(model, terminal); } _cache.Set(payment.Id, payment, DateTimeOffset.Now.AddMinutes(_settings.PaymentTimeout)); _db.Payment.Add(payment); await _db.SaveChangesAsync(); GatewaySaleResult res = GetSaleResult(payment); _logger.LogInformation("API Sale Result: {@params}", res); return(res); }