fetchInvoice method

Future<InvoiceResponse?> fetchInvoice({
  1. required LnurlResponse lnurlResponse,
  2. required int amountSats,
  3. ZapRequest? zapRequest,
  4. String? comment,
})

fetch invoice from callback

Implementation

Future<InvoiceResponse?> fetchInvoice(
    {required LnurlResponse lnurlResponse,
    required int amountSats,
    ZapRequest? zapRequest,
    String? comment}) async {
  var callback = lnurlResponse.callback!;
  if (callback.contains("?")) {
    callback += "&";
  } else {
    callback += "?";
  }

  final amount = amountSats * 1000;
  callback += "amount=$amount";

  if (comment != null && comment.trim() != '') {
    var commentNum = lnurlResponse.commentAllowed;
    if (commentNum != null) {
      if (commentNum < comment.length) {
        comment = comment.substring(0, commentNum);
      }
      callback += "&comment=${Uri.encodeQueryComponent(comment)}";
    }
  }

  // ZAP ?
  if (lnurlResponse.doesAllowsNostr &&
      zapRequest != null &&
      zapRequest.sig.isNotEmpty) {
    Logger.log.d(jsonEncode(zapRequest));
    var eventStr = Uri.encodeQueryComponent(jsonEncode(zapRequest));
    callback += "&nostr=$eventStr";
  }

  Logger.log.d("getInvoice callback $callback");

  try {
    var response = await _transport.fetchInvoice(callback);
    String invoice = response!["pr"];
    return InvoiceResponse(
        invoice: invoice,
        amountSats: amountSats,
        nostrPubkey: lnurlResponse.nostrPubkey);
  } catch (e) {
    Logger.log.d(e);
  }
  return null;
}