@@ -64,7 +64,7 @@ use crate::ln::onion_utils::{HTLCFailReason, INVALID_ONION_BLINDING};
6464use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, CommitmentUpdate, DecodeError, LightningError, MessageSendEvent};
6565#[cfg(test)]
6666use crate::ln::outbound_payment;
67- use crate::ln::outbound_payment::{OutboundPayments, PendingOutboundPayment, RetryableInvoiceRequest, SendAlongPathArgs, StaleExpiration};
67+ use crate::ln::outbound_payment::{Bolt11PaymentError, OutboundPayments, PendingOutboundPayment, RetryableInvoiceRequest, SendAlongPathArgs, StaleExpiration};
6868use crate::offers::invoice::{Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder, UnsignedBolt12Invoice};
6969use crate::offers::invoice_error::InvoiceError;
7070use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestBuilder};
@@ -2042,21 +2042,22 @@ where
20422042/// [`send_payment`].
20432043///
20442044/// ```
2045+ /// # use bitcoin::hashes::Hash;
20452046/// # use lightning::events::{Event, EventsProvider};
20462047/// # use lightning::types::payment::PaymentHash;
2047- /// # use lightning::ln::channelmanager::{AChannelManager, PaymentId, RecentPaymentDetails, RecipientOnionFields, Retry};
2048- /// # use lightning::routing::router::RouteParameters;
2048+ /// # use lightning::ln::channelmanager::{AChannelManager, PaymentId, RecentPaymentDetails, Retry};
2049+ /// # use lightning::routing::router::RouteParametersConfig;
2050+ /// # use lightning_invoice::Bolt11Invoice;
20492051/// #
20502052/// # fn example<T: AChannelManager>(
2051- /// # channel_manager: T, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields ,
2052- /// # route_params: RouteParameters, retry: Retry
2053+ /// # channel_manager: T, invoice: &Bolt11Invoice, route_params_config: RouteParametersConfig ,
2054+ /// # retry: Retry
20532055/// # ) {
20542056/// # let channel_manager = channel_manager.get_cm();
2055- /// // let (payment_hash, recipient_onion, route_params) =
2056- /// // payment::payment_parameters_from_invoice(&invoice);
2057- /// let payment_id = PaymentId([42; 32]);
2058- /// match channel_manager.send_payment(
2059- /// payment_hash, recipient_onion, payment_id, route_params, retry
2057+ /// # let payment_id = PaymentId([42; 32]);
2058+ /// # let payment_hash = PaymentHash((*invoice.payment_hash()).to_byte_array());
2059+ /// match channel_manager.pay_for_bolt11_invoice(
2060+ /// invoice, payment_id, None, route_params_config, retry
20602061/// ) {
20612062/// Ok(()) => println!("Sending payment with hash {}", payment_hash),
20622063/// Err(e) => println!("Failed sending payment with hash {}: {:?}", payment_hash, e),
@@ -4769,6 +4770,34 @@ where
47694770 self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
47704771 }
47714772
4773+ /// Pays a [`Bolt11Invoice`] associated with the `payment_id`. See [`Self::send_payment`] for more info.
4774+ ///
4775+ /// # Payment Id
4776+ /// The invoice's `payment_hash().0` serves as a reliable choice for the `payment_id`.
4777+ ///
4778+ /// # Handling Invoice Amounts
4779+ /// Some invoices include a specific amount, while others require you to specify one.
4780+ /// - If the invoice **includes** an amount, user must not provide `amount_msats`.
4781+ /// - If the invoice **doesn't include** an amount, you'll need to specify `amount_msats`.
4782+ ///
4783+ /// If these conditions aren’t met, the function will return `Bolt11PaymentError::InvalidAmount`.
4784+ ///
4785+ /// # Custom Routing Parameters
4786+ /// Users can customize routing parameters via [`RouteParametersConfig`].
4787+ /// To use default settings, call the function with `RouteParametersConfig::default()`.
4788+ pub fn pay_for_bolt11_invoice(
4789+ &self, invoice: &Bolt11Invoice, payment_id: PaymentId, amount_msats: Option<u64>,
4790+ route_params_config: RouteParametersConfig, retry_strategy: Retry
4791+ ) -> Result<(), Bolt11PaymentError> {
4792+ let best_block_height = self.best_block.read().unwrap().height;
4793+ let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
4794+ self.pending_outbound_payments
4795+ .pay_for_bolt11_invoice(invoice, payment_id, amount_msats, route_params_config, retry_strategy,
4796+ &self.router, self.list_usable_channels(), || self.compute_inflight_htlcs(),
4797+ &self.entropy_source, &self.node_signer, best_block_height, &self.logger,
4798+ &self.pending_events, |args| self.send_payment_along_path(args))
4799+ }
4800+
47724801 /// Pays the [`Bolt12Invoice`] associated with the `payment_id` encoded in its `payer_metadata`.
47734802 ///
47744803 /// The invoice's `payer_metadata` is used to authenticate that the invoice was indeed requested
0 commit comments