Add /qr route and refactor payment QR code generation
Introduce a dedicated /qr route for QR code preview via query params. Add PaymentRequest model for payment data and validation, replacing the old inline model. Extract QR code generation to PaymentQrCodeService for reuse. Refactor Home.razor to use new model/service and improve validation/UI. Add EmptyLayout for minimal layout needs. Update .gitignore and _Imports.razor for new structure.
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
@page "/qr"
|
||||
@layout EmptyLayout
|
||||
@using Microsoft.AspNetCore.Components
|
||||
@using QRPay.Models
|
||||
@using QRPay.Services
|
||||
|
||||
<PageTitle>QR Preview</PageTitle>
|
||||
|
||||
<div class="qr-route-page">
|
||||
<h1 class="visually-hidden">QR payment preview</h1>
|
||||
|
||||
@if (_errors.Count > 0)
|
||||
{
|
||||
<div class="qr-route-error" role="alert">
|
||||
<p>Unable to generate QR code.</p>
|
||||
@foreach (var error in _errors)
|
||||
{
|
||||
<p>@error</p>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(_qrCodeDataUri))
|
||||
{
|
||||
<img class="qr-route-image" src="@_qrCodeDataUri" alt="Payment QR code" />
|
||||
}
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[SupplyParameterFromQuery(Name = "amount")]
|
||||
public string? Amount { get; set; }
|
||||
|
||||
[SupplyParameterFromQuery(Name = "iban")]
|
||||
public string? Iban { get; set; }
|
||||
|
||||
[SupplyParameterFromQuery(Name = "name")]
|
||||
public string? Name { get; set; }
|
||||
|
||||
[SupplyParameterFromQuery(Name = "remark")]
|
||||
public string? Remark { get; set; }
|
||||
|
||||
private readonly List<string> _errors = [];
|
||||
private string _qrCodeDataUri = string.Empty;
|
||||
|
||||
protected override void OnParametersSet()
|
||||
{
|
||||
_errors.Clear();
|
||||
_qrCodeDataUri = string.Empty;
|
||||
|
||||
decimal? parsedAmount = null;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Amount))
|
||||
{
|
||||
if (PaymentRequest.TryParseAmount(Amount, out var amount))
|
||||
{
|
||||
parsedAmount = amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
_errors.Add("Invalid amount format. Use a dot as the decimal separator, for example 12.50.");
|
||||
}
|
||||
}
|
||||
|
||||
var paymentRequest = new PaymentRequest
|
||||
{
|
||||
Amount = parsedAmount,
|
||||
IBAN = Iban ?? string.Empty,
|
||||
Name = Name ?? string.Empty,
|
||||
Remark = Remark ?? string.Empty
|
||||
};
|
||||
|
||||
_errors.AddRange(paymentRequest.ValidateForQrRoute());
|
||||
|
||||
if (_errors.Count == 0)
|
||||
{
|
||||
_qrCodeDataUri = PaymentQrCodeService.GenerateDataUri(paymentRequest);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user