Use nicer representation of payment methods in the Greenfield API

This commit is contained in:
nicolas.dorier
2020-08-26 21:24:37 +09:00
parent be21e7d253
commit f2e94e6819
9 changed files with 82 additions and 8 deletions

View File

@@ -877,7 +877,7 @@ normal:
InvoiceEntity invoiceEntity = new InvoiceEntity();
invoiceEntity.Networks = networkProvider;
invoiceEntity.Payments = new System.Collections.Generic.List<PaymentEntity>();
invoiceEntity.ProductInformation = new ProductInformation() { Price = 100 };
invoiceEntity.Price = 100;
PaymentMethodDictionary paymentMethods = new PaymentMethodDictionary();
paymentMethods.Add(new PaymentMethod() { Network = networkBTC, CryptoCode = "BTC", Rate = 10513.44m, }
.SetPaymentMethodDetails(

View File

@@ -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]
[Trait("Fast", "Fast")]
public async Task CheckNoDeadLink()

View File

@@ -267,7 +267,7 @@ namespace BTCPayServer.Controllers.GreenField
Monitoring = entity.MonitoringExpiration - entity.ExpirationTime,
PaymentTolerance = entity.PaymentTolerance,
PaymentMethods =
entity.GetPaymentMethods().Select(method => method.GetId().ToString()).ToArray(),
entity.GetPaymentMethods().Select(method => method.GetId().ToStringNormalized()).ToArray(),
SpeedPolicy = entity.SpeedPolicy
}
};

View File

@@ -64,20 +64,38 @@ namespace BTCPayServer.Payments
//BTCLike case is special because it is in legacy mode.
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()
{
return $"{CryptoCode} ({PaymentType.ToPrettyString()})";
}
static char[] Separators = new[] { '_', '-' };
public static bool TryParse(string str, out PaymentMethodId paymentMethodId)
{
str ??= "";
paymentMethodId = null;
var parts = str.Split('_', StringSplitOptions.RemoveEmptyEntries);
var parts = str.Split(Separators, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0 || parts.Length > 2)
return false;
PaymentType type = PaymentTypes.BTCLike;
#if ALTCOINS
if (parts[0].ToUpperInvariant() == "XMR")
type = PaymentTypes.MoneroLike;
#endif
if (parts.Length == 2)
{
if (!PaymentTypes.TryParse(parts[1], out type))

View File

@@ -19,6 +19,10 @@ namespace BTCPayServer.Payments
public override string ToPrettyString() => "On-Chain";
public override string GetId() => "BTCLike";
public override string ToStringNormalized()
{
return "OnChain";
}
public override CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str)
{

View File

@@ -17,7 +17,10 @@ namespace BTCPayServer.Payments
public override string ToPrettyString() => "Off-Chain";
public override string GetId() => "LightningLike";
public override string ToStringNormalized()
{
return "LightningNetwork";
}
public override CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str)
{
return ((BTCPayNetwork)network)?.ToObject<LightningLikePaymentData>(str);

View File

@@ -22,6 +22,13 @@ namespace BTCPayServer.Payments
/// </summary>
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)
{
switch (paymentType.ToLowerInvariant())
@@ -36,7 +43,7 @@ namespace BTCPayServer.Payments
break;
#if ALTCOINS
case "monerolike":
type = MoneroPaymentType.Instance;
type = PaymentTypes.MoneroLike;
break;
#endif
default:
@@ -61,6 +68,15 @@ namespace BTCPayServer.Payments
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 CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str);
public abstract string SerializePaymentData(BTCPayNetworkBase network, CryptoPaymentData paymentData);

View File

@@ -14,7 +14,10 @@ namespace BTCPayServer.Services.Altcoins.Monero.Payments
public override string ToPrettyString() => "On-Chain";
public override string GetId() => "MoneroLike";
public override string ToStringNormalized()
{
return "Monero";
}
public override CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str)
{

View File

@@ -587,7 +587,7 @@
"items": {
"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": {
"type": "integer",