Send a payment

Sending is two steps: prepare (shows the fee and validates), then send.

Future<breez.Payment?> payInvoice(breez.BreezSdk sdk, String bolt11, {BigInt? amountSats, required String idempotencyKey}) async {
  final parsed = await sdk.parse(input: bolt11);
  if (parsed is! breez.InputType_Bolt11Invoice) throw StateError('not a BOLT11 invoice');
  if (parsed.field0.amountMsat == null && amountSats == null) throw StateError('invoice has no amount');

  final prep = await sdk.prepareSendPayment(
    request: breez.PrepareSendPaymentRequest(
      paymentRequest: breez.PaymentRequest.input(input: bolt11),
      amount: amountSats,           // only for amount-less invoices
    ),
  );
  if (prep.amount < BigInt.from(330)) throw StateError('minimum is 330 sats');
  final m = prep.paymentMethod;
  final fee = m is breez.SendPaymentMethod_Bolt11Invoice ? m.lightningFeeSats : BigInt.zero;
  // show or log: prep.amount + fee; check the balance covers both before sending

  final res = await sdk.sendPayment(
    request: breez.SendPaymentRequest(
      prepareResponse: prep,
      options: const breez.SendPaymentOptions.bolt11Invoice(preferSpark: false, completionTimeoutSecs: 30),
      idempotencyKey: idempotencyKey,   // a UUID you store BEFORE the call: a retry must reuse it
    ),
  );
  return res.payment;   // status completed | pending | failed
}

Rules that matter for money: