@@ -64,7 +64,7 @@ use crate::ln::onion_utils::{HTLCFailReason, INVALID_ONION_BLINDING};
64
64
use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, CommitmentUpdate, DecodeError, LightningError, MessageSendEvent};
65
65
#[cfg(test)]
66
66
use 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};
68
68
use crate::offers::invoice::{Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder, UnsignedBolt12Invoice};
69
69
use crate::offers::invoice_error::InvoiceError;
70
70
use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestBuilder};
@@ -2042,21 +2042,22 @@ where
2042
2042
/// [`send_payment`].
2043
2043
///
2044
2044
/// ```
2045
+ /// # use bitcoin::hashes::Hash;
2045
2046
/// # use lightning::events::{Event, EventsProvider};
2046
2047
/// # 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;
2049
2051
/// #
2050
2052
/// # 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
2053
2055
/// # ) {
2054
2056
/// # 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
2060
2061
/// ) {
2061
2062
/// Ok(()) => println!("Sending payment with hash {}", payment_hash),
2062
2063
/// Err(e) => println!("Failed sending payment with hash {}: {:?}", payment_hash, e),
@@ -4769,6 +4770,34 @@ where
4769
4770
self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
4770
4771
}
4771
4772
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
+
4772
4801
/// Pays the [`Bolt12Invoice`] associated with the `payment_id` encoded in its `payer_metadata`.
4773
4802
///
4774
4803
/// The invoice's `payer_metadata` is used to authenticate that the invoice was indeed requested
0 commit comments