# Receiving Payments

To receive a payment, you'll need to create an invoice of your own with an amount and description. ChannelManager contains the remaining information needed for the invoice. Use the provided utility to generate an invoice and register a pending payment in ChannelManager.

  • Rust
  • Kotlin
  • Swift
let invoice = match utils::create_invoice_from_channelmanager(
    channel_manager,
    keys_manager,
    logger,
    currency,
    Some(amt_msat),
    "description".to_string(),
    expiry_secs,
    None,
) {
    Ok(inv) => {
        println!("SUCCESS: generated invoice: {}", inv);
        inv
    }
    Err(e) => {
        println!("ERROR: failed to create invoice: {:?}", e);
        return;
    }
};

let payment_hash = PaymentHash(invoice.payment_hash().to_byte_array());
inbound_payments.payments.insert(
    payment_hash,
    PaymentInfo {
        preimage: None,
        secret: Some(invoice.payment_secret().clone()),
        status: HTLCStatus::Pending,
        amt_msat: MillisatAmount(Some(amt_msat)),
    },
);

While it is possible to create an invoice without using the utility, ChannelManager will reject any incoming HTLCs for unregistered payments to protect your privacy. In this case, use either create_inbound_payment or create_inbound_payment_for_hash to register a payment with ChannelManager before creating the invoice with the returned payment hash and/or secret. You might also opt to for inbound_payment, useful for generating invoices for phantom node payments (opens new window) without a ChannelManager.

# PaymentClaimable Event Handling

As with sending a payment, LDK will generate an event once a payment is received. It is your responsibility to handle the PaymentClaimable event by using ChannelManager to release the preimage and claim the funds.

  • Rust
  • Kotlin
  • Swift
Event::PaymentClaimable {
    payment_hash,
    purpose,
    amount_msat,
    receiver_node_id: _,
    via_channel_id: _,
    via_user_channel_id: _,
    claim_deadline: _,
    onion_fields: _,
    counterparty_skimmed_fee_msat: _,
} => {
    println!(
        "\nEVENT: received payment from payment hash {} of {} millisatoshis",
        payment_hash, amount_msat,
    );
    print!("> ");
    io::stdout().flush().unwrap();
    let payment_preimage = match purpose {
        PaymentPurpose::InvoicePayment { payment_preimage, .. } => payment_preimage,
        PaymentPurpose::SpontaneousPayment(preimage) => Some(preimage),
    };
    channel_manager.claim_funds(payment_preimage.unwrap());
}

References: Rust PaymentClaimable docs (opens new window), Java/Kotlin PaymentClaimable bindings (opens new window)

Last Updated: 11/18/2024, 6:18:24 PM