mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 14:04:26 +01:00
Use nicer representation of payment methods in the Greenfield API
This commit is contained in:
@@ -877,7 +877,7 @@ normal:
|
|||||||
InvoiceEntity invoiceEntity = new InvoiceEntity();
|
InvoiceEntity invoiceEntity = new InvoiceEntity();
|
||||||
invoiceEntity.Networks = networkProvider;
|
invoiceEntity.Networks = networkProvider;
|
||||||
invoiceEntity.Payments = new System.Collections.Generic.List<PaymentEntity>();
|
invoiceEntity.Payments = new System.Collections.Generic.List<PaymentEntity>();
|
||||||
invoiceEntity.ProductInformation = new ProductInformation() { Price = 100 };
|
invoiceEntity.Price = 100;
|
||||||
PaymentMethodDictionary paymentMethods = new PaymentMethodDictionary();
|
PaymentMethodDictionary paymentMethods = new PaymentMethodDictionary();
|
||||||
paymentMethods.Add(new PaymentMethod() { Network = networkBTC, CryptoCode = "BTC", Rate = 10513.44m, }
|
paymentMethods.Add(new PaymentMethod() { Network = networkBTC, CryptoCode = "BTC", Rate = 10513.44m, }
|
||||||
.SetPaymentMethodDetails(
|
.SetPaymentMethodDetails(
|
||||||
|
|||||||
@@ -151,6 +151,36 @@ namespace BTCPayServer.Tests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
[Trait("Fast", "Fast")]
|
||||||
|
public void CanParsePaymentMethodId()
|
||||||
|
{
|
||||||
|
var id = PaymentMethodId.Parse("BTC");
|
||||||
|
var id1 = PaymentMethodId.Parse("BTC-OnChain");
|
||||||
|
var id2 = PaymentMethodId.Parse("BTC-BTCLike");
|
||||||
|
Assert.Equal(id, id1);
|
||||||
|
Assert.Equal(id, id2);
|
||||||
|
Assert.Equal("BTC", id.ToString());
|
||||||
|
Assert.Equal("BTC", id.ToString());
|
||||||
|
id = PaymentMethodId.Parse("LTC");
|
||||||
|
Assert.Equal("LTC", id.ToString());
|
||||||
|
Assert.Equal("LTC", id.ToStringNormalized());
|
||||||
|
id = PaymentMethodId.Parse("LTC-offchain");
|
||||||
|
id1 = PaymentMethodId.Parse("LTC-OffChain");
|
||||||
|
id2 = PaymentMethodId.Parse("LTC-LightningLike");
|
||||||
|
Assert.Equal(id, id1);
|
||||||
|
Assert.Equal(id, id2);
|
||||||
|
Assert.Equal("LTC_LightningLike", id.ToString());
|
||||||
|
Assert.Equal("LTC-LightningNetwork", id.ToStringNormalized());
|
||||||
|
#if ALTCOINS
|
||||||
|
id = PaymentMethodId.Parse("XMR");
|
||||||
|
id1 = PaymentMethodId.Parse("XMR-MoneroLike");
|
||||||
|
Assert.Equal(id, id1);
|
||||||
|
Assert.Equal("XMR_MoneroLike", id.ToString());
|
||||||
|
Assert.Equal("XMR", id.ToStringNormalized());
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
[Trait("Fast", "Fast")]
|
[Trait("Fast", "Fast")]
|
||||||
public async Task CheckNoDeadLink()
|
public async Task CheckNoDeadLink()
|
||||||
|
|||||||
@@ -267,7 +267,7 @@ namespace BTCPayServer.Controllers.GreenField
|
|||||||
Monitoring = entity.MonitoringExpiration - entity.ExpirationTime,
|
Monitoring = entity.MonitoringExpiration - entity.ExpirationTime,
|
||||||
PaymentTolerance = entity.PaymentTolerance,
|
PaymentTolerance = entity.PaymentTolerance,
|
||||||
PaymentMethods =
|
PaymentMethods =
|
||||||
entity.GetPaymentMethods().Select(method => method.GetId().ToString()).ToArray(),
|
entity.GetPaymentMethods().Select(method => method.GetId().ToStringNormalized()).ToArray(),
|
||||||
SpeedPolicy = entity.SpeedPolicy
|
SpeedPolicy = entity.SpeedPolicy
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -64,20 +64,38 @@ namespace BTCPayServer.Payments
|
|||||||
//BTCLike case is special because it is in legacy mode.
|
//BTCLike case is special because it is in legacy mode.
|
||||||
return PaymentType == PaymentTypes.BTCLike ? CryptoCode : $"{CryptoCode}_{PaymentType}";
|
return PaymentType == PaymentTypes.BTCLike ? CryptoCode : $"{CryptoCode}_{PaymentType}";
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// A string we can expose to Greenfield API, not subjected to internal legacy
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public string ToStringNormalized()
|
||||||
|
{
|
||||||
|
if (PaymentType == PaymentTypes.BTCLike)
|
||||||
|
return CryptoCode;
|
||||||
|
#if ALTCOINS
|
||||||
|
if (CryptoCode == "XMR" && PaymentType == PaymentTypes.MoneroLike)
|
||||||
|
return CryptoCode;
|
||||||
|
#endif
|
||||||
|
return $"{CryptoCode}-{PaymentType.ToStringNormalized()}";
|
||||||
|
}
|
||||||
|
|
||||||
public string ToPrettyString()
|
public string ToPrettyString()
|
||||||
{
|
{
|
||||||
return $"{CryptoCode} ({PaymentType.ToPrettyString()})";
|
return $"{CryptoCode} ({PaymentType.ToPrettyString()})";
|
||||||
}
|
}
|
||||||
|
static char[] Separators = new[] { '_', '-' };
|
||||||
public static bool TryParse(string str, out PaymentMethodId paymentMethodId)
|
public static bool TryParse(string str, out PaymentMethodId paymentMethodId)
|
||||||
{
|
{
|
||||||
str ??= "";
|
str ??= "";
|
||||||
paymentMethodId = null;
|
paymentMethodId = null;
|
||||||
var parts = str.Split('_', StringSplitOptions.RemoveEmptyEntries);
|
var parts = str.Split(Separators, StringSplitOptions.RemoveEmptyEntries);
|
||||||
if (parts.Length == 0 || parts.Length > 2)
|
if (parts.Length == 0 || parts.Length > 2)
|
||||||
return false;
|
return false;
|
||||||
PaymentType type = PaymentTypes.BTCLike;
|
PaymentType type = PaymentTypes.BTCLike;
|
||||||
|
#if ALTCOINS
|
||||||
|
if (parts[0].ToUpperInvariant() == "XMR")
|
||||||
|
type = PaymentTypes.MoneroLike;
|
||||||
|
#endif
|
||||||
if (parts.Length == 2)
|
if (parts.Length == 2)
|
||||||
{
|
{
|
||||||
if (!PaymentTypes.TryParse(parts[1], out type))
|
if (!PaymentTypes.TryParse(parts[1], out type))
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ namespace BTCPayServer.Payments
|
|||||||
|
|
||||||
public override string ToPrettyString() => "On-Chain";
|
public override string ToPrettyString() => "On-Chain";
|
||||||
public override string GetId() => "BTCLike";
|
public override string GetId() => "BTCLike";
|
||||||
|
public override string ToStringNormalized()
|
||||||
|
{
|
||||||
|
return "OnChain";
|
||||||
|
}
|
||||||
|
|
||||||
public override CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str)
|
public override CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,7 +17,10 @@ namespace BTCPayServer.Payments
|
|||||||
|
|
||||||
public override string ToPrettyString() => "Off-Chain";
|
public override string ToPrettyString() => "Off-Chain";
|
||||||
public override string GetId() => "LightningLike";
|
public override string GetId() => "LightningLike";
|
||||||
|
public override string ToStringNormalized()
|
||||||
|
{
|
||||||
|
return "LightningNetwork";
|
||||||
|
}
|
||||||
public override CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str)
|
public override CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str)
|
||||||
{
|
{
|
||||||
return ((BTCPayNetwork)network)?.ToObject<LightningLikePaymentData>(str);
|
return ((BTCPayNetwork)network)?.ToObject<LightningLikePaymentData>(str);
|
||||||
|
|||||||
@@ -22,6 +22,13 @@ namespace BTCPayServer.Payments
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static LightningPaymentType LightningLike => LightningPaymentType.Instance;
|
public static LightningPaymentType LightningLike => LightningPaymentType.Instance;
|
||||||
|
|
||||||
|
#if ALTCOINS
|
||||||
|
/// <summary>
|
||||||
|
/// Monero payment
|
||||||
|
/// </summary>
|
||||||
|
public static MoneroPaymentType MoneroLike => MoneroPaymentType.Instance;
|
||||||
|
#endif
|
||||||
|
|
||||||
public static bool TryParse(string paymentType, out PaymentType type)
|
public static bool TryParse(string paymentType, out PaymentType type)
|
||||||
{
|
{
|
||||||
switch (paymentType.ToLowerInvariant())
|
switch (paymentType.ToLowerInvariant())
|
||||||
@@ -36,7 +43,7 @@ namespace BTCPayServer.Payments
|
|||||||
break;
|
break;
|
||||||
#if ALTCOINS
|
#if ALTCOINS
|
||||||
case "monerolike":
|
case "monerolike":
|
||||||
type = MoneroPaymentType.Instance;
|
type = PaymentTypes.MoneroLike;
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
@@ -61,6 +68,15 @@ namespace BTCPayServer.Payments
|
|||||||
return GetId();
|
return GetId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A string we can expose to Greenfield API, not subjected to internal legacy
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual string ToStringNormalized()
|
||||||
|
{
|
||||||
|
return ToString();
|
||||||
|
}
|
||||||
|
|
||||||
public abstract string GetId();
|
public abstract string GetId();
|
||||||
public abstract CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str);
|
public abstract CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str);
|
||||||
public abstract string SerializePaymentData(BTCPayNetworkBase network, CryptoPaymentData paymentData);
|
public abstract string SerializePaymentData(BTCPayNetworkBase network, CryptoPaymentData paymentData);
|
||||||
|
|||||||
@@ -14,7 +14,10 @@ namespace BTCPayServer.Services.Altcoins.Monero.Payments
|
|||||||
public override string ToPrettyString() => "On-Chain";
|
public override string ToPrettyString() => "On-Chain";
|
||||||
|
|
||||||
public override string GetId() => "MoneroLike";
|
public override string GetId() => "MoneroLike";
|
||||||
|
public override string ToStringNormalized()
|
||||||
|
{
|
||||||
|
return "Monero";
|
||||||
|
}
|
||||||
|
|
||||||
public override CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str)
|
public override CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -587,7 +587,7 @@
|
|||||||
"items": {
|
"items": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"description": "A specific set of payment methods to use for this invoice (ie. BTC, BTC_OnChain). By default, select all payment methods activated in the store."
|
"description": "A specific set of payment methods to use for this invoice (ie. BTC, BTC-LightningNetwork). By default, select all payment methods activated in the store."
|
||||||
},
|
},
|
||||||
"expirationMinutes": {
|
"expirationMinutes": {
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
|
|||||||
Reference in New Issue
Block a user