diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/invoices.ts | 2 | ||||
| -rw-r--r-- | lib/payments.ts | 23 |
2 files changed, 24 insertions, 1 deletions
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); |
