2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2018-02-19 23:13:23 +09:00
|
|
|
|
|
|
|
namespace BTCPayServer.Payments
|
|
|
|
{
|
2018-02-20 12:45:04 +09:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A value object which represent a crypto currency with his payment type (ie, onchain or offchain)
|
|
|
|
/// </summary>
|
2018-02-19 23:13:23 +09:00
|
|
|
public class PaymentMethodId
|
|
|
|
{
|
2019-06-04 08:59:01 +09:00
|
|
|
public PaymentMethodId(string cryptoCode, PaymentType paymentType)
|
2018-02-19 23:13:23 +09:00
|
|
|
{
|
|
|
|
if (cryptoCode == null)
|
|
|
|
throw new ArgumentNullException(nameof(cryptoCode));
|
2019-06-04 08:59:01 +09:00
|
|
|
if (paymentType == null)
|
|
|
|
throw new ArgumentNullException(nameof(paymentType));
|
2018-02-19 23:13:23 +09:00
|
|
|
PaymentType = paymentType;
|
2019-05-29 14:33:31 +00:00
|
|
|
CryptoCode = cryptoCode.ToUpperInvariant();
|
2018-02-19 23:13:23 +09:00
|
|
|
}
|
2018-02-20 12:45:04 +09:00
|
|
|
|
|
|
|
[Obsolete("Should only be used for legacy stuff")]
|
|
|
|
public bool IsBTCOnChain
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return CryptoCode == "BTC" && PaymentType == PaymentTypes.BTCLike;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 23:13:23 +09:00
|
|
|
public string CryptoCode { get; private set; }
|
2019-06-04 08:59:01 +09:00
|
|
|
public PaymentType PaymentType { get; private set; }
|
2018-02-19 23:13:23 +09:00
|
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
PaymentMethodId item = obj as PaymentMethodId;
|
|
|
|
if (item == null)
|
|
|
|
return false;
|
|
|
|
return ToString().Equals(item.ToString(), StringComparison.InvariantCulture);
|
|
|
|
}
|
|
|
|
public static bool operator ==(PaymentMethodId a, PaymentMethodId b)
|
|
|
|
{
|
|
|
|
if (System.Object.ReferenceEquals(a, b))
|
|
|
|
return true;
|
|
|
|
if (((object)a == null) || ((object)b == null))
|
|
|
|
return false;
|
|
|
|
return a.ToString() == b.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator !=(PaymentMethodId a, PaymentMethodId b)
|
|
|
|
{
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
#pragma warning disable CA1307 // Specify StringComparison
|
|
|
|
return ToString().GetHashCode();
|
|
|
|
#pragma warning restore CA1307 // Specify StringComparison
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
2019-05-29 14:33:31 +00:00
|
|
|
//BTCLike case is special because it is in legacy mode.
|
2019-05-24 06:38:47 +00:00
|
|
|
return PaymentType == PaymentTypes.BTCLike ? CryptoCode : $"{CryptoCode}_{PaymentType}";
|
2018-02-19 23:13:23 +09:00
|
|
|
}
|
|
|
|
|
2019-06-04 01:06:03 +09:00
|
|
|
public string ToPrettyString()
|
|
|
|
{
|
2019-06-04 08:59:01 +09:00
|
|
|
return $"{CryptoCode} ({PaymentType.ToPrettyString()})";
|
2019-06-04 01:06:03 +09:00
|
|
|
}
|
|
|
|
|
2019-02-21 19:34:11 +09:00
|
|
|
public static bool TryParse(string str, out PaymentMethodId paymentMethodId)
|
|
|
|
{
|
|
|
|
paymentMethodId = null;
|
|
|
|
var parts = str.Split('_', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
if (parts.Length == 0 || parts.Length > 2)
|
|
|
|
return false;
|
2019-06-04 08:59:01 +09:00
|
|
|
PaymentType type = PaymentTypes.BTCLike;
|
2019-02-21 19:34:11 +09:00
|
|
|
if (parts.Length == 2)
|
|
|
|
{
|
2019-06-04 08:59:01 +09:00
|
|
|
if (!PaymentTypes.TryParse(parts[1], out type))
|
|
|
|
return false;
|
2019-02-21 19:34:11 +09:00
|
|
|
}
|
|
|
|
paymentMethodId = new PaymentMethodId(parts[0], type);
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-19 23:13:23 +09:00
|
|
|
public static PaymentMethodId Parse(string str)
|
|
|
|
{
|
2019-02-21 19:34:11 +09:00
|
|
|
if (!TryParse(str, out var result))
|
|
|
|
throw new FormatException("Invalid PaymentMethodId");
|
|
|
|
return result;
|
2018-02-19 23:13:23 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|