1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
12 * A utility for paying [`Invoice`]s and sending spontaneous payments.
14 * See [module-level documentation] for details.
16 * [module-level documentation]: crate::payment
18 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
19 public class InvoicePayer extends CommonBase {
20 InvoicePayer(Object _dummy, long ptr) { super(ptr); }
21 @Override @SuppressWarnings("deprecation")
22 protected void finalize() throws Throwable {
24 if (ptr != 0) { bindings.InvoicePayer_free(ptr); }
28 * Creates an invoice payer that retries failed payment paths.
30 * Will forward any [`Event::PaymentPathFailed`] events to the decorated `event_handler` once
31 * `retry` has been exceeded for a given [`Invoice`].
33 public static InvoicePayer of(org.ldk.structs.Payer payer, org.ldk.structs.Router router, org.ldk.structs.Logger logger, org.ldk.structs.EventHandler event_handler, org.ldk.structs.Retry retry) {
34 long ret = bindings.InvoicePayer_new(payer == null ? 0 : payer.ptr, router == null ? 0 : router.ptr, logger == null ? 0 : logger.ptr, event_handler == null ? 0 : event_handler.ptr, retry.ptr);
35 Reference.reachabilityFence(payer);
36 Reference.reachabilityFence(router);
37 Reference.reachabilityFence(logger);
38 Reference.reachabilityFence(event_handler);
39 Reference.reachabilityFence(retry);
40 if (ret >= 0 && ret <= 4096) { return null; }
41 org.ldk.structs.InvoicePayer ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InvoicePayer(null, ret); }
42 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
43 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(payer); };
44 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(router); };
45 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
46 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(event_handler); };
51 * Pays the given [`Invoice`], caching it for later use in case a retry is needed.
53 * [`Invoice::payment_hash`] is used as the [`PaymentId`], which ensures idempotency as long
54 * as the payment is still pending. Once the payment completes or fails, you must ensure that
55 * a second payment with the same [`PaymentHash`] is never sent.
57 * If you wish to use a different payment idempotency token, see
58 * [`Self::pay_invoice_with_id`].
60 public Result_PaymentIdPaymentErrorZ pay_invoice(org.ldk.structs.Invoice invoice) {
61 long ret = bindings.InvoicePayer_pay_invoice(this.ptr, invoice == null ? 0 : invoice.ptr);
62 Reference.reachabilityFence(this);
63 Reference.reachabilityFence(invoice);
64 if (ret >= 0 && ret <= 4096) { return null; }
65 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
66 if (this != null) { this.ptrs_to.add(invoice); };
71 * Pays the given [`Invoice`] with a custom idempotency key, caching the invoice for later use
72 * in case a retry is needed.
74 * Note that idempotency is only guaranteed as long as the payment is still pending. Once the
75 * payment completes or fails, no idempotency guarantees are made.
77 * You should ensure that the [`Invoice::payment_hash`] is unique and the same [`PaymentHash`]
78 * has never been paid before.
80 * See [`Self::pay_invoice`] for a variant which uses the [`PaymentHash`] for the idempotency
83 public Result_NonePaymentErrorZ pay_invoice_with_id(org.ldk.structs.Invoice invoice, byte[] payment_id) {
84 long ret = bindings.InvoicePayer_pay_invoice_with_id(this.ptr, invoice == null ? 0 : invoice.ptr, InternalUtils.check_arr_len(payment_id, 32));
85 Reference.reachabilityFence(this);
86 Reference.reachabilityFence(invoice);
87 Reference.reachabilityFence(payment_id);
88 if (ret >= 0 && ret <= 4096) { return null; }
89 Result_NonePaymentErrorZ ret_hu_conv = Result_NonePaymentErrorZ.constr_from_ptr(ret);
90 if (this != null) { this.ptrs_to.add(invoice); };
95 * Pays the given zero-value [`Invoice`] using the given amount, caching it for later use in
96 * case a retry is needed.
98 * [`Invoice::payment_hash`] is used as the [`PaymentId`], which ensures idempotency as long
99 * as the payment is still pending. Once the payment completes or fails, you must ensure that
100 * a second payment with the same [`PaymentHash`] is never sent.
102 * If you wish to use a different payment idempotency token, see
103 * [`Self::pay_zero_value_invoice_with_id`].
105 public Result_PaymentIdPaymentErrorZ pay_zero_value_invoice(org.ldk.structs.Invoice invoice, long amount_msats) {
106 long ret = bindings.InvoicePayer_pay_zero_value_invoice(this.ptr, invoice == null ? 0 : invoice.ptr, amount_msats);
107 Reference.reachabilityFence(this);
108 Reference.reachabilityFence(invoice);
109 Reference.reachabilityFence(amount_msats);
110 if (ret >= 0 && ret <= 4096) { return null; }
111 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
112 if (this != null) { this.ptrs_to.add(invoice); };
117 * Pays the given zero-value [`Invoice`] using the given amount and custom idempotency key,
118 * caching the invoice for later use in case a retry is needed.
120 * Note that idempotency is only guaranteed as long as the payment is still pending. Once the
121 * payment completes or fails, no idempotency guarantees are made.
123 * You should ensure that the [`Invoice::payment_hash`] is unique and the same [`PaymentHash`]
124 * has never been paid before.
126 * See [`Self::pay_zero_value_invoice`] for a variant which uses the [`PaymentHash`] for the
129 public Result_NonePaymentErrorZ pay_zero_value_invoice_with_id(org.ldk.structs.Invoice invoice, long amount_msats, byte[] payment_id) {
130 long ret = bindings.InvoicePayer_pay_zero_value_invoice_with_id(this.ptr, invoice == null ? 0 : invoice.ptr, amount_msats, InternalUtils.check_arr_len(payment_id, 32));
131 Reference.reachabilityFence(this);
132 Reference.reachabilityFence(invoice);
133 Reference.reachabilityFence(amount_msats);
134 Reference.reachabilityFence(payment_id);
135 if (ret >= 0 && ret <= 4096) { return null; }
136 Result_NonePaymentErrorZ ret_hu_conv = Result_NonePaymentErrorZ.constr_from_ptr(ret);
137 if (this != null) { this.ptrs_to.add(invoice); };
142 * Pays `pubkey` an amount using the hash of the given preimage, caching it for later use in
143 * case a retry is needed.
145 * The hash of the [`PaymentPreimage`] is used as the [`PaymentId`], which ensures idempotency
146 * as long as the payment is still pending. Once the payment completes or fails, you must
147 * ensure that a second payment with the same [`PaymentPreimage`] is never sent.
149 public Result_PaymentIdPaymentErrorZ pay_pubkey(byte[] pubkey, byte[] payment_preimage, long amount_msats, int final_cltv_expiry_delta) {
150 long ret = bindings.InvoicePayer_pay_pubkey(this.ptr, InternalUtils.check_arr_len(pubkey, 33), InternalUtils.check_arr_len(payment_preimage, 32), amount_msats, final_cltv_expiry_delta);
151 Reference.reachabilityFence(this);
152 Reference.reachabilityFence(pubkey);
153 Reference.reachabilityFence(payment_preimage);
154 Reference.reachabilityFence(amount_msats);
155 Reference.reachabilityFence(final_cltv_expiry_delta);
156 if (ret >= 0 && ret <= 4096) { return null; }
157 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
162 * Pays `pubkey` an amount using the hash of the given preimage and a custom idempotency key,
163 * caching the invoice for later use in case a retry is needed.
165 * Note that idempotency is only guaranteed as long as the payment is still pending. Once the
166 * payment completes or fails, no idempotency guarantees are made.
168 * You should ensure that the [`PaymentPreimage`] is unique and the corresponding
169 * [`PaymentHash`] has never been paid before.
171 public Result_NonePaymentErrorZ pay_pubkey_with_id(byte[] pubkey, byte[] payment_preimage, byte[] payment_id, long amount_msats, int final_cltv_expiry_delta) {
172 long ret = bindings.InvoicePayer_pay_pubkey_with_id(this.ptr, InternalUtils.check_arr_len(pubkey, 33), InternalUtils.check_arr_len(payment_preimage, 32), InternalUtils.check_arr_len(payment_id, 32), amount_msats, final_cltv_expiry_delta);
173 Reference.reachabilityFence(this);
174 Reference.reachabilityFence(pubkey);
175 Reference.reachabilityFence(payment_preimage);
176 Reference.reachabilityFence(payment_id);
177 Reference.reachabilityFence(amount_msats);
178 Reference.reachabilityFence(final_cltv_expiry_delta);
179 if (ret >= 0 && ret <= 4096) { return null; }
180 Result_NonePaymentErrorZ ret_hu_conv = Result_NonePaymentErrorZ.constr_from_ptr(ret);
185 * Removes the payment cached by the given payment hash.
187 * Should be called once a payment has failed or succeeded if not using [`InvoicePayer`] as an
188 * [`EventHandler`]. Otherwise, calling this method is unnecessary.
190 public void remove_cached_payment(byte[] payment_hash) {
191 bindings.InvoicePayer_remove_cached_payment(this.ptr, InternalUtils.check_arr_len(payment_hash, 32));
192 Reference.reachabilityFence(this);
193 Reference.reachabilityFence(payment_hash);
197 * Constructs a new EventHandler which calls the relevant methods on this_arg.
198 * This copies the `inner` pointer in this_arg and thus the returned EventHandler must be freed before this_arg is
200 public EventHandler as_EventHandler() {
201 long ret = bindings.InvoicePayer_as_EventHandler(this.ptr);
202 Reference.reachabilityFence(this);
203 if (ret >= 0 && ret <= 4096) { return null; }
204 EventHandler ret_hu_conv = new EventHandler(null, ret);
205 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };