Simplified version, with less validation

This commit is contained in:
HugoDoyon 2019-08-02 14:14:43 -04:00
parent 6b777878e3
commit 56e5acfb65
3 changed files with 10 additions and 255 deletions

View File

@ -126,118 +126,7 @@ namespace BTCPayServer.Tests
#pragma warning restore CS0618
}
[Fact]
public void CanPayWithLightningWhenInvoiceTotalUnderLightningMaxValueWhenOnChainMinValueIsGreater()
{
#pragma warning disable CS0618
//Given
var store = new StoreBlob
{
OnChainMinValue = new CurrencyValue() {Value = 100.00m, Currency = "USD"},
LightningMaxValue = new CurrencyValue() {Value = 50.00m, Currency = "USD"}
};
var paymentMethodId = new PaymentMethodId("BTC", PaymentTypes.LightningLike);
//When
var totalInvoiceAmount = new Money(80m, MoneyUnit.BTC);
//Then
var errorMessage = handlerLN.IsPaymentMethodAllowedBasedOnInvoiceAmount(store, currencyPairRateResult,
totalInvoiceAmount, paymentMethodId);
Assert.Equal(errorMessage.Result, string.Empty);
#pragma warning restore CS0618
}
[Fact]
public void CanPayWithLightningWhenInvoiceTotalUnderLightningMaxValueWhenOnChainMinValueIsLower()
{
#pragma warning disable CS0618
//Given
var store = new StoreBlob
{
OnChainMinValue = new CurrencyValue() {Value = 50.00m, Currency = "USD"},
LightningMaxValue = new CurrencyValue() {Value = 100.00m, Currency = "USD"}
};
var paymentMethodId = new PaymentMethodId("BTC", PaymentTypes.LightningLike);
//When
var totalInvoiceAmount = new Money(80m, MoneyUnit.BTC);
//Then
var errorMessage = handlerLN.IsPaymentMethodAllowedBasedOnInvoiceAmount(store, currencyPairRateResult,
totalInvoiceAmount, paymentMethodId);
Assert.Equal(errorMessage.Result, string.Empty);
#pragma warning restore CS0618
}
[Fact]
public void CannotPayWithLightningWhenInvoiceTotalUnderLightningMaxValueWhenOnChainMinValueIsGreater()
{
#pragma warning disable CS0618
//Given
var store = new StoreBlob
{
OnChainMinValue = new CurrencyValue() {Value = 100.00m, Currency = "USD"},
LightningMaxValue = new CurrencyValue() {Value = 50.00m, Currency = "USD"}
};
var paymentMethodId = new PaymentMethodId("BTC", PaymentTypes.LightningLike);
//When
var totalInvoiceAmount = new Money(105m, MoneyUnit.BTC);
//Then
var errorMessage = handlerLN.IsPaymentMethodAllowedBasedOnInvoiceAmount(store, currencyPairRateResult,
totalInvoiceAmount, paymentMethodId);
Assert.NotEqual(errorMessage.Result, string.Empty);
#pragma warning restore CS0618
}
[Fact]
public void CannotPayWithLightningWhenInvoiceTotalUnderLightningMaxValueWhenOnChainMinValueIsLower()
{
#pragma warning disable CS0618
//Given
var store = new StoreBlob
{
OnChainMinValue = new CurrencyValue() {Value = 50.00m, Currency = "USD"},
LightningMaxValue = new CurrencyValue() {Value = 100.00m, Currency = "USD"}
};
var paymentMethodId = new PaymentMethodId("BTC", PaymentTypes.LightningLike);
//When
var totalInvoiceAmount = new Money(105m, MoneyUnit.BTC);
//Then
var errorMessage = handlerLN.IsPaymentMethodAllowedBasedOnInvoiceAmount(store, currencyPairRateResult,
totalInvoiceAmount, paymentMethodId);
Assert.NotEqual(errorMessage.Result, string.Empty);
#pragma warning restore CS0618
}
//----
[Fact]
public void CanPayWithBitcoinWhenInvoiceTotalAboveOnChainMinValue()
{
@ -317,88 +206,7 @@ namespace BTCPayServer.Tests
#pragma warning restore CS0618
}
[Fact]
public void CanPayWithBitcoinWhenInvoiceTotalAboveOnchainMinValueWhenLightningMaxValueIsLower()
{
#pragma warning disable CS0618
//Given
var store = new StoreBlob
{
OnChainMinValue = new CurrencyValue() {Value = 100.00m, Currency = "USD"},
LightningMaxValue = new CurrencyValue() {Value = 50.00m, Currency = "USD"}
};
var paymentMethodId = new PaymentMethodId("BTC", PaymentTypes.BTCLike);
//When
var totalInvoiceAmount = new Money(80m, MoneyUnit.BTC);
//Then
var errorMessage = handlerBTC.IsPaymentMethodAllowedBasedOnInvoiceAmount(store, currencyPairRateResult,
totalInvoiceAmount, paymentMethodId);
Assert.Equal(errorMessage.Result, string.Empty);
#pragma warning restore CS0618
}
[Fact]
public void CanPayWithBitcoinWhenInvoiceTotalAboveOnChainMinValueWhenLightningMaxValueIsGreater()
{
#pragma warning disable CS0618
//Given
var store = new StoreBlob
{
OnChainMinValue = new CurrencyValue() {Value = 50.00m, Currency = "USD"},
LightningMaxValue = new CurrencyValue() {Value = 100.00m, Currency = "USD"}
};
var paymentMethodId = new PaymentMethodId("BTC", PaymentTypes.BTCLike);
//When
var totalInvoiceAmount = new Money(80m, MoneyUnit.BTC);
//Then
var errorMessage = handlerBTC.IsPaymentMethodAllowedBasedOnInvoiceAmount(store, currencyPairRateResult,
totalInvoiceAmount, paymentMethodId);
Assert.Equal(errorMessage.Result, string.Empty);
#pragma warning restore CS0618
}
[Fact]
public void CannotPayWithBitcoinWhenInvoiceTotalUnderOnChainMinValueWhenLightningMaxValueIsLower()
{
#pragma warning disable CS0618
//Given
var store = new StoreBlob
{
OnChainMinValue = new CurrencyValue() {Value = 100.00m, Currency = "USD"},
LightningMaxValue = new CurrencyValue() {Value = 50.00m, Currency = "USD"}
};
var paymentMethodId = new PaymentMethodId("BTC", PaymentTypes.BTCLike);
//When
var totalInvoiceAmount = new Money(45m, MoneyUnit.BTC);
//Then
var errorMessage = handlerBTC.IsPaymentMethodAllowedBasedOnInvoiceAmount(store, currencyPairRateResult,
totalInvoiceAmount, paymentMethodId);
Assert.NotEqual(errorMessage.Result, string.Empty);
#pragma warning restore CS0618
}
[Fact]
public void CannotPayWithBitcoinWhenInvoiceTotalUnderOnChainMinValueWhenLightningMaxValueIsGreater()
@ -426,5 +234,6 @@ namespace BTCPayServer.Tests
#pragma warning restore CS0618
}
}
}

View File

@ -74,38 +74,11 @@ namespace BTCPayServer.Payments.Bitcoin
CurrencyValue limitValue = null;
string errorMessage = null;
if (paymentMethodId.PaymentType == PaymentTypes.BTCLike)
if (storeBlob.OnChainMinValue != null)
{
if (storeBlob.LightningMaxValue == null &&
storeBlob.OnChainMinValue != null)
{
compare = (value, limit) => value < limit;
limitValue = storeBlob.OnChainMinValue;
errorMessage = "The amount of the invoice is too low to be paid on chain";
}
else if (storeBlob.LightningMaxValue != null &&
storeBlob.OnChainMinValue != null)
{
if (storeBlob.LightningMaxValue.Currency == storeBlob.OnChainMinValue.Currency &&
storeBlob.LightningMaxValue.Value <= storeBlob.OnChainMinValue.Value)
{
//Case where both fields are set but OnChainMinValue is greater
// --> then use LightningMaxValue as limit
compare = (value, limit) => value < limit;
limitValue = storeBlob.LightningMaxValue;
errorMessage = "The amount of the invoice is too low to be paid on chain";
}
else if (storeBlob.LightningMaxValue.Currency == storeBlob.OnChainMinValue.Currency &&
storeBlob.LightningMaxValue.Value > storeBlob.OnChainMinValue.Value)
{
//Case where both fields are set but LightningMaxValue is greater
// --> then use OnChainMinValue as limit
// (Otherwise a gap of price value with no payment method is possible)
compare = (value, limit) => value < limit;
limitValue = storeBlob.OnChainMinValue;
errorMessage = "The amount of the invoice is too low to be paid on chain";
}
}
compare = (value, limit) => value < limit;
limitValue = storeBlob.OnChainMinValue;
errorMessage = "The amount of the invoice is too low to be paid on chain";
}

View File

@ -151,38 +151,11 @@ namespace BTCPayServer.Payments.Lightning
CurrencyValue limitValue = null;
string errorMessage = null;
if (paymentMethodId.PaymentType == PaymentTypes.LightningLike)
if (storeBlob.LightningMaxValue != null)
{
if (storeBlob.LightningMaxValue != null &&
storeBlob.OnChainMinValue == null)
{
compare = (value, limit) => value > limit;
limitValue = storeBlob.LightningMaxValue;
errorMessage = "The amount of the invoice is too high to be paid with lightning";
}
else if (storeBlob.LightningMaxValue != null &&
storeBlob.OnChainMinValue != null)
{
if (storeBlob.LightningMaxValue.Currency == storeBlob.OnChainMinValue.Currency &&
storeBlob.LightningMaxValue.Value >= storeBlob.OnChainMinValue.Value)
{
//Case where both fields are set but LightningMaxValue is greater
// --> then use LightningMaxValue as limit
compare = (value, limit) => value > limit;
limitValue = storeBlob.LightningMaxValue;
errorMessage = "The amount of the invoice is too high to be paid with lightning";
}
else if (storeBlob.LightningMaxValue.Currency == storeBlob.OnChainMinValue.Currency &&
storeBlob.LightningMaxValue.Value < storeBlob.OnChainMinValue.Value)
{
//Case where both fields are set but OnChainMinValue is greater
// --> then use OnChainMinValue as limit
// (Otherwise a gap of price value with no payment method is possible)
compare = (value, limit) => value > limit;
limitValue = storeBlob.OnChainMinValue;
errorMessage = "The amount of the invoice is too high to be paid with lightning";
}
}
compare = (value, limit) => value > limit;
limitValue = storeBlob.LightningMaxValue;
errorMessage = "The amount of the invoice is too high to be paid with lightning";
}