# Sending Payments
Lightning payments are used to pay invoices, which are typically encoded as a
string in accordance with BOLT 11. After parsing the invoice, you'll need to
find a route from your node to the recipient and then make the payment using
ChannelManager
.
- Rust
- Java
// Parse the invoice.
let invoice = Invoice::from_str(encoded_invoice)
.expect("ERROR: failed to parse invoice");
let amt_pico_btc = invoice.amount_pico_btc()
.expect("ERROR: invalid invoice: must contain amount to pay");
let amt_msat = amt_pico_btc / 10;
let payer_pubkey = channel_manager.get_our_node_id();
let network_graph = router.network_graph.read().unwrap();
let payee_pubkey = invoice.recover_payee_pub_key();
let payee_features = invoice.features().cloned();
let first_hops = channel_manager.list_usable_channels();
let last_hops = invoice.route_hints();
let final_cltv = invoice.min_final_cltv_expiry() as u32;
// Find a route and send the payment.
let route = router::get_route(
&payer_pubkey, &network_graph, &payee_pubkey, payee_features,
Some(&first_hops.iter().collect::<Vec<_>>()), &last_hops,
amt_msat, final_cltv, logger.clone(),
).expect("ERROR: failed to find route");
let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
let payment_secret = invoice.payment_secret().cloned();
channel_manager.send_payment(&route, payment_hash, &payment_secret)
.expect("ERROR: failed to send payment");
An event is generated once a payment has completed. Successful payments result
in a PaymentSent
event with the preimage of the payment hash. Be sure to look
out for a PaymentFailed
event, if the payment fails for some reason, and act
accordingly.
- Rust
- Java
// In the event handler passed to BackgroundProcessor::start
match event {
Event::PaymentSent { payment_preimage } => {
// Handle successful payment
}
Event::PaymentFailed { payment_hash, rejected_by_dest } => {
// Handle failed payment
}
// ...
}