btcpayserver/BTCPayServer/Controllers/UIInvoiceController.Testing.cs

135 lines
5.5 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Data;
using BTCPayServer.Filters;
Checkout v2 finetuning (#4276) * Indent all JSON files with two spaces * Upgrade Vue.js * Cheat mode improvements * Show payment details in case of expired invoice * Add logo size recommendation * Show clipboard copy hint cursor * Improve info area and wording * Update BIP21 wording * Invoice details adjustments * Remove form; switch payment methods via AJAX * UI updates * Decrease paddings to gain space * Tighten up padding between logo mark and the store title text * Add drop-shadow to the containers * Wording * Cheating improvements * Improve footer spacing * Cheating improvements * Display addresses * More improvements * Expire invoices * Customize invoice expiry * Footer improvements * Remove theme switch * Remove non-existing sourcemap references * Move inline JS to checkout.js file * Plugin compatibility See Kukks/btcpayserver#8 * Test fix * Upgrade vue-i18next * Extract translations into a separate file * Round QR code borders * Remove "Pay with Bitcoin" title in BIP21 case * Add copy hint to payment details * Cheating: Reduce margins * Adjust dt color * Hide addresses for first iteration * Improve View Details button * Make info section collapsible * Revert original en locale file * Checkout v2 tests * Result view link fixes * Fix BIP21 + lazy payment methods case * More result page link improvements * minor visual improvements * Update clipboard code Remove fallback for old browsers. https://caniuse.com/?search=navigator.clipboard * Transition copy symbol * Update info text color * Invert dark neutral colors Simplifies the dark theme quite a bit. * copy adjustments * updates QR border-radius * Add option to remove logo * More checkout v2 test cases * JS improvements * Remove leftovers * Update test * Fix links * Update tests * Update plugins integration * Remove obsolete url code * Minor view update * Update JS to not use arrow functions * Remove FormId from Checkout Appearance settings * Add English-only hint and feedback link * Checkout Appearance: Make options clearer, remove Custom CSS for v2 * Clipboard copy full URL instead of just address/BOLT11 * Upgrade JS libs, add content checks * Add test for BIP21 setting with zero amount invoice Co-authored-by: dstrukt <gfxdsign@gmail.com>
2022-11-24 00:53:32 +01:00
using BTCPayServer.Lightning;
using BTCPayServer.Payments;
using BTCPayServer.Payments.Bitcoin;
using BTCPayServer.Payments.Lightning;
2021-12-31 16:59:02 +09:00
using BTCPayServer.Services;
using BTCPayServer.Services.Invoices;
using Microsoft.AspNetCore.Mvc;
using NBitcoin;
namespace BTCPayServer.Controllers
{
2022-01-07 12:32:00 +09:00
public partial class UIInvoiceController
{
2021-10-11 18:01:32 +09:00
public class FakePaymentRequest
{
public Decimal Amount { get; set; }
public string CryptoCode { get; set; } = "BTC";
Checkout v2 finetuning (#4276) * Indent all JSON files with two spaces * Upgrade Vue.js * Cheat mode improvements * Show payment details in case of expired invoice * Add logo size recommendation * Show clipboard copy hint cursor * Improve info area and wording * Update BIP21 wording * Invoice details adjustments * Remove form; switch payment methods via AJAX * UI updates * Decrease paddings to gain space * Tighten up padding between logo mark and the store title text * Add drop-shadow to the containers * Wording * Cheating improvements * Improve footer spacing * Cheating improvements * Display addresses * More improvements * Expire invoices * Customize invoice expiry * Footer improvements * Remove theme switch * Remove non-existing sourcemap references * Move inline JS to checkout.js file * Plugin compatibility See Kukks/btcpayserver#8 * Test fix * Upgrade vue-i18next * Extract translations into a separate file * Round QR code borders * Remove "Pay with Bitcoin" title in BIP21 case * Add copy hint to payment details * Cheating: Reduce margins * Adjust dt color * Hide addresses for first iteration * Improve View Details button * Make info section collapsible * Revert original en locale file * Checkout v2 tests * Result view link fixes * Fix BIP21 + lazy payment methods case * More result page link improvements * minor visual improvements * Update clipboard code Remove fallback for old browsers. https://caniuse.com/?search=navigator.clipboard * Transition copy symbol * Update info text color * Invert dark neutral colors Simplifies the dark theme quite a bit. * copy adjustments * updates QR border-radius * Add option to remove logo * More checkout v2 test cases * JS improvements * Remove leftovers * Update test * Fix links * Update tests * Update plugins integration * Remove obsolete url code * Minor view update * Update JS to not use arrow functions * Remove FormId from Checkout Appearance settings * Add English-only hint and feedback link * Checkout Appearance: Make options clearer, remove Custom CSS for v2 * Clipboard copy full URL instead of just address/BOLT11 * Upgrade JS libs, add content checks * Add test for BIP21 setting with zero amount invoice Co-authored-by: dstrukt <gfxdsign@gmail.com>
2022-11-24 00:53:32 +01:00
public string PaymentMethodId { get; set; } = "BTC";
2021-10-11 18:01:32 +09:00
}
2021-11-29 07:23:56 +01:00
public class MineBlocksRequest
{
public string PaymentMethodId { get; set; }
public int BlockCount { get; set; } = 1;
2021-11-29 07:23:56 +01:00
public string CryptoCode { get; set; } = "BTC";
}
2021-12-31 16:59:02 +09:00
[HttpPost("i/{invoiceId}/test-payment")]
2021-10-11 12:32:09 +09:00
[CheatModeRoute]
public async Task<IActionResult> TestPayment(string invoiceId, FakePaymentRequest request,
[FromServices] IEnumerable<ICheckoutCheatModeExtension> extensions)
{
var isSats = request.CryptoCode.ToUpper(CultureInfo.InvariantCulture) == "SATS";
var amount = isSats ? new Money(request.Amount, MoneyUnit.Satoshi).ToDecimal(MoneyUnit.BTC) : request.Amount;
var invoice = await _InvoiceRepository.GetInvoice(invoiceId);
var store = await _StoreRepository.FindStore(invoice.StoreId);
PaymentMethodId paymentMethodId = GetPaymentMethodId(request.PaymentMethodId, store);
var paymentMethod = invoice.GetPaymentPrompt(paymentMethodId);
var extension = GetCheatModeExtension(extensions, paymentMethodId);
var details = _handlers.ParsePaymentPromptDetails(paymentMethod);
if (extension is not null)
{
try
2021-10-11 18:01:32 +09:00
{
var result = await extension.PayInvoice(new ICheckoutCheatModeExtension.PayInvoiceContext(
invoice,
amount,
store,
paymentMethod,
details));
return Ok(new
{
Txid = result.TransactionId,
AmountRemaining = result.AmountRemaining ?? paymentMethod.Calculate().Due - amount,
SuccessMessage = result.SuccessMessage ?? $"Created transaction {result.TransactionId}"
});
}
catch (Exception e)
{
return BadRequest(new
{
ErrorMessage = e.Message
});
Checkout v2 finetuning (#4276) * Indent all JSON files with two spaces * Upgrade Vue.js * Cheat mode improvements * Show payment details in case of expired invoice * Add logo size recommendation * Show clipboard copy hint cursor * Improve info area and wording * Update BIP21 wording * Invoice details adjustments * Remove form; switch payment methods via AJAX * UI updates * Decrease paddings to gain space * Tighten up padding between logo mark and the store title text * Add drop-shadow to the containers * Wording * Cheating improvements * Improve footer spacing * Cheating improvements * Display addresses * More improvements * Expire invoices * Customize invoice expiry * Footer improvements * Remove theme switch * Remove non-existing sourcemap references * Move inline JS to checkout.js file * Plugin compatibility See Kukks/btcpayserver#8 * Test fix * Upgrade vue-i18next * Extract translations into a separate file * Round QR code borders * Remove "Pay with Bitcoin" title in BIP21 case * Add copy hint to payment details * Cheating: Reduce margins * Adjust dt color * Hide addresses for first iteration * Improve View Details button * Make info section collapsible * Revert original en locale file * Checkout v2 tests * Result view link fixes * Fix BIP21 + lazy payment methods case * More result page link improvements * minor visual improvements * Update clipboard code Remove fallback for old browsers. https://caniuse.com/?search=navigator.clipboard * Transition copy symbol * Update info text color * Invert dark neutral colors Simplifies the dark theme quite a bit. * copy adjustments * updates QR border-radius * Add option to remove logo * More checkout v2 test cases * JS improvements * Remove leftovers * Update test * Fix links * Update tests * Update plugins integration * Remove obsolete url code * Minor view update * Update JS to not use arrow functions * Remove FormId from Checkout Appearance settings * Add English-only hint and feedback link * Checkout Appearance: Make options clearer, remove Custom CSS for v2 * Clipboard copy full URL instead of just address/BOLT11 * Upgrade JS libs, add content checks * Add test for BIP21 setting with zero amount invoice Co-authored-by: dstrukt <gfxdsign@gmail.com>
2022-11-24 00:53:32 +01:00
}
}
else
{
return BadRequest(new { ErrorMessage = "No ICheatModeExtension registered for this payment method" });
}
}
2021-12-31 16:59:02 +09:00
private static ICheckoutCheatModeExtension GetCheatModeExtension(IEnumerable<ICheckoutCheatModeExtension> extensions, PaymentMethodId paymentMethodId)
{
return extensions.Where(e => e.Handle(paymentMethodId)).FirstOrDefault();
}
private static PaymentMethodId GetPaymentMethodId(string requestPmi, StoreData store)
{
return new[] { store.GetDefaultPaymentId() }
.Concat(store.GetEnabledPaymentIds())
.FirstOrDefault(p => p?.ToString() == requestPmi);
}
[HttpPost("i/{invoiceId}/mine-blocks")]
2021-11-29 07:23:56 +01:00
[CheatModeRoute]
public async Task<IActionResult> MineBlock(string invoiceId, MineBlocksRequest request, [FromServices] IEnumerable<ICheckoutCheatModeExtension> extensions)
2021-11-29 07:23:56 +01:00
{
if (request.BlockCount <= 0)
return BadRequest(new { ErrorMessage = "Number of blocks should be at least 1" });
var invoice = await _InvoiceRepository.GetInvoice(invoiceId);
var store = await _StoreRepository.FindStore(invoice.StoreId);
var paymentMethodId = GetPaymentMethodId(request.PaymentMethodId, store);
var extension = GetCheatModeExtension(extensions, paymentMethodId);
if (extension != null)
2021-11-29 07:23:56 +01:00
{
try
2021-11-29 07:23:56 +01:00
{
var result = await extension.MineBlock(new() { BlockCount = request.BlockCount });
var defaultMessage = $"Mined {request.BlockCount} block{(request.BlockCount == 1 ? "" : "s")} ";
return Ok(new { SuccessMessage = result.SuccessMessage ?? defaultMessage });
}
catch (Exception e)
{
return BadRequest(new { ErrorMessage = e.Message });
2021-11-29 07:23:56 +01:00
}
}
else
return BadRequest(new { ErrorMessage = "No ICheatModeExtension registered for this payment method" });
2021-11-29 07:23:56 +01:00
}
[HttpPost("i/{invoiceId}/expire")]
2021-10-11 12:32:09 +09:00
[CheatModeRoute]
public async Task<IActionResult> Expire(string invoiceId, int seconds)
{
try
{
await _InvoiceRepository.UpdateInvoiceExpiry(invoiceId, TimeSpan.FromSeconds(seconds));
Checkout v2 finetuning (#4276) * Indent all JSON files with two spaces * Upgrade Vue.js * Cheat mode improvements * Show payment details in case of expired invoice * Add logo size recommendation * Show clipboard copy hint cursor * Improve info area and wording * Update BIP21 wording * Invoice details adjustments * Remove form; switch payment methods via AJAX * UI updates * Decrease paddings to gain space * Tighten up padding between logo mark and the store title text * Add drop-shadow to the containers * Wording * Cheating improvements * Improve footer spacing * Cheating improvements * Display addresses * More improvements * Expire invoices * Customize invoice expiry * Footer improvements * Remove theme switch * Remove non-existing sourcemap references * Move inline JS to checkout.js file * Plugin compatibility See Kukks/btcpayserver#8 * Test fix * Upgrade vue-i18next * Extract translations into a separate file * Round QR code borders * Remove "Pay with Bitcoin" title in BIP21 case * Add copy hint to payment details * Cheating: Reduce margins * Adjust dt color * Hide addresses for first iteration * Improve View Details button * Make info section collapsible * Revert original en locale file * Checkout v2 tests * Result view link fixes * Fix BIP21 + lazy payment methods case * More result page link improvements * minor visual improvements * Update clipboard code Remove fallback for old browsers. https://caniuse.com/?search=navigator.clipboard * Transition copy symbol * Update info text color * Invert dark neutral colors Simplifies the dark theme quite a bit. * copy adjustments * updates QR border-radius * Add option to remove logo * More checkout v2 test cases * JS improvements * Remove leftovers * Update test * Fix links * Update tests * Update plugins integration * Remove obsolete url code * Minor view update * Update JS to not use arrow functions * Remove FormId from Checkout Appearance settings * Add English-only hint and feedback link * Checkout Appearance: Make options clearer, remove Custom CSS for v2 * Clipboard copy full URL instead of just address/BOLT11 * Upgrade JS libs, add content checks * Add test for BIP21 setting with zero amount invoice Co-authored-by: dstrukt <gfxdsign@gmail.com>
2022-11-24 00:53:32 +01:00
return Ok(new { SuccessMessage = $"Invoice set to expire in {seconds} seconds." });
}
catch (Exception e)
{
return BadRequest(new { ErrorMessage = e.Message });
}
}
}
}