2021-10-11 04:11:02 +02:00
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 ;
2021-10-11 04:11:02 +02:00
using Microsoft.AspNetCore.Mvc ;
using NBitcoin ;
namespace BTCPayServer.Controllers
{
2022-01-07 04:32:00 +01:00
public partial class UIInvoiceController
2021-10-11 04:11:02 +02:00
{
2021-10-11 11:01:32 +02:00
public class FakePaymentRequest
{
public Decimal Amount { get ; set ; }
2021-11-11 10:31:15 +01:00
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
{
2021-11-29 11:44:56 +01:00
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
2021-10-11 04:11:02 +02: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 )
2021-10-11 04:11:02 +02:00
{
var invoice = await _InvoiceRepository . GetInvoice ( invoiceId ) ;
var store = await _StoreRepository . FindStore ( invoice . StoreId ) ;
2021-12-31 08:59:02 +01:00
2021-10-11 04:11:02 +02:00
// TODO support altcoins, not just bitcoin
2021-11-11 10:31:15 +01:00
var network = _NetworkProvider . GetNetwork < BTCPayNetwork > ( request . CryptoCode ) ;
2022-06-30 08:43:04 +02:00
var paymentMethodId = new [ ] { store . GetDefaultPaymentId ( ) } . Concat ( store . GetEnabledPaymentIds ( _NetworkProvider ) )
. FirstOrDefault ( p = > p ! = null & & p . CryptoCode = = request . CryptoCode & & p . PaymentType = = PaymentTypes . BTCLike ) ;
2021-10-11 04:11:02 +02:00
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
2021-10-11 04:11:02 +02: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
2021-10-11 04:11:02 +02: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
} ) ;
2021-10-11 04:11:02 +02:00
}
catch ( Exception e )
{
2021-10-11 11:01:32 +02:00
return BadRequest ( new
{
ErrorMessage = e . Message ,
AmountRemaining = invoice . Price
} ) ;
2021-10-11 04:11:02 +02:00
}
}
2021-12-31 08:59:02 +01:00
2021-11-29 07:23:56 +01:00
[HttpPost]
[Route("i/{invoiceId}/mine-blocks")]
[CheatModeRoute]
2021-12-10 12:31:04 +01:00
public IActionResult MineBlock ( string invoiceId , MineBlocksRequest request , [ FromServices ] Cheater cheater )
2021-11-29 07:23:56 +01:00
{
// TODO support altcoins, not just bitcoin
2021-11-29 11:44:56 +01:00
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
{
2021-11-29 11:44: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
} ) ;
}
}
2021-10-11 04:11:02 +02:00
[HttpPost]
[Route("i/{invoiceId}/expire")]
2021-10-11 05:32:09 +02:00
[CheatModeRoute]
public async Task < IActionResult > TestExpireNow ( string invoiceId , [ FromServices ] Cheater cheater )
2021-10-11 04:11:02 +02:00
{
try
{
2021-10-11 05:32:09 +02:00
await cheater . UpdateInvoiceExpiry ( invoiceId , DateTimeOffset . Now ) ;
2021-10-11 10:58:01 +02:00
return Ok ( new { SuccessMessage = "Invoice is now expired." } ) ;
2021-10-11 04:11:02 +02:00
}
catch ( Exception e )
{
2021-10-11 10:58:01 +02:00
return BadRequest ( new { ErrorMessage = e . Message } ) ;
2021-10-11 04:11:02 +02:00
}
}
}
}