示例#1
0
        private InvoiceEntity ToEntity(Data.InvoiceData invoice)
        {
            var entity = invoice.GetBlob(_Networks);
            PaymentMethodDictionary paymentMethods = null;

#pragma warning disable CS0618
            entity.Payments = invoice.Payments.Select(p =>
            {
                var paymentEntity = p.GetBlob(_Networks);
                if (paymentEntity is null)
                {
                    return(null);
                }
                // PaymentEntity on version 0 does not have their own fee, because it was assumed that the payment method have fixed fee.
                // We want to hide this legacy detail in InvoiceRepository, so we fetch the fee from the PaymentMethod and assign it to the PaymentEntity.
                if (paymentEntity.Version == 0)
                {
                    if (paymentMethods == null)
                    {
                        paymentMethods = entity.GetPaymentMethods();
                    }
                    var paymentMethodDetails = paymentMethods.TryGet(paymentEntity.GetPaymentMethodId())?.GetPaymentMethodDetails();
                    if (paymentMethodDetails != null) // == null should never happen, but we never know.
                    {
                        paymentEntity.NetworkFee = paymentMethodDetails.GetNextNetworkFee();
                    }
                }

                return(paymentEntity);
            })
                              .Where(p => p != null)
                              .OrderBy(a => a.ReceivedTime).ToList();
#pragma warning restore CS0618
            var state = invoice.GetInvoiceState();
            entity.ExceptionStatus = state.ExceptionStatus;
            entity.Status          = state.Status;
            entity.RefundMail      = invoice.CustomerEmail;
            entity.Refundable      = false;
            if (invoice.HistoricalAddressInvoices != null)
            {
                entity.HistoricalAddresses = invoice.HistoricalAddressInvoices.ToArray();
            }
            if (invoice.AddressInvoices != null)
            {
                entity.AvailableAddressHashes = invoice.AddressInvoices.Select(a => a.GetAddress() + a.GetpaymentMethodId().ToString()).ToHashSet();
            }
            if (invoice.Events != null)
            {
                entity.Events = invoice.Events.OrderBy(c => c.Timestamp).ToList();
            }
            if (!string.IsNullOrEmpty(entity.RefundMail) && string.IsNullOrEmpty(entity.Metadata.BuyerEmail))
            {
                entity.Metadata.BuyerEmail = entity.RefundMail;
            }
            entity.Archived = invoice.Archived;
            return(entity);
        }
        private InvoiceEntity ToEntity(Data.InvoiceData invoice)
        {
            var entity = ToObject(invoice.Blob);
            PaymentMethodDictionary paymentMethods = null;

#pragma warning disable CS0618
            entity.Payments = invoice.Payments.Select(p =>
            {
                var paymentEntity       = ToObject <PaymentEntity>(p.Blob, null);
                paymentEntity.Network   = _Networks.GetNetwork <BTCPayNetwork>(paymentEntity.CryptoCode);
                paymentEntity.Accounted = p.Accounted;
                // PaymentEntity on version 0 does not have their own fee, because it was assumed that the payment method have fixed fee.
                // We want to hide this legacy detail in InvoiceRepository, so we fetch the fee from the PaymentMethod and assign it to the PaymentEntity.
                if (paymentEntity.Version == 0)
                {
                    if (paymentMethods == null)
                    {
                        paymentMethods = entity.GetPaymentMethods();
                    }
                    var paymentMethodDetails = paymentMethods.TryGet(paymentEntity.GetPaymentMethodId())?.GetPaymentMethodDetails();
                    if (paymentMethodDetails != null) // == null should never happen, but we never know.
                    {
                        paymentEntity.NetworkFee = paymentMethodDetails.GetNextNetworkFee();
                    }
                }

                return(paymentEntity);
            })
                              .OrderBy(a => a.ReceivedTime).ToList();
#pragma warning restore CS0618
            var state = invoice.GetInvoiceState();
            entity.ExceptionStatus = state.ExceptionStatus;
            entity.Status          = state.Status;
            entity.RefundMail      = invoice.CustomerEmail;
            entity.Refundable      = invoice.RefundAddresses.Count != 0;
            if (invoice.HistoricalAddressInvoices != null)
            {
                entity.HistoricalAddresses = invoice.HistoricalAddressInvoices.ToArray();
            }
            if (invoice.AddressInvoices != null)
            {
                entity.AvailableAddressHashes = invoice.AddressInvoices.Select(a => a.GetAddress() + a.GetpaymentMethodId().ToString()).ToHashSet();
            }
            if (invoice.Events != null)
            {
                entity.Events = invoice.Events.OrderBy(c => c.Timestamp).ToList();
            }
            return(entity);
        }
        internal decimal GetValue(PaymentMethodDictionary paymentMethods, PaymentMethodId paymentMethodId, decimal?value = null)
        {
            value = value ?? this.GetCryptoPaymentData().GetValue();
            var to   = paymentMethodId;
            var from = this.GetPaymentMethodId();

            if (to == from)
            {
                return(decimal.Round(value.Value, 8));
            }
            var fromRate = paymentMethods[from].Rate;
            var toRate   = paymentMethods[to].Rate;

            var fiatValue          = fromRate * decimal.Round(value.Value, 8);
            var otherCurrencyValue = toRate == 0 ? 0.0m : fiatValue / toRate;

            return(otherCurrencyValue);
        }
示例#4
0
        public PaymentMethodDictionary GetPaymentMethods(BTCPayNetworkProvider networkProvider, bool alwaysIncludeBTC = false)
        {
            PaymentMethodDictionary rates = new PaymentMethodDictionary(networkProvider);
            var           serializer      = new Serializer(Dummy);
            PaymentMethod phantom         = null;

#pragma warning disable CS0618
            // Legacy
            if (alwaysIncludeBTC)
            {
                var btcNetwork = networkProvider?.GetNetwork("BTC");
                phantom = new PaymentMethod()
                {
                    ParentEntity = this, IsPhantomBTC = true, Rate = Rate, CryptoCode = "BTC", TxFee = TxFee, FeeRate = new FeeRate(TxFee, 100), DepositAddress = DepositAddress, Network = btcNetwork
                };
                if (btcNetwork != null || networkProvider == null)
                {
                    rates.Add(phantom);
                }
            }
            if (PaymentMethod != null)
            {
                foreach (var prop in PaymentMethod.Properties())
                {
                    if (prop.Name == "BTC" && phantom != null)
                    {
                        rates.Remove(phantom);
                    }
                    var r = serializer.ToObject <PaymentMethod>(prop.Value.ToString());
                    var paymentMethodId = PaymentMethodId.Parse(prop.Name);
                    r.CryptoCode   = paymentMethodId.CryptoCode;
                    r.PaymentType  = paymentMethodId.PaymentType.ToString();
                    r.ParentEntity = this;
                    r.Network      = networkProvider?.GetNetwork(r.CryptoCode);
                    if (r.Network != null || networkProvider == null)
                    {
                        rates.Add(r);
                    }
                }
            }
#pragma warning restore CS0618
            return(rates);
        }
示例#5
0
        public void SetPaymentMethods(PaymentMethodDictionary paymentMethods)
        {
            var obj        = new JObject();
            var serializer = new Serializer(null);

#pragma warning disable CS0618
            foreach (var v in paymentMethods)
            {
                var clone = serializer.ToObject <PaymentMethod>(serializer.ToString(v));
                clone.CryptoCode  = null;
                clone.PaymentType = null;
                obj.Add(new JProperty(v.GetId().ToString(), JObject.Parse(serializer.ToString(clone))));
            }
            PaymentMethod = obj;
            foreach (var cryptoData in paymentMethods)
            {
                cryptoData.ParentEntity = this;
            }
#pragma warning restore CS0618
        }
示例#6
0
        public PaymentMethodDictionary GetPaymentMethods()
        {
            PaymentMethodDictionary paymentMethods = new PaymentMethodDictionary();
            var serializer = new Serializer(null);

#pragma warning disable CS0618
            if (PaymentMethod != null)
            {
                foreach (var prop in PaymentMethod.Properties())
                {
                    var r = serializer.ToObject <PaymentMethod>(prop.Value.ToString());
                    var paymentMethodId = PaymentMethodId.Parse(prop.Name);
                    r.CryptoCode   = paymentMethodId.CryptoCode;
                    r.PaymentType  = paymentMethodId.PaymentType.ToString();
                    r.ParentEntity = this;
                    r.Network      = Networks?.UnfilteredNetworks.GetNetwork <BTCPayNetworkBase>(r.CryptoCode);
                    paymentMethods.Add(r);
                }
            }
#pragma warning restore CS0618
            return(paymentMethods);
        }
示例#7
0
        public void SetPaymentMethods(PaymentMethodDictionary paymentMethods)
        {
            if (paymentMethods.NetworkProvider != null)
            {
                throw new InvalidOperationException($"{nameof(paymentMethods)} should have NetworkProvider to null");
            }
            var obj        = new JObject();
            var serializer = new Serializer(Dummy);

#pragma warning disable CS0618
            foreach (var v in paymentMethods)
            {
                var clone = serializer.ToObject <PaymentMethod>(serializer.ToString(v));
                clone.CryptoCode  = null;
                clone.PaymentType = null;
                obj.Add(new JProperty(v.GetId().ToString(), JObject.Parse(serializer.ToString(clone))));
            }
            PaymentMethod = obj;
            foreach (var cryptoData in paymentMethods)
            {
                cryptoData.ParentEntity = this;
            }
#pragma warning restore CS0618
        }