using System;
using System.Threading;
using System.Threading.Tasks;
namespace Darkmatter.Fonepay
{
///
/// Public facade. Auto-loads config + secrets via .
/// Caller never touches credentials.
///
public sealed class FonepayClient
{
private readonly FonepayApiClient _api;
public FonepayClient() : this(FonepayConfig.Load())
{
}
public FonepayClient(FonepayConfigSO config)
{
var signer = new HmacSha512Signer(config.GetSecretKey());
_api = new FonepayApiClient(config, signer);
}
///
/// Request a new QR code. The returned URL is valid for 15 minutes. Call GetStatusAsync() to check if the QR code has been paid.
///
///
///
///
public Task PurchaseAsync(QrRequest req, CancellationToken ct = default)
=> _api.PostQRAsync(req, ct);
///
/// Check if a QR code has been paid. Call after PostQRAsync() to check if the QR code has been paid. Returns "PAID" if successful, "UNPAID" if not yet paid, or an error message if the PRN is invalid or expired.
///
///
///
///
public Task GetStatusAsync(string prn, CancellationToken ct = default)
=> _api.GetStatusAsync(prn, ct);
///
/// Request a tax refund. Returns "REFUND_SUCCESS" if successful, or an error message if the PRN is invalid, expired, or not eligible for refund.
///
///
///
///
public Task PostTaxRefundAsync(TaxRefundRequest req, CancellationToken ct = default)
=> _api.PostTaxRefundAsync(req, ct);
///
/// Connects to the QR websocket and awaits the terminal payment frame, then auto-disconnects.
/// Pass from .
///
public async Task AwaitPaymentAsync(
string websocketUrl,
Action onQrVerified = null,
CancellationToken ct = default)
{
if (string.IsNullOrEmpty(websocketUrl))
throw new ArgumentException("Websocket URL required", nameof(websocketUrl));
using var ws = new FonepayWebsocketClient();
var tcs = new TaskCompletionSource(
TaskCreationOptions.RunContinuationsAsynchronously);
if (onQrVerified != null)
ws.OnQrVerified += onQrVerified;
ws.OnPaymentReceived += msg => tcs.TrySetResult(msg.Status);
using var ctReg = ct.Register(() => tcs.TrySetCanceled(ct));
try
{
await ws.ConnectAsync(websocketUrl, ct);
return await tcs.Task;
}
finally
{
await ws.DisconnectAsync();
}
}
}
}