public async Task <ViewPaymentRequestViewModel> GetPaymentRequest(string id, string userId = null) { var pr = await _PaymentRequestRepository.FindPaymentRequest(id, null); if (pr == null) { return(null); } var blob = pr.GetBlob(); var rateRules = pr.StoreData.GetStoreBlob().GetRateRules(_BtcPayNetworkProvider); var invoices = await _PaymentRequestRepository.GetInvoicesForPaymentRequest(id); var paymentStats = _AppService.GetContributionsByPaymentMethodId(blob.Currency, invoices, true); var amountDue = blob.Amount - paymentStats.TotalCurrency; var pendingInvoice = invoices.SingleOrDefault(entity => entity.Status == InvoiceStatus.New); return(new ViewPaymentRequestViewModel(pr) { Archived = pr.Archived, AmountFormatted = _currencies.FormatCurrency(blob.Amount, blob.Currency), AmountCollected = paymentStats.TotalCurrency, AmountCollectedFormatted = _currencies.FormatCurrency(paymentStats.TotalCurrency, blob.Currency), AmountDue = amountDue, AmountDueFormatted = _currencies.FormatCurrency(amountDue, blob.Currency), CurrencyData = _currencies.GetCurrencyData(blob.Currency, true), LastUpdated = DateTime.Now, AnyPendingInvoice = pendingInvoice != null, PendingInvoiceHasPayments = pendingInvoice != null && pendingInvoice.ExceptionStatus != InvoiceExceptionStatus.None, Invoices = invoices.Select(entity => new ViewPaymentRequestViewModel.PaymentRequestInvoice() { Id = entity.Id, Amount = entity.ProductInformation.Price, AmountFormatted = _currencies.FormatCurrency(entity.ProductInformation.Price, blob.Currency), Currency = entity.ProductInformation.Currency, ExpiryDate = entity.ExpirationTime.DateTime, Status = entity.GetInvoiceState().ToString(), Payments = entity.GetPayments().Select(paymentEntity => { var paymentData = paymentEntity.GetCryptoPaymentData(); var paymentMethodId = paymentEntity.GetPaymentMethodId(); string txId = paymentData.GetPaymentId(); string link = GetTransactionLink(paymentMethodId, txId); return new ViewPaymentRequestViewModel.PaymentRequestInvoicePayment() { Amount = paymentData.GetValue(), PaymentMethod = paymentMethodId.ToString(), Link = link, Id = txId }; }).ToList() }).ToList() }); }
public async Task <IActionResult> ViewPullPayment(string pullPaymentId) { using var ctx = _dbContextFactory.CreateContext(); var pp = await ctx.PullPayments.FindAsync(pullPaymentId); if (pp is null) { return(NotFound()); } var blob = pp.GetBlob(); var payouts = (await ctx.Payouts.GetPayoutInPeriod(pp) .OrderByDescending(o => o.Date) .ToListAsync()) .Select(o => new { Entity = o, Blob = o.GetBlob(_serializerSettings), ProofBlob = _payoutHandlers.FindPayoutHandler(o.GetPaymentMethodId())?.ParseProof(o) }); var cd = _currencyNameTable.GetCurrencyData(blob.Currency, false); var totalPaid = payouts.Where(p => p.Entity.State != PayoutState.Cancelled).Select(p => p.Blob.Amount).Sum(); var amountDue = blob.Limit - totalPaid; ViewPullPaymentModel vm = new (pp, DateTimeOffset.UtcNow) { AmountFormatted = _currencyNameTable.FormatCurrency(blob.Limit, blob.Currency), AmountCollected = totalPaid, AmountCollectedFormatted = _currencyNameTable.FormatCurrency(totalPaid, blob.Currency), AmountDue = amountDue, ClaimedAmount = amountDue, AmountDueFormatted = _currencyNameTable.FormatCurrency(amountDue, blob.Currency), CurrencyData = cd, StartDate = pp.StartDate, LastRefreshed = DateTime.UtcNow, Payouts = payouts .Select(entity => new ViewPullPaymentModel.PayoutLine { Id = entity.Entity.Id, Amount = entity.Blob.Amount, AmountFormatted = _currencyNameTable.FormatCurrency(entity.Blob.Amount, blob.Currency), Currency = blob.Currency, Status = entity.Entity.State, Destination = entity.Blob.Destination, PaymentMethod = PaymentMethodId.Parse(entity.Entity.PaymentMethodId), Link = entity.ProofBlob?.Link, TransactionId = entity.ProofBlob?.Id }).ToList() }; vm.IsPending &= vm.AmountDue > 0.0m; return(View(nameof(ViewPullPayment), vm)); }
public async Task <IActionResult> ViewPullPayment(string pullPaymentId) { using var ctx = _dbContextFactory.CreateContext(); var pp = await ctx.PullPayments.FindAsync(pullPaymentId); if (pp is null) { return(NotFound()); } var blob = pp.GetBlob(); var payouts = (await ctx.Payouts.GetPayoutInPeriod(pp) .OrderByDescending(o => o.Date) .ToListAsync()) .Select(o => new { Entity = o, Blob = o.GetBlob(_serializerSettings), TransactionId = o.GetProofBlob(_serializerSettings)?.TransactionId?.ToString() }); var cd = _currencyNameTable.GetCurrencyData(blob.Currency, false); var totalPaid = payouts.Where(p => p.Entity.State != PayoutState.Cancelled).Select(p => p.Blob.Amount).Sum(); var amountDue = blob.Limit - totalPaid; ViewPullPaymentModel vm = new ViewPullPaymentModel(pp, DateTimeOffset.UtcNow) { AmountFormatted = _currencyNameTable.FormatCurrency(blob.Limit, blob.Currency), AmountCollected = totalPaid, AmountCollectedFormatted = _currencyNameTable.FormatCurrency(totalPaid, blob.Currency), AmountDue = amountDue, ClaimedAmount = amountDue, AmountDueFormatted = _currencyNameTable.FormatCurrency(amountDue, blob.Currency), CurrencyData = cd, LastUpdated = DateTime.Now, Payouts = payouts .Select(entity => new ViewPullPaymentModel.PayoutLine() { Id = entity.Entity.Id, Amount = entity.Blob.Amount, AmountFormatted = _currencyNameTable.FormatCurrency(entity.Blob.Amount, blob.Currency), Currency = blob.Currency, Status = entity.Entity.State.ToString(), Destination = entity.Blob.Destination.Address.ToString(), Link = GetTransactionLink(_networkProvider.GetNetwork <BTCPayNetwork>(entity.Entity.GetPaymentMethodId().CryptoCode), entity.TransactionId), TransactionId = entity.TransactionId }).ToList() }; vm.IsPending &= vm.AmountDue > 0.0m; return(View(nameof(ViewPullPayment), vm)); }
public async Task <ViewPaymentRequestViewModel> GetPaymentRequest(string id, string userId = null) { var pr = await _PaymentRequestRepository.FindPaymentRequest(id, null); if (pr == null) { return(null); } var blob = pr.GetBlob(); var invoices = await _PaymentRequestRepository.GetInvoicesForPaymentRequest(id); var paymentStats = _AppService.GetContributionsByPaymentMethodId(blob.Currency, invoices, true); var amountDue = blob.Amount - paymentStats.TotalCurrency; var pendingInvoice = invoices.OrderByDescending(entity => entity.InvoiceTime) .FirstOrDefault(entity => entity.Status == InvoiceStatusLegacy.New); return(new ViewPaymentRequestViewModel(pr) { Archived = pr.Archived, AmountFormatted = _currencies.FormatCurrency(blob.Amount, blob.Currency), AmountCollected = paymentStats.TotalCurrency, AmountCollectedFormatted = _currencies.FormatCurrency(paymentStats.TotalCurrency, blob.Currency), AmountDue = amountDue, AmountDueFormatted = _currencies.FormatCurrency(amountDue, blob.Currency), CurrencyData = _currencies.GetCurrencyData(blob.Currency, true), LastUpdated = DateTime.Now, AnyPendingInvoice = pendingInvoice != null, PendingInvoiceHasPayments = pendingInvoice != null && pendingInvoice.ExceptionStatus != InvoiceExceptionStatus.None, Invoices = invoices.Select(entity => { var state = entity.GetInvoiceState(); return new ViewPaymentRequestViewModel.PaymentRequestInvoice { Id = entity.Id, Amount = entity.Price, AmountFormatted = _currencies.FormatCurrency(entity.Price, blob.Currency), Currency = entity.Currency, ExpiryDate = entity.ExpirationTime.DateTime, State = state, StateFormatted = state.ToString(), Payments = entity .GetPayments(true) .Select(paymentEntity => { var paymentData = paymentEntity.GetCryptoPaymentData(); var paymentMethodId = paymentEntity.GetPaymentMethodId(); if (paymentData is null || paymentMethodId is null) { return null; } string txId = paymentData.GetPaymentId(); string link = GetTransactionLink(paymentMethodId, txId); var paymentMethod = entity.GetPaymentMethod(paymentMethodId); var amount = paymentData.GetValue(); var rate = paymentMethod.Rate; var paid = (amount - paymentEntity.NetworkFee) * rate; return new ViewPaymentRequestViewModel.PaymentRequestInvoicePayment { Amount = amount, Paid = paid, ReceivedDate = paymentEntity.ReceivedTime.DateTime, PaidFormatted = _currencies.FormatCurrency(paid, blob.Currency), RateFormatted = _currencies.FormatCurrency(rate, blob.Currency), PaymentMethod = paymentMethodId.ToPrettyString(), Link = link, Id = txId }; }) .Where(payment => payment != null) .ToList() }; }).ToList() });
public async Task <ViewPaymentRequestViewModel> GetPaymentRequest(string id, string userId = null) { var pr = await _PaymentRequestRepository.FindPaymentRequest(id, null); if (pr == null) { return(null); } var blob = pr.GetBlob(); var rateRules = pr.StoreData.GetStoreBlob().GetRateRules(_BtcPayNetworkProvider); var invoices = await _PaymentRequestRepository.GetInvoicesForPaymentRequest(id); var paymentStats = _AppService.GetCurrentContributionAmountStats(invoices, true); var amountCollected = await _AppService.GetCurrentContributionAmount(paymentStats, blob.Currency, rateRules); var amountDue = blob.Amount - amountCollected; return(new ViewPaymentRequestViewModel(pr) { AmountFormatted = _currencies.FormatCurrency(blob.Amount, blob.Currency), AmountCollected = amountCollected, AmountCollectedFormatted = _currencies.FormatCurrency(amountCollected, blob.Currency), AmountDue = amountDue, AmountDueFormatted = _currencies.FormatCurrency(amountDue, blob.Currency), CurrencyData = _currencies.GetCurrencyData(blob.Currency, true), LastUpdated = DateTime.Now, AnyPendingInvoice = invoices.Any(entity => entity.Status == InvoiceStatus.New), Invoices = invoices.Select(entity => new ViewPaymentRequestViewModel.PaymentRequestInvoice() { Id = entity.Id, Amount = entity.ProductInformation.Price, Currency = entity.ProductInformation.Currency, ExpiryDate = entity.ExpirationTime.DateTime, Status = entity.GetInvoiceState().ToString(), Payments = entity.GetPayments().Select(paymentEntity => { var paymentNetwork = _BtcPayNetworkProvider.GetNetwork(paymentEntity.GetCryptoCode()); var paymentData = paymentEntity.GetCryptoPaymentData(); string link = null; string txId = null; switch (paymentData) { case Payments.Bitcoin.BitcoinLikePaymentData onChainPaymentData: txId = onChainPaymentData.Outpoint.Hash.ToString(); link = string.Format(CultureInfo.InvariantCulture, paymentNetwork.BlockExplorerLink, txId); break; case LightningLikePaymentData lightningLikePaymentData: txId = lightningLikePaymentData.BOLT11; break; } return new ViewPaymentRequestViewModel.PaymentRequestInvoicePayment() { Amount = paymentData.GetValue(), PaymentMethod = paymentEntity.GetPaymentMethodId().ToString(), Link = link, Id = txId }; }).ToList() }).ToList()