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