@page "/" @using QRCoder @using SixLabors.ImageSharp.Formats @using System.ComponentModel.DataAnnotations @inject IJSRuntime JSRuntime QRPay - QR Payment Generator
@if (!_showQRCode) {

QRPay

Generate QR payment codes instantly

} else {

Payment QR Code

Scan this code with your banking app

Payment QR Code
To: @paymentModel.Name
Amount: €@paymentModel.Amount.ToString("F2")
@if (!string.IsNullOrWhiteSpace(paymentModel.Remark)) {
Reference: @paymentModel.Remark
}
}
@code { private PaymentModel paymentModel = new(); private string QRCodeStr = string.Empty; private bool _showQRCode = false; private bool _isGenerating = false; private bool _isCopying = false; private bool _copySuccess = false; private System.Threading.Timer? _copySuccessTimer; public class PaymentModel { [Required(ErrorMessage = "Beneficiary name is required")] [StringLength(70, ErrorMessage = "Name cannot exceed 70 characters")] public string Name { get; set; } = string.Empty; [Required(ErrorMessage = "IBAN is required")] [RegularExpression(@"^[A-Z]{2}[0-9]{2}[A-Z0-9]{4}[0-9]{7}([A-Z0-9]?){0,16}$", ErrorMessage = "Please enter a valid IBAN")] public string IBAN { get; set; } = string.Empty; [Required(ErrorMessage = "Amount is required")] [Range(0.01, 999999.99, ErrorMessage = "Amount must be between €0.01 and €999,999.99")] public decimal Amount { get; set; } = 0; [StringLength(140, ErrorMessage = "Remark cannot exceed 140 characters")] public string? Remark { get; set; } = string.Empty; } public async Task GenerateQRCode() { _isGenerating = true; StateHasChanged(); // Small delay to show loading state await Task.Delay(500); try { QRCodeStr = string.Empty; if (!string.IsNullOrWhiteSpace(paymentModel.IBAN)) { using (var ms = new MemoryStream()) { QRCodeGenerator qRCodeGenerator = new QRCodeGenerator(); var payment = new PayBySquareTextGenerator.Payment( paymentModel.IBAN.Replace(" ", ""), paymentModel.Amount, "EUR", "1", paymentModel.Remark ?? string.Empty); payment.ConstantSymbol = "0308"; payment.BeneficiaryName = paymentModel.Name; var payBySquare = new PayBySquareTextGenerator.PayBySquareOverkill(payment); var stringData = payBySquare.GeneratePayBySquareOverkillString(); // Generate QR code data - smaller size for better clipboard handling using var qrCodeData = QRCodeGenerator.GenerateQrCode(stringData, QRCodeGenerator.ECCLevel.Q); using var qrCode = new PngByteQRCode(qrCodeData); // Reduced from 20 to 10 for smaller clipboard image byte[] qrCodeImage = qrCode.GetGraphic(10); QRCodeStr = $"data:image/png;base64,{Convert.ToBase64String(qrCodeImage)}"; } _showQRCode = true; } } finally { _isGenerating = false; StateHasChanged(); } } public async Task CopyQRCodeToClipboard() { try { _isCopying = true; _copySuccess = false; StateHasChanged(); // Use JavaScript to copy the QR code image to clipboard with smaller size var success = await JSRuntime.InvokeAsync("copyImageToClipboardSmall", "qrcode-image"); _isCopying = false; if (success) { _copySuccess = true; StateHasChanged(); // Reset the success state after 3 seconds _copySuccessTimer?.Dispose(); _copySuccessTimer = new System.Threading.Timer(_ => { InvokeAsync(() => { _copySuccess = false; StateHasChanged(); }); }, null, TimeSpan.FromSeconds(3), Timeout.InfiniteTimeSpan); } else { StateHasChanged(); // Could show an error message here if needed } } catch (Exception) { _isCopying = false; StateHasChanged(); } } public void GoBack() { _showQRCode = false; QRCodeStr = string.Empty; paymentModel = new PaymentModel(); // Reset the form _copySuccess = false; _copySuccessTimer?.Dispose(); StateHasChanged(); } public void Dispose() { _copySuccessTimer?.Dispose(); } }