mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 14:04:26 +01:00
The Big Cleanup: Refactor BTCPay internals (#5809)
This commit is contained in:
@@ -6,6 +6,8 @@ using BTCPayServer.Data;
|
||||
using BTCPayServer.Filters;
|
||||
using BTCPayServer.Lightning;
|
||||
using BTCPayServer.Payments;
|
||||
using BTCPayServer.Payments.Bitcoin;
|
||||
using BTCPayServer.Payments.Lightning;
|
||||
using BTCPayServer.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NBitcoin;
|
||||
@@ -41,60 +43,61 @@ namespace BTCPayServer.Controllers
|
||||
var btcpayNetwork = _NetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
|
||||
var network = btcpayNetwork.NBitcoinNetwork;
|
||||
var paymentMethodId = new[] { store.GetDefaultPaymentId() }
|
||||
.Concat(store.GetEnabledPaymentIds(_NetworkProvider))
|
||||
.Concat(store.GetEnabledPaymentIds())
|
||||
.FirstOrDefault(p => p?.ToString() == request.PaymentMethodId);
|
||||
|
||||
try
|
||||
{
|
||||
var paymentMethod = invoice.GetPaymentMethod(paymentMethodId);
|
||||
var destination = paymentMethod?.GetPaymentMethodDetails().GetPaymentDestination();
|
||||
var paymentMethod = invoice.GetPaymentPrompt(paymentMethodId);
|
||||
var details = _handlers.ParsePaymentPromptDetails(paymentMethod);
|
||||
var destination = paymentMethod?.Destination;
|
||||
|
||||
switch (paymentMethod?.GetId().PaymentType)
|
||||
if (details is BitcoinPaymentPromptDetails)
|
||||
{
|
||||
case BitcoinPaymentType:
|
||||
var address = BitcoinAddress.Create(destination, network);
|
||||
var txid = (await cheater.CashCow.SendToAddressAsync(address, amount)).ToString();
|
||||
var address = BitcoinAddress.Create(destination, network);
|
||||
var txid = (await cheater.GetCashCow(cryptoCode).SendToAddressAsync(address, amount)).ToString();
|
||||
|
||||
return Ok(new
|
||||
{
|
||||
Txid = txid,
|
||||
AmountRemaining = paymentMethod.Calculate().Due - amount.ToDecimal(MoneyUnit.BTC),
|
||||
SuccessMessage = $"Created transaction {txid}"
|
||||
});
|
||||
}
|
||||
else if (details is LigthningPaymentPromptDetails)
|
||||
{
|
||||
// requires the channels to be set up using the BTCPayServer.Tests/docker-lightning-channel-setup.sh script
|
||||
var lnClient = lightningClientFactoryService.Create(
|
||||
Environment.GetEnvironmentVariable("BTCPAY_BTCEXTERNALLNDREST"),
|
||||
btcpayNetwork);
|
||||
|
||||
var lnAmount = new LightMoney(amount.Satoshi, LightMoneyUnit.Satoshi);
|
||||
var response = await lnClient.Pay(destination, new PayInvoiceParams { Amount = lnAmount });
|
||||
|
||||
if (response.Result == PayResult.Ok)
|
||||
{
|
||||
var bolt11 = BOLT11PaymentRequest.Parse(destination, network);
|
||||
var paymentHash = bolt11.PaymentHash?.ToString();
|
||||
var paid = response.Details.TotalAmount.ToDecimal(LightMoneyUnit.BTC);
|
||||
return Ok(new
|
||||
{
|
||||
Txid = txid,
|
||||
AmountRemaining = paymentMethod.Calculate().Due - amount.ToDecimal(MoneyUnit.BTC),
|
||||
SuccessMessage = $"Created transaction {txid}"
|
||||
});
|
||||
|
||||
case LightningPaymentType:
|
||||
// requires the channels to be set up using the BTCPayServer.Tests/docker-lightning-channel-setup.sh script
|
||||
var lnClient = lightningClientFactoryService.Create(
|
||||
Environment.GetEnvironmentVariable("BTCPAY_BTCEXTERNALLNDREST"),
|
||||
btcpayNetwork);
|
||||
|
||||
var lnAmount = new LightMoney(amount.Satoshi, LightMoneyUnit.Satoshi);
|
||||
var response = await lnClient.Pay(destination, new PayInvoiceParams { Amount = lnAmount });
|
||||
|
||||
if (response.Result == PayResult.Ok)
|
||||
{
|
||||
var bolt11 = BOLT11PaymentRequest.Parse(destination, network);
|
||||
var paymentHash = bolt11.PaymentHash?.ToString();
|
||||
var paid = response.Details.TotalAmount.ToDecimal(LightMoneyUnit.BTC);
|
||||
return Ok(new
|
||||
{
|
||||
Txid = paymentHash,
|
||||
AmountRemaining = paymentMethod.Calculate().TotalDue - paid,
|
||||
SuccessMessage = $"Sent payment {paymentHash}"
|
||||
});
|
||||
}
|
||||
return UnprocessableEntity(new
|
||||
{
|
||||
ErrorMessage = response.ErrorDetail
|
||||
});
|
||||
|
||||
default:
|
||||
return UnprocessableEntity(new
|
||||
{
|
||||
ErrorMessage = $"Payment method {paymentMethodId} is not supported"
|
||||
Txid = paymentHash,
|
||||
AmountRemaining = paymentMethod.Calculate().TotalDue - paid,
|
||||
SuccessMessage = $"Sent payment {paymentHash}"
|
||||
});
|
||||
}
|
||||
return UnprocessableEntity(new
|
||||
{
|
||||
ErrorMessage = response.ErrorDetail
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
return UnprocessableEntity(new
|
||||
{
|
||||
ErrorMessage = $"Payment method {paymentMethodId} is not supported"
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -109,12 +112,12 @@ namespace BTCPayServer.Controllers
|
||||
[CheatModeRoute]
|
||||
public IActionResult MineBlock(string invoiceId, MineBlocksRequest request, [FromServices] Cheater cheater)
|
||||
{
|
||||
var blockRewardBitcoinAddress = cheater.CashCow.GetNewAddress();
|
||||
var blockRewardBitcoinAddress = cheater.GetCashCow(request.CryptoCode).GetNewAddress();
|
||||
try
|
||||
{
|
||||
if (request.BlockCount > 0)
|
||||
{
|
||||
cheater.CashCow.GenerateToAddress(request.BlockCount, blockRewardBitcoinAddress);
|
||||
cheater.GetCashCow(request.CryptoCode).GenerateToAddress(request.BlockCount, blockRewardBitcoinAddress);
|
||||
return Ok(new { SuccessMessage = $"Mined {request.BlockCount} block{(request.BlockCount == 1 ? "" : "s")} " });
|
||||
}
|
||||
return BadRequest(new { ErrorMessage = "Number of blocks should be at least 1" });
|
||||
|
||||
Reference in New Issue
Block a user