示例#1
0
        public async Task <IActionResult> Invoice(string invoiceId)
        {
            var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery()
            {
                InvoiceId = new[] { invoiceId },
                UserId = GetUserId(),
                IncludeAddresses = true,
                IncludeEvents = true
            })).FirstOrDefault();

            if (invoice == null)
            {
                return(NotFound());
            }

            var prodInfo = invoice.ProductInformation;
            var store    = await _StoreRepository.FindStore(invoice.StoreId);

            var model = new InvoiceDetailsModel()
            {
                StoreName        = store.StoreName,
                StoreLink        = Url.Action(nameof(StoresController.UpdateStore), "Stores", new { storeId = store.Id }),
                Id               = invoice.Id,
                State            = invoice.GetInvoiceState().ToString(),
                TransactionSpeed = invoice.SpeedPolicy == SpeedPolicy.HighSpeed ? "high" :
                                   invoice.SpeedPolicy == SpeedPolicy.MediumSpeed ? "medium" :
                                   invoice.SpeedPolicy == SpeedPolicy.LowMediumSpeed ? "low-medium" :
                                   "low",
                RefundEmail        = invoice.RefundMail,
                CreatedDate        = invoice.InvoiceTime,
                ExpirationDate     = invoice.ExpirationTime,
                MonitoringDate     = invoice.MonitoringExpiration,
                OrderId            = invoice.OrderId,
                BuyerInformation   = invoice.BuyerInformation,
                Fiat               = _CurrencyNameTable.DisplayFormatCurrency(prodInfo.Price, prodInfo.Currency),
                TaxIncluded        = _CurrencyNameTable.DisplayFormatCurrency(prodInfo.TaxIncluded, prodInfo.Currency),
                NotificationEmail  = invoice.NotificationEmail,
                NotificationUrl    = invoice.NotificationURL?.AbsoluteUri,
                RedirectUrl        = invoice.RedirectURL?.AbsoluteUri,
                ProductInformation = invoice.ProductInformation,
                StatusException    = invoice.ExceptionStatus,
                Events             = invoice.Events,
                PosData            = PosDataParser.ParsePosData(invoice.PosData)
            };

            model.Addresses = invoice.HistoricalAddresses.Select(h =>
                                                                 new InvoiceDetailsModel.AddressModel
            {
                Destination   = h.GetAddress(),
                PaymentMethod = h.GetPaymentMethodId().ToPrettyString(),
                Current       = !h.UnAssigned.HasValue
            }).ToArray();

            var details = InvoicePopulatePayments(invoice);

            model.CryptoPayments = details.CryptoPayments;
            model.Payments       = details.Payments;

            return(View(model));
        }
        public async Task <IActionResult> Invoice(string invoiceId)
        {
            var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery()
            {
                InvoiceId = invoiceId,
                UserId = GetUserId(),
                IncludeAddresses = true,
                IncludeEvents = true
            })).FirstOrDefault();

            if (invoice == null)
            {
                return(NotFound());
            }

            var dto   = invoice.EntityToDTO(_NetworkProvider);
            var store = await _StoreRepository.FindStore(invoice.StoreId);

            InvoiceDetailsModel model = new InvoiceDetailsModel()
            {
                StoreName        = store.StoreName,
                StoreLink        = Url.Action(nameof(StoresController.UpdateStore), "Stores", new { storeId = store.Id }),
                Id               = invoice.Id,
                State            = invoice.GetInvoiceState().ToString(),
                TransactionSpeed = invoice.SpeedPolicy == SpeedPolicy.HighSpeed ? "high" :
                                   invoice.SpeedPolicy == SpeedPolicy.MediumSpeed ? "medium" :
                                   invoice.SpeedPolicy == SpeedPolicy.LowMediumSpeed ? "low-medium" :
                                   "low",
                RefundEmail        = invoice.RefundMail,
                CreatedDate        = invoice.InvoiceTime,
                ExpirationDate     = invoice.ExpirationTime,
                MonitoringDate     = invoice.MonitoringExpiration,
                OrderId            = invoice.OrderId,
                BuyerInformation   = invoice.BuyerInformation,
                Fiat               = _CurrencyNameTable.DisplayFormatCurrency(dto.Price, dto.Currency),
                TaxIncluded        = _CurrencyNameTable.DisplayFormatCurrency(invoice.ProductInformation.TaxIncluded, dto.Currency),
                NotificationEmail  = invoice.NotificationEmail,
                NotificationUrl    = invoice.NotificationURL,
                RedirectUrl        = invoice.RedirectURL,
                ProductInformation = invoice.ProductInformation,
                StatusException    = invoice.ExceptionStatus,
                Events             = invoice.Events,
                PosData            = PosDataParser.ParsePosData(dto.PosData)
            };

            foreach (var data in invoice.GetPaymentMethods(null))
            {
                var cryptoInfo      = dto.CryptoInfo.First(o => o.GetpaymentMethodId() == data.GetId());
                var accounting      = data.Calculate();
                var paymentMethodId = data.GetId();
                var cryptoPayment   = new InvoiceDetailsModel.CryptoPayment();
                cryptoPayment.PaymentMethod = ToString(paymentMethodId);
                cryptoPayment.Due           = _CurrencyNameTable.DisplayFormatCurrency(accounting.Due.ToDecimal(MoneyUnit.BTC), paymentMethodId.CryptoCode);
                cryptoPayment.Paid          = _CurrencyNameTable.DisplayFormatCurrency(accounting.CryptoPaid.ToDecimal(MoneyUnit.BTC), paymentMethodId.CryptoCode);
                cryptoPayment.Overpaid      = _CurrencyNameTable.DisplayFormatCurrency(accounting.OverpaidHelper.ToDecimal(MoneyUnit.BTC), paymentMethodId.CryptoCode);

                var onchainMethod = data.GetPaymentMethodDetails() as Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod;
                if (onchainMethod != null)
                {
                    cryptoPayment.Address = onchainMethod.DepositAddress;
                }
                cryptoPayment.Rate       = ExchangeRate(data);
                cryptoPayment.PaymentUrl = cryptoInfo.PaymentUrls.BIP21;
                model.CryptoPayments.Add(cryptoPayment);
            }

            var onChainPayments = invoice
                                  .GetPayments()
                                  .Select <PaymentEntity, Task <object> >(async payment =>
            {
                var paymentNetwork = _NetworkProvider.GetNetwork(payment.GetCryptoCode());
                var paymentData    = payment.GetCryptoPaymentData();
                if (paymentData is Payments.Bitcoin.BitcoinLikePaymentData onChainPaymentData)
                {
                    var m            = new InvoiceDetailsModel.Payment();
                    m.Crypto         = payment.GetPaymentMethodId().CryptoCode;
                    m.DepositAddress = onChainPaymentData.GetDestination(paymentNetwork);

                    int confirmationCount = 0;
                    if ((onChainPaymentData.ConfirmationCount < paymentNetwork.MaxTrackedConfirmation && payment.Accounted) &&
                        (onChainPaymentData.Legacy || invoice.MonitoringExpiration < DateTimeOffset.UtcNow))         // The confirmation count in the paymentData is not up to date
                    {
                        confirmationCount = (await((ExplorerClientProvider)_ServiceProvider.GetService(typeof(ExplorerClientProvider))).GetExplorerClient(payment.GetCryptoCode())?.GetTransactionAsync(onChainPaymentData.Outpoint.Hash))?.Confirmations ?? 0;
                        onChainPaymentData.ConfirmationCount = confirmationCount;
                        payment.SetCryptoPaymentData(onChainPaymentData);
                        await _InvoiceRepository.UpdatePayments(new List <PaymentEntity> {
                            payment
                        });
                    }
                    else
                    {
                        confirmationCount = onChainPaymentData.ConfirmationCount;
                    }
                    if (confirmationCount >= paymentNetwork.MaxTrackedConfirmation)
                    {
                        m.Confirmations = "At least " + (paymentNetwork.MaxTrackedConfirmation);
                    }
                    else
                    {
                        m.Confirmations = confirmationCount.ToString(CultureInfo.InvariantCulture);
                    }

                    m.TransactionId   = onChainPaymentData.Outpoint.Hash.ToString();
                    m.ReceivedTime    = payment.ReceivedTime;
                    m.TransactionLink = string.Format(CultureInfo.InvariantCulture, paymentNetwork.BlockExplorerLink, m.TransactionId);
                    m.Replaced        = !payment.Accounted;
                    return(m);
                }
                else
                {
                    var lightningPaymentData = (Payments.Lightning.LightningLikePaymentData)paymentData;
                    return(new InvoiceDetailsModel.OffChainPayment()
                    {
                        Crypto = paymentNetwork.CryptoCode,
                        BOLT11 = lightningPaymentData.BOLT11
                    });
                }
            })
                                  .ToArray();
            await Task.WhenAll(onChainPayments);

            model.Addresses = invoice.HistoricalAddresses.Select(h => new InvoiceDetailsModel.AddressModel
            {
                Destination   = h.GetAddress(),
                PaymentMethod = ToString(h.GetPaymentMethodId()),
                Current       = !h.UnAssigned.HasValue
            }).ToArray();
            model.OnChainPayments  = onChainPayments.Select(p => p.GetAwaiter().GetResult()).OfType <InvoiceDetailsModel.Payment>().ToList();
            model.OffChainPayments = onChainPayments.Select(p => p.GetAwaiter().GetResult()).OfType <InvoiceDetailsModel.OffChainPayment>().ToList();
            model.StatusMessage    = StatusMessage;
            return(View(model));
        }
        public async Task <IActionResult> Invoice(string invoiceId)
        {
            var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery()
            {
                InvoiceId = new[] { invoiceId },
                UserId = GetUserId(),
                IncludeAddresses = true,
                IncludeEvents = true,
                IncludeArchived = true,
            })).FirstOrDefault();

            if (invoice == null)
            {
                return(NotFound());
            }

            var store = await _StoreRepository.FindStore(invoice.StoreId);

            var invoiceState = invoice.GetInvoiceState();
            var model        = new InvoiceDetailsModel()
            {
                StoreId            = store.Id,
                StoreName          = store.StoreName,
                StoreLink          = Url.Action(nameof(StoresController.UpdateStore), "Stores", new { storeId = store.Id }),
                PaymentRequestLink = Url.Action(nameof(PaymentRequestController.ViewPaymentRequest), "PaymentRequest", new { id = invoice.Metadata.PaymentRequestId }),
                Id               = invoice.Id,
                State            = invoiceState.ToString(),
                TransactionSpeed = invoice.SpeedPolicy == SpeedPolicy.HighSpeed ? "high" :
                                   invoice.SpeedPolicy == SpeedPolicy.MediumSpeed ? "medium" :
                                   invoice.SpeedPolicy == SpeedPolicy.LowMediumSpeed ? "low-medium" :
                                   "low",
                RefundEmail     = invoice.RefundMail,
                CreatedDate     = invoice.InvoiceTime,
                ExpirationDate  = invoice.ExpirationTime,
                MonitoringDate  = invoice.MonitoringExpiration,
                Fiat            = _CurrencyNameTable.DisplayFormatCurrency(invoice.Price, invoice.Currency),
                TaxIncluded     = _CurrencyNameTable.DisplayFormatCurrency(invoice.Metadata.TaxIncluded ?? 0.0m, invoice.Currency),
                NotificationUrl = invoice.NotificationURL?.AbsoluteUri,
                RedirectUrl     = invoice.RedirectURL?.AbsoluteUri,
                TypedMetadata   = invoice.Metadata,
                StatusException = invoice.ExceptionStatus,
                Events          = invoice.Events,
                PosData         = PosDataParser.ParsePosData(invoice.Metadata.PosData),
                Archived        = invoice.Archived,
                CanRefund       = CanRefund(invoiceState),
                ShowCheckout    = invoice.Status == InvoiceStatusLegacy.New,
                Deliveries      = (await _InvoiceRepository.GetWebhookDeliveries(invoiceId))
                                  .Select(c => new Models.StoreViewModels.DeliveryViewModel(c))
                                  .ToList(),
                CanMarkInvalid  = invoiceState.CanMarkInvalid(),
                CanMarkComplete = invoiceState.CanMarkComplete(),
            };

            model.Addresses = invoice.HistoricalAddresses.Select(h =>
                                                                 new InvoiceDetailsModel.AddressModel
            {
                Destination   = h.GetAddress(),
                PaymentMethod = h.GetPaymentMethodId().ToPrettyString(),
                Current       = !h.UnAssigned.HasValue
            }).ToArray();

            var details = InvoicePopulatePayments(invoice);

            model.CryptoPayments = details.CryptoPayments;
            model.Payments       = details.Payments;

            return(View(model));
        }
示例#4
0
        public async Task <IActionResult> Invoice(string invoiceId)
        {
            var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery()
            {
                InvoiceId = invoiceId,
                UserId = GetUserId(),
                IncludeAddresses = true,
                IncludeEvents = true
            })).FirstOrDefault();

            if (invoice == null)
            {
                return(NotFound());
            }

            var prodInfo = invoice.ProductInformation;
            var store    = await _StoreRepository.FindStore(invoice.StoreId);

            var model = new InvoiceDetailsModel()
            {
                StoreName        = store.StoreName,
                StoreLink        = Url.Action(nameof(StoresController.UpdateStore), "Stores", new { storeId = store.Id }),
                Id               = invoice.Id,
                State            = invoice.GetInvoiceState().ToString(),
                TransactionSpeed = invoice.SpeedPolicy == SpeedPolicy.HighSpeed ? "high" :
                                   invoice.SpeedPolicy == SpeedPolicy.MediumSpeed ? "medium" :
                                   invoice.SpeedPolicy == SpeedPolicy.LowMediumSpeed ? "low-medium" :
                                   "low",
                RefundEmail        = invoice.RefundMail,
                CreatedDate        = invoice.InvoiceTime,
                ExpirationDate     = invoice.ExpirationTime,
                MonitoringDate     = invoice.MonitoringExpiration,
                OrderId            = invoice.OrderId,
                BuyerInformation   = invoice.BuyerInformation,
                Fiat               = _CurrencyNameTable.DisplayFormatCurrency(prodInfo.Price, prodInfo.Currency),
                TaxIncluded        = _CurrencyNameTable.DisplayFormatCurrency(prodInfo.TaxIncluded, prodInfo.Currency),
                NotificationEmail  = invoice.NotificationEmail,
                NotificationUrl    = invoice.NotificationURL,
                RedirectUrl        = invoice.RedirectURL,
                ProductInformation = invoice.ProductInformation,
                StatusException    = invoice.ExceptionStatus,
                Events             = invoice.Events,
                PosData            = PosDataParser.ParsePosData(invoice.PosData),
                StatusMessage      = StatusMessage
            };

            model.Addresses = invoice.HistoricalAddresses.Select(h => new InvoiceDetailsModel.AddressModel
            {
                Destination   = h.GetAddress(),
                PaymentMethod = ToString(h.GetPaymentMethodId()),
                Current       = !h.UnAssigned.HasValue
            }).ToArray();

            var updateConfirmationCountIfNeeded = invoice
                                                  .GetPayments()
                                                  .Select <PaymentEntity, Task>(async payment =>
            {
                var paymentNetwork = _NetworkProvider.GetNetwork(payment.GetCryptoCode());
                var paymentData    = payment.GetCryptoPaymentData();
                if (paymentData is Payments.Bitcoin.BitcoinLikePaymentData onChainPaymentData)
                {
                    int confirmationCount = 0;
                    if ((onChainPaymentData.ConfirmationCount < paymentNetwork.MaxTrackedConfirmation && payment.Accounted) &&
                        (onChainPaymentData.Legacy || invoice.MonitoringExpiration < DateTimeOffset.UtcNow))
                    // The confirmation count in the paymentData is not up to date
                    {
                        confirmationCount = (await((ExplorerClientProvider)_ServiceProvider.GetService(typeof(ExplorerClientProvider))).GetExplorerClient(payment.GetCryptoCode())?.GetTransactionAsync(onChainPaymentData.Outpoint.Hash))?.Confirmations ?? 0;
                        onChainPaymentData.ConfirmationCount = confirmationCount;
                        payment.SetCryptoPaymentData(onChainPaymentData);
                        await _InvoiceRepository.UpdatePayments(new List <PaymentEntity> {
                            payment
                        });
                    }
                }
            })
                                                  .ToArray();
            await Task.WhenAll(updateConfirmationCountIfNeeded);

            var details = InvoicePopulatePayments(invoice);

            model.CryptoPayments   = details.CryptoPayments;
            model.OnChainPayments  = details.OnChainPayments;
            model.OffChainPayments = details.OffChainPayments;

            return(View(model));
        }