From 797d10c9322b2de3ec14fa85689ae70435e98c8f Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 23 Feb 2023 15:47:16 -0800 Subject: fix fee subtraction --- lib/payments.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'lib/payments.js') diff --git a/lib/payments.js b/lib/payments.js index 89652ff4..aaf39210 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -25,9 +25,11 @@ export let debit = async ( type = types.internal, id = v4() ) => { - let ref; + let ref, tip; let invoice = await g(`invoice:${hash}`); - if (invoice) ref = invoice.uid; + if (invoice) { + ref = invoice.uid; + } let { id: uid, currency, username } = user; @@ -44,16 +46,21 @@ export let debit = async ( let covered = Math.min(credit, ourfee) || 0; ourfee -= covered; - l("debiting", user.username, balance, amount, ourfee, amount + ourfee); - if (balance < amount + ourfee) fail("Insufficient funds"); + l("debiting", user.username, balance, amount, fee, ourfee, amount + fee + ourfee); + if (balance < amount + fee + ourfee) fail("Insufficient funds"); await db .multi() .decrBy(`credit:${type}:${uid}`, covered) - .decrBy(`balance:${uid}`, amount + ourfee) + .decrBy(`balance:${uid}`, amount + fee + ourfee) .exec(); }); }); + if (invoice?.tip) { + tip = invoice.tip; + amount -= tip; + } + let p = { id, amount: -amount, @@ -66,6 +73,7 @@ export let debit = async ( currency, type, ref, + tip, created: Date.now() }; -- cgit v1.2.3