Generate Payment link from PaymentType

This commit is contained in:
Kukks 2020-07-29 08:53:30 +02:00
parent c06c5d4104
commit e33200058d
8 changed files with 50 additions and 20 deletions

View file

@ -20,7 +20,8 @@ namespace BTCPayServer
"XMR_X = XMR_BTC * BTC_X",
"XMR_BTC = kraken(XMR_BTC)"
},
CryptoImagePath = "/imlegacy/monero.svg"
CryptoImagePath = "/imlegacy/monero.svg",
UriScheme = "monero"
});
}
}

View file

@ -3,5 +3,6 @@ namespace BTCPayServer
public class MoneroLikeSpecificBtcPayNetwork : BTCPayNetworkBase
{
public int MaxTrackedConfirmation = 10;
public string UriScheme { get; set; }
}
}

View file

@ -2,7 +2,9 @@ using System;
using System.Globalization;
using System.Linq;
using BTCPayServer.Payments.Bitcoin;
using BTCPayServer.Services;
using BTCPayServer.Services.Invoices;
using NBitcoin;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Payments
@ -64,6 +66,19 @@ namespace BTCPayServer.Payments
txId = txId.Split('-').First();
return string.Format(CultureInfo.InvariantCulture, network.BlockExplorerLink, txId);
}
public override string GetPaymentLink(BTCPayNetworkBase network, IPaymentMethodDetails paymentMethodDetails,
Money cryptoInfoDue, string serverUri)
{
var bip21 = ((BTCPayNetwork)network).GenerateBIP21(paymentMethodDetails.GetPaymentDestination(), cryptoInfoDue);
if ((paymentMethodDetails as BitcoinLikeOnChainPaymentMethod)?.PayjoinEnabled is true)
{
bip21 += $"&{PayjoinClient.BIP21EndpointKey}={serverUri.WithTrailingSlash()}{network.CryptoCode}/{PayjoinClient.BIP21EndpointKey}";
}
return bip21;
}
public override string InvoiceViewPaymentPartialName { get; } = "ViewBitcoinLikePaymentData";
}
}

View file

@ -1,5 +1,7 @@
using System;
using BTCPayServer.Payments.Lightning;
using BTCPayServer.Services.Invoices;
using NBitcoin;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@ -8,13 +10,14 @@ namespace BTCPayServer.Payments
public class LightningPaymentType : PaymentType
{
public static LightningPaymentType Instance { get; } = new LightningPaymentType();
private LightningPaymentType()
{
}
public override string ToPrettyString() => "Off-Chain";
public override string GetId() => "LightningLike";
public override CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str)
{
return ((BTCPayNetwork)network).ToObject<LightningLikePaymentData>(str);
@ -35,7 +38,8 @@ namespace BTCPayServer.Payments
return JsonConvert.SerializeObject(details);
}
public override ISupportedPaymentMethod DeserializeSupportedPaymentMethod(BTCPayNetworkBase network, JToken value)
public override ISupportedPaymentMethod DeserializeSupportedPaymentMethod(BTCPayNetworkBase network,
JToken value)
{
return JsonConvert.DeserializeObject<LightningSupportedPaymentMethod>(value.ToString());
}
@ -44,6 +48,14 @@ namespace BTCPayServer.Payments
{
return null;
}
public override string GetPaymentLink(BTCPayNetworkBase network, IPaymentMethodDetails paymentMethodDetails,
Money cryptoInfoDue, string serverUri)
{
return
$"lightning:{paymentMethodDetails.GetPaymentDestination().ToUpperInvariant().Replace("LIGHTNING:", "", StringComparison.InvariantCultureIgnoreCase)}";
}
public override string InvoiceViewPaymentPartialName { get; } = "ViewLightningLikePaymentData";
}
}

View file

@ -1,6 +1,7 @@
using System;
using BTCPayServer.Services.Altcoins.Monero.Payments;
using BTCPayServer.Services.Invoices;
using NBitcoin;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Payments
@ -63,6 +64,8 @@ namespace BTCPayServer.Payments
public abstract string SerializePaymentMethodDetails(BTCPayNetworkBase network, IPaymentMethodDetails details);
public abstract ISupportedPaymentMethod DeserializeSupportedPaymentMethod(BTCPayNetworkBase network, JToken value);
public abstract string GetTransactionLink(BTCPayNetworkBase network, string txId);
public abstract string GetPaymentLink(BTCPayNetworkBase network, IPaymentMethodDetails paymentMethodDetails,
Money cryptoInfoDue, string serverUri);
public abstract string InvoiceViewPaymentPartialName { get; }
}
}

View file

@ -75,20 +75,15 @@ namespace BTCPayServer.Services.Altcoins.Monero.Payments
public override void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse, StoreBlob storeBlob)
{
var paymentMethodId = new PaymentMethodId(model.CryptoCode, PaymentType);
var client = _moneroRpcProvider.WalletRpcClients[model.CryptoCode];
var cryptoInfo = invoiceResponse.CryptoInfo.First(o => o.GetpaymentMethodId() == paymentMethodId);
var network = _networkProvider.GetNetwork<MoneroLikeSpecificBtcPayNetwork>(model.CryptoCode);
model.IsLightning = false;
model.PaymentMethodName = GetPaymentMethodName(network);
model.CryptoImage = GetCryptoImage(network);
model.InvoiceBitcoinUrl = client.SendCommandAsync<MakeUriRequest, MakeUriResponse>("make_uri", new MakeUriRequest()
model.InvoiceBitcoinUrl = MoneroPaymentType.Instance.GetPaymentLink(network, new MoneroLikeOnChainPaymentMethodDetails()
{
Address = cryptoInfo.Address,
Amount = MoneroMoney.Convert(decimal.Parse(cryptoInfo.Due, CultureInfo.InvariantCulture))
}).GetAwaiter()
.GetResult().Uri;
DepositAddress = cryptoInfo.Address
}, cryptoInfo.Due, null);
model.InvoiceBitcoinUrlQR = model.InvoiceBitcoinUrl;
}
public override string GetCryptoImage(PaymentMethodId paymentMethodId)

View file

@ -1,6 +1,7 @@
using System.Globalization;
using BTCPayServer.Payments;
using BTCPayServer.Services.Invoices;
using NBitcoin;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@ -44,6 +45,12 @@ namespace BTCPayServer.Services.Altcoins.Monero.Payments
return string.Format(CultureInfo.InvariantCulture, network.BlockExplorerLink, txId);
}
public override string GetPaymentLink(BTCPayNetworkBase network, IPaymentMethodDetails paymentMethodDetails, Money cryptoInfoDue, string serverUri)
{
return
$"{(network as MoneroLikeSpecificBtcPayNetwork).UriScheme}:{paymentMethodDetails.GetPaymentDestination()}?tx_amount={cryptoInfoDue.ToDecimal(MoneyUnit.BTC)}";
}
public override string InvoiceViewPaymentPartialName { get; } = "Monero/ViewMoneroLikePaymentData";
}
}

View file

@ -456,7 +456,8 @@ namespace BTCPayServer.Services.Invoices
{
cryptoInfo.PaymentUrls = new InvoicePaymentUrls()
{
BOLT11 = $"lightning:{cryptoInfo.Address}"
BOLT11 = paymentId.PaymentType.GetPaymentLink(info.Network, details, cryptoInfo.Due,
ServerUrl)
};
}
else if (paymentId.PaymentType == PaymentTypes.BTCLike)
@ -466,15 +467,10 @@ namespace BTCPayServer.Services.Invoices
minerInfo.SatoshiPerBytes = ((BitcoinLikeOnChainPaymentMethod)details).FeeRate
.GetFee(1).Satoshi;
dto.MinerFees.TryAdd(cryptoInfo.CryptoCode, minerInfo);
var bip21 = ((BTCPayNetwork)info.Network).GenerateBIP21(cryptoInfo.Address, cryptoInfo.Due);
if ((details as BitcoinLikeOnChainPaymentMethod)?.PayjoinEnabled is true)
cryptoInfo.PaymentUrls = new InvoicePaymentUrls()
{
bip21 += $"&{PayjoinClient.BIP21EndpointKey}={ServerUrl.WithTrailingSlash()}{cryptoCode}/{PayjoinClient.BIP21EndpointKey}";
}
cryptoInfo.PaymentUrls = new NBitpayClient.InvoicePaymentUrls()
{
BIP21 = bip21,
BIP21 = paymentId.PaymentType.GetPaymentLink(info.Network, details, cryptoInfo.Due,
ServerUrl)
};
#pragma warning disable 618