From 4e9c0c5198c7884cfd97cb4495f6156632e3124a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 5 Feb 2026 16:44:47 -0800 Subject: payment forwarding --- lib/invoices.ts | 2 ++ lib/payments.ts | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/invoices.ts b/lib/invoices.ts index 4a9d5c89..2d9d82ad 100644 --- a/lib/invoices.ts +++ b/lib/invoices.ts @@ -36,6 +36,7 @@ export const generate = async ({ invoice, user }) => { type = PaymentType.lightning, webhook, secret, + forward, } = invoice; amount = Number.parseInt(amount || 0); @@ -168,6 +169,7 @@ export const generate = async ({ invoice, user }) => { type, uid: user.id, webhook, + forward, }; if (type === "liquid") { diff --git a/lib/payments.ts b/lib/payments.ts index 011807d5..526ae5a2 100644 --- a/lib/payments.ts +++ b/lib/payments.ts @@ -315,7 +315,28 @@ export const completePayment = async (inv, p, user) => { const { id, autowithdraw, threshold, reserve, destination, username } = user; let withdrawal; if (p.confirmed) { - if (autowithdraw) { + if (inv.forward && !inv.forwarded) { + try { + // Skip forward if destination is the same user's own invoice + try { + const destInv = await getInvoice(inv.forward); + if (destInv?.uid === id) { + inv.forwarded = true; + await s(`invoice:${inv.id}`, inv); + callWebhook(inv, p); + return; + } + } catch {} + + inv.forwarded = true; + await s(`invoice:${inv.id}`, inv); + const w = await pay({ amount: p.amount, to: inv.forward, user }); + callWebhook(inv, p); + return w; + } catch (e) { + warn(username, "forward failed", inv.forward, e.message); + } + } else if (autowithdraw && p.type !== "ark") { try { const to = destination.trim(); const balance = await getBalance(id); -- cgit v1.2.3