btcpayserver/BTCPayServer/Controllers/UIInvoiceController.Testing.cs

116 lines
4.2 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Data;
using BTCPayServer.Filters;
using BTCPayServer.Payments;
2021-12-31 08:59:02 +01:00
using BTCPayServer.Services;
using Microsoft.AspNetCore.Mvc;
using NBitcoin;
namespace BTCPayServer.Controllers
{
2022-01-07 04:32:00 +01:00
public partial class UIInvoiceController
{
2021-10-11 11:01:32 +02:00
public class FakePaymentRequest
{
public Decimal Amount { get; set; }
public string CryptoCode { get; set; } = "BTC";
2021-10-11 11:01:32 +02:00
}
2021-11-29 07:23:56 +01:00
public class MineBlocksRequest
{
public int BlockCount { get; set; } = 1;
2021-11-29 07:23:56 +01:00
public string CryptoCode { get; set; } = "BTC";
}
2021-12-31 08:59:02 +01:00
[HttpPost]
[Route("i/{invoiceId}/test-payment")]
2021-10-11 05:32:09 +02:00
[CheatModeRoute]
public async Task<IActionResult> TestPayment(string invoiceId, FakePaymentRequest request, [FromServices] Cheater cheater)
{
var invoice = await _InvoiceRepository.GetInvoice(invoiceId);
var store = await _StoreRepository.FindStore(invoice.StoreId);
2021-12-31 08:59:02 +01:00
// TODO support altcoins, not just bitcoin
var network = _NetworkProvider.GetNetwork<BTCPayNetwork>(request.CryptoCode);
var paymentMethodId = new [] {store.GetDefaultPaymentId()}.Concat(store.GetEnabledPaymentIds(_NetworkProvider))
.FirstOrDefault(p => p!= null && p.CryptoCode == request.CryptoCode && p.PaymentType == PaymentTypes.BTCLike);
var bitcoinAddressString = invoice.GetPaymentMethod(paymentMethodId).GetPaymentMethodDetails().GetPaymentDestination();
var bitcoinAddressObj = BitcoinAddress.Create(bitcoinAddressString, network.NBitcoinNetwork);
var BtcAmount = request.Amount;
2021-12-31 08:59:02 +01:00
try
{
var paymentMethod = invoice.GetPaymentMethod(paymentMethodId);
var rate = paymentMethod.Rate;
2021-10-11 11:01:32 +02:00
var txid = cheater.CashCow.SendToAddress(bitcoinAddressObj, new Money(BtcAmount, MoneyUnit.BTC)).ToString();
2021-12-31 08:59:02 +01:00
// TODO The value of totalDue is wrong. How can we get the real total due? invoice.Price is only correct if this is the 2nd payment, not for a 3rd or 4th payment.
var totalDue = invoice.Price;
2021-10-11 11:01:32 +02:00
return Ok(new
{
Txid = txid,
AmountRemaining = (totalDue - (BtcAmount * rate)) / rate,
SuccessMessage = "Created transaction " + txid
});
}
catch (Exception e)
{
2021-10-11 11:01:32 +02:00
return BadRequest(new
{
ErrorMessage = e.Message,
AmountRemaining = invoice.Price
});
}
}
2021-12-31 08:59:02 +01:00
2021-11-29 07:23:56 +01:00
[HttpPost]
[Route("i/{invoiceId}/mine-blocks")]
[CheatModeRoute]
public IActionResult MineBlock(string invoiceId, MineBlocksRequest request, [FromServices] Cheater cheater)
2021-11-29 07:23:56 +01:00
{
// TODO support altcoins, not just bitcoin
var blockRewardBitcoinAddress = cheater.CashCow.GetNewAddress();
2021-11-29 07:23:56 +01:00
try
{
2021-12-31 08:59:02 +01:00
if (request.BlockCount > 0)
2021-11-29 07:23:56 +01:00
{
cheater.CashCow.GenerateToAddress(request.BlockCount, blockRewardBitcoinAddress);
2021-11-29 07:23:56 +01:00
return Ok(new
{
2021-12-31 08:59:02 +01:00
SuccessMessage = "Mined " + request.BlockCount + " blocks"
});
2021-11-29 07:23:56 +01:00
}
return BadRequest(new
{
ErrorMessage = "Number of blocks should be > 0"
});
}
catch (Exception e)
{
return BadRequest(new
{
ErrorMessage = e.Message
});
}
}
[HttpPost]
[Route("i/{invoiceId}/expire")]
2021-10-11 05:32:09 +02:00
[CheatModeRoute]
public async Task<IActionResult> TestExpireNow(string invoiceId, [FromServices] Cheater cheater)
{
try
{
2021-10-11 05:32:09 +02:00
await cheater.UpdateInvoiceExpiry(invoiceId, DateTimeOffset.Now);
return Ok(new { SuccessMessage = "Invoice is now expired." });
}
catch (Exception e)
{
return BadRequest(new { ErrorMessage = e.Message });
}
}
}
}